]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
43da3d12 GR |
2 | * gl520sm.c - Part of lm_sensors, Linux kernel modules for hardware |
3 | * monitoring | |
4 | * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>, | |
5 | * Kyösti Mälkki <kmalkki@cc.hut.fi> | |
6 | * Copyright (c) 2005 Maarten Deprez <maartendeprez@users.sourceforge.net> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | * | |
22 | */ | |
1da177e4 LT |
23 | |
24 | #include <linux/module.h> | |
25 | #include <linux/init.h> | |
26 | #include <linux/slab.h> | |
0cacdf29 | 27 | #include <linux/jiffies.h> |
1da177e4 | 28 | #include <linux/i2c.h> |
943b0830 | 29 | #include <linux/hwmon.h> |
86d47f12 | 30 | #include <linux/hwmon-sysfs.h> |
303760b4 | 31 | #include <linux/hwmon-vid.h> |
943b0830 | 32 | #include <linux/err.h> |
9a61bf63 | 33 | #include <linux/mutex.h> |
87808be4 | 34 | #include <linux/sysfs.h> |
1da177e4 LT |
35 | |
36 | /* Type of the extra sensor */ | |
37 | static unsigned short extra_sensor_type; | |
38 | module_param(extra_sensor_type, ushort, 0); | |
39 | MODULE_PARM_DESC(extra_sensor_type, "Type of extra sensor (0=autodetect, 1=temperature, 2=voltage)"); | |
40 | ||
41 | /* Addresses to scan */ | |
25e9c86d | 42 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END }; |
1da177e4 | 43 | |
43da3d12 GR |
44 | /* |
45 | * Many GL520 constants specified below | |
46 | * One of the inputs can be configured as either temp or voltage. | |
47 | * That's why _TEMP2 and _IN4 access the same register | |
48 | */ | |
1da177e4 LT |
49 | |
50 | /* The GL520 registers */ | |
51 | #define GL520_REG_CHIP_ID 0x00 | |
52 | #define GL520_REG_REVISION 0x01 | |
53 | #define GL520_REG_CONF 0x03 | |
54 | #define GL520_REG_MASK 0x11 | |
55 | ||
56 | #define GL520_REG_VID_INPUT 0x02 | |
57 | ||
8b4b0ab4 JD |
58 | static const u8 GL520_REG_IN_INPUT[] = { 0x15, 0x14, 0x13, 0x0d, 0x0e }; |
59 | static const u8 GL520_REG_IN_LIMIT[] = { 0x0c, 0x09, 0x0a, 0x0b }; | |
60 | static const u8 GL520_REG_IN_MIN[] = { 0x0c, 0x09, 0x0a, 0x0b, 0x18 }; | |
61 | static const u8 GL520_REG_IN_MAX[] = { 0x0c, 0x09, 0x0a, 0x0b, 0x17 }; | |
62 | ||
63 | static const u8 GL520_REG_TEMP_INPUT[] = { 0x04, 0x0e }; | |
64 | static const u8 GL520_REG_TEMP_MAX[] = { 0x05, 0x17 }; | |
65 | static const u8 GL520_REG_TEMP_MAX_HYST[] = { 0x06, 0x18 }; | |
1da177e4 LT |
66 | |
67 | #define GL520_REG_FAN_INPUT 0x07 | |
68 | #define GL520_REG_FAN_MIN 0x08 | |
69 | #define GL520_REG_FAN_DIV 0x0f | |
70 | #define GL520_REG_FAN_OFF GL520_REG_FAN_DIV | |
71 | ||
72 | #define GL520_REG_ALARMS 0x12 | |
73 | #define GL520_REG_BEEP_MASK 0x10 | |
74 | #define GL520_REG_BEEP_ENABLE GL520_REG_CONF | |
75 | ||
1da177e4 LT |
76 | /* Client data */ |
77 | struct gl520_data { | |
1beeffe4 | 78 | struct device *hwmon_dev; |
9a61bf63 | 79 | struct mutex update_lock; |
1da177e4 LT |
80 | char valid; /* zero until the following fields are valid */ |
81 | unsigned long last_updated; /* in jiffies */ | |
82 | ||
83 | u8 vid; | |
84 | u8 vrm; | |
85 | u8 in_input[5]; /* [0] = VVD */ | |
86 | u8 in_min[5]; /* [0] = VDD */ | |
87 | u8 in_max[5]; /* [0] = VDD */ | |
88 | u8 fan_input[2]; | |
89 | u8 fan_min[2]; | |
90 | u8 fan_div[2]; | |
91 | u8 fan_off; | |
92 | u8 temp_input[2]; | |
93 | u8 temp_max[2]; | |
94 | u8 temp_max_hyst[2]; | |
95 | u8 alarms; | |
96 | u8 beep_enable; | |
97 | u8 beep_mask; | |
98 | u8 alarm_mask; | |
99 | u8 two_temps; | |
100 | }; | |
101 | ||
df51f2d7 AL |
102 | /* |
103 | * Registers 0x07 to 0x0c are word-sized, others are byte-sized | |
104 | * GL520 uses a high-byte first convention | |
105 | */ | |
106 | static int gl520_read_value(struct i2c_client *client, u8 reg) | |
107 | { | |
108 | if ((reg >= 0x07) && (reg <= 0x0c)) | |
109 | return i2c_smbus_read_word_swapped(client, reg); | |
110 | else | |
111 | return i2c_smbus_read_byte_data(client, reg); | |
112 | } | |
113 | ||
114 | static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value) | |
115 | { | |
116 | if ((reg >= 0x07) && (reg <= 0x0c)) | |
117 | return i2c_smbus_write_word_swapped(client, reg, value); | |
118 | else | |
119 | return i2c_smbus_write_byte_data(client, reg, value); | |
120 | } | |
121 | ||
122 | static struct gl520_data *gl520_update_device(struct device *dev) | |
123 | { | |
124 | struct i2c_client *client = to_i2c_client(dev); | |
125 | struct gl520_data *data = i2c_get_clientdata(client); | |
126 | int val, i; | |
127 | ||
128 | mutex_lock(&data->update_lock); | |
129 | ||
130 | if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) { | |
131 | ||
132 | dev_dbg(&client->dev, "Starting gl520sm update\n"); | |
133 | ||
134 | data->alarms = gl520_read_value(client, GL520_REG_ALARMS); | |
135 | data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK); | |
136 | data->vid = gl520_read_value(client, | |
137 | GL520_REG_VID_INPUT) & 0x1f; | |
138 | ||
139 | for (i = 0; i < 4; i++) { | |
140 | data->in_input[i] = gl520_read_value(client, | |
141 | GL520_REG_IN_INPUT[i]); | |
142 | val = gl520_read_value(client, GL520_REG_IN_LIMIT[i]); | |
143 | data->in_min[i] = val & 0xff; | |
144 | data->in_max[i] = (val >> 8) & 0xff; | |
145 | } | |
146 | ||
147 | val = gl520_read_value(client, GL520_REG_FAN_INPUT); | |
148 | data->fan_input[0] = (val >> 8) & 0xff; | |
149 | data->fan_input[1] = val & 0xff; | |
150 | ||
151 | val = gl520_read_value(client, GL520_REG_FAN_MIN); | |
152 | data->fan_min[0] = (val >> 8) & 0xff; | |
153 | data->fan_min[1] = val & 0xff; | |
154 | ||
155 | data->temp_input[0] = gl520_read_value(client, | |
156 | GL520_REG_TEMP_INPUT[0]); | |
157 | data->temp_max[0] = gl520_read_value(client, | |
158 | GL520_REG_TEMP_MAX[0]); | |
159 | data->temp_max_hyst[0] = gl520_read_value(client, | |
160 | GL520_REG_TEMP_MAX_HYST[0]); | |
161 | ||
162 | val = gl520_read_value(client, GL520_REG_FAN_DIV); | |
163 | data->fan_div[0] = (val >> 6) & 0x03; | |
164 | data->fan_div[1] = (val >> 4) & 0x03; | |
165 | data->fan_off = (val >> 2) & 0x01; | |
166 | ||
167 | data->alarms &= data->alarm_mask; | |
168 | ||
169 | val = gl520_read_value(client, GL520_REG_CONF); | |
170 | data->beep_enable = !((val >> 2) & 1); | |
171 | ||
172 | /* Temp1 and Vin4 are the same input */ | |
173 | if (data->two_temps) { | |
174 | data->temp_input[1] = gl520_read_value(client, | |
175 | GL520_REG_TEMP_INPUT[1]); | |
176 | data->temp_max[1] = gl520_read_value(client, | |
177 | GL520_REG_TEMP_MAX[1]); | |
178 | data->temp_max_hyst[1] = gl520_read_value(client, | |
179 | GL520_REG_TEMP_MAX_HYST[1]); | |
180 | } else { | |
181 | data->in_input[4] = gl520_read_value(client, | |
182 | GL520_REG_IN_INPUT[4]); | |
183 | data->in_min[4] = gl520_read_value(client, | |
184 | GL520_REG_IN_MIN[4]); | |
185 | data->in_max[4] = gl520_read_value(client, | |
186 | GL520_REG_IN_MAX[4]); | |
187 | } | |
188 | ||
189 | data->last_updated = jiffies; | |
190 | data->valid = 1; | |
191 | } | |
192 | ||
193 | mutex_unlock(&data->update_lock); | |
194 | ||
195 | return data; | |
196 | } | |
197 | ||
1da177e4 LT |
198 | /* |
199 | * Sysfs stuff | |
200 | */ | |
201 | ||
86d47f12 JD |
202 | static ssize_t get_cpu_vid(struct device *dev, struct device_attribute *attr, |
203 | char *buf) | |
1da177e4 | 204 | { |
86d47f12 | 205 | struct gl520_data *data = gl520_update_device(dev); |
1da177e4 LT |
206 | return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm)); |
207 | } | |
86d47f12 | 208 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, get_cpu_vid, NULL); |
1da177e4 | 209 | |
43da3d12 | 210 | #define VDD_FROM_REG(val) (((val) * 95 + 2) / 4) |
2a844c14 | 211 | #define VDD_TO_REG(val) clamp_val((((val) * 4 + 47) / 95), 0, 255) |
1da177e4 | 212 | |
43da3d12 | 213 | #define IN_FROM_REG(val) ((val) * 19) |
2a844c14 | 214 | #define IN_TO_REG(val) clamp_val((((val) + 9) / 19), 0, 255) |
1da177e4 | 215 | |
86d47f12 JD |
216 | static ssize_t get_in_input(struct device *dev, struct device_attribute *attr, |
217 | char *buf) | |
1da177e4 | 218 | { |
86d47f12 JD |
219 | int n = to_sensor_dev_attr(attr)->index; |
220 | struct gl520_data *data = gl520_update_device(dev); | |
1da177e4 LT |
221 | u8 r = data->in_input[n]; |
222 | ||
223 | if (n == 0) | |
224 | return sprintf(buf, "%d\n", VDD_FROM_REG(r)); | |
225 | else | |
226 | return sprintf(buf, "%d\n", IN_FROM_REG(r)); | |
227 | } | |
228 | ||
86d47f12 JD |
229 | static ssize_t get_in_min(struct device *dev, struct device_attribute *attr, |
230 | char *buf) | |
1da177e4 | 231 | { |
86d47f12 JD |
232 | int n = to_sensor_dev_attr(attr)->index; |
233 | struct gl520_data *data = gl520_update_device(dev); | |
1da177e4 LT |
234 | u8 r = data->in_min[n]; |
235 | ||
236 | if (n == 0) | |
237 | return sprintf(buf, "%d\n", VDD_FROM_REG(r)); | |
238 | else | |
239 | return sprintf(buf, "%d\n", IN_FROM_REG(r)); | |
240 | } | |
241 | ||
86d47f12 JD |
242 | static ssize_t get_in_max(struct device *dev, struct device_attribute *attr, |
243 | char *buf) | |
1da177e4 | 244 | { |
86d47f12 JD |
245 | int n = to_sensor_dev_attr(attr)->index; |
246 | struct gl520_data *data = gl520_update_device(dev); | |
1da177e4 LT |
247 | u8 r = data->in_max[n]; |
248 | ||
249 | if (n == 0) | |
250 | return sprintf(buf, "%d\n", VDD_FROM_REG(r)); | |
251 | else | |
252 | return sprintf(buf, "%d\n", IN_FROM_REG(r)); | |
253 | } | |
254 | ||
86d47f12 JD |
255 | static ssize_t set_in_min(struct device *dev, struct device_attribute *attr, |
256 | const char *buf, size_t count) | |
1da177e4 | 257 | { |
86d47f12 JD |
258 | struct i2c_client *client = to_i2c_client(dev); |
259 | struct gl520_data *data = i2c_get_clientdata(client); | |
260 | int n = to_sensor_dev_attr(attr)->index; | |
1da177e4 | 261 | u8 r; |
43da3d12 GR |
262 | long v; |
263 | int err; | |
264 | ||
265 | err = kstrtol(buf, 10, &v); | |
266 | if (err) | |
267 | return err; | |
1da177e4 | 268 | |
9a61bf63 | 269 | mutex_lock(&data->update_lock); |
1da177e4 LT |
270 | |
271 | if (n == 0) | |
272 | r = VDD_TO_REG(v); | |
273 | else | |
274 | r = IN_TO_REG(v); | |
275 | ||
276 | data->in_min[n] = r; | |
277 | ||
278 | if (n < 4) | |
86d47f12 JD |
279 | gl520_write_value(client, GL520_REG_IN_MIN[n], |
280 | (gl520_read_value(client, GL520_REG_IN_MIN[n]) | |
281 | & ~0xff) | r); | |
1da177e4 | 282 | else |
86d47f12 | 283 | gl520_write_value(client, GL520_REG_IN_MIN[n], r); |
1da177e4 | 284 | |
9a61bf63 | 285 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
286 | return count; |
287 | } | |
288 | ||
86d47f12 JD |
289 | static ssize_t set_in_max(struct device *dev, struct device_attribute *attr, |
290 | const char *buf, size_t count) | |
1da177e4 | 291 | { |
86d47f12 JD |
292 | struct i2c_client *client = to_i2c_client(dev); |
293 | struct gl520_data *data = i2c_get_clientdata(client); | |
294 | int n = to_sensor_dev_attr(attr)->index; | |
1da177e4 | 295 | u8 r; |
43da3d12 GR |
296 | long v; |
297 | int err; | |
298 | ||
299 | err = kstrtol(buf, 10, &v); | |
300 | if (err) | |
301 | return err; | |
1da177e4 LT |
302 | |
303 | if (n == 0) | |
304 | r = VDD_TO_REG(v); | |
305 | else | |
306 | r = IN_TO_REG(v); | |
307 | ||
9a61bf63 | 308 | mutex_lock(&data->update_lock); |
1da177e4 LT |
309 | |
310 | data->in_max[n] = r; | |
311 | ||
312 | if (n < 4) | |
86d47f12 JD |
313 | gl520_write_value(client, GL520_REG_IN_MAX[n], |
314 | (gl520_read_value(client, GL520_REG_IN_MAX[n]) | |
315 | & ~0xff00) | (r << 8)); | |
1da177e4 | 316 | else |
86d47f12 | 317 | gl520_write_value(client, GL520_REG_IN_MAX[n], r); |
1da177e4 | 318 | |
9a61bf63 | 319 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
320 | return count; |
321 | } | |
322 | ||
86d47f12 JD |
323 | static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, get_in_input, NULL, 0); |
324 | static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, get_in_input, NULL, 1); | |
325 | static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, get_in_input, NULL, 2); | |
326 | static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, get_in_input, NULL, 3); | |
327 | static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, get_in_input, NULL, 4); | |
328 | static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR, | |
329 | get_in_min, set_in_min, 0); | |
330 | static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR, | |
331 | get_in_min, set_in_min, 1); | |
332 | static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO | S_IWUSR, | |
333 | get_in_min, set_in_min, 2); | |
334 | static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO | S_IWUSR, | |
335 | get_in_min, set_in_min, 3); | |
336 | static SENSOR_DEVICE_ATTR(in4_min, S_IRUGO | S_IWUSR, | |
337 | get_in_min, set_in_min, 4); | |
338 | static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR, | |
339 | get_in_max, set_in_max, 0); | |
340 | static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR, | |
341 | get_in_max, set_in_max, 1); | |
342 | static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO | S_IWUSR, | |
343 | get_in_max, set_in_max, 2); | |
344 | static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO | S_IWUSR, | |
345 | get_in_max, set_in_max, 3); | |
346 | static SENSOR_DEVICE_ATTR(in4_max, S_IRUGO | S_IWUSR, | |
347 | get_in_max, set_in_max, 4); | |
348 | ||
1da177e4 | 349 | #define DIV_FROM_REG(val) (1 << (val)) |
43da3d12 GR |
350 | #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : (480000 / ((val) << (div)))) |
351 | #define FAN_TO_REG(val, div) ((val) <= 0 ? 0 : \ | |
2a844c14 | 352 | clamp_val((480000 + ((val) << ((div)-1))) / ((val) << (div)), 1, 255)) |
1da177e4 | 353 | |
86d47f12 JD |
354 | static ssize_t get_fan_input(struct device *dev, struct device_attribute *attr, |
355 | char *buf) | |
1da177e4 | 356 | { |
86d47f12 JD |
357 | int n = to_sensor_dev_attr(attr)->index; |
358 | struct gl520_data *data = gl520_update_device(dev); | |
359 | ||
360 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_input[n], | |
361 | data->fan_div[n])); | |
1da177e4 LT |
362 | } |
363 | ||
86d47f12 JD |
364 | static ssize_t get_fan_min(struct device *dev, struct device_attribute *attr, |
365 | char *buf) | |
1da177e4 | 366 | { |
86d47f12 JD |
367 | int n = to_sensor_dev_attr(attr)->index; |
368 | struct gl520_data *data = gl520_update_device(dev); | |
369 | ||
370 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[n], | |
371 | data->fan_div[n])); | |
1da177e4 LT |
372 | } |
373 | ||
86d47f12 JD |
374 | static ssize_t get_fan_div(struct device *dev, struct device_attribute *attr, |
375 | char *buf) | |
1da177e4 | 376 | { |
86d47f12 JD |
377 | int n = to_sensor_dev_attr(attr)->index; |
378 | struct gl520_data *data = gl520_update_device(dev); | |
379 | ||
380 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[n])); | |
1da177e4 LT |
381 | } |
382 | ||
86d47f12 JD |
383 | static ssize_t get_fan_off(struct device *dev, struct device_attribute *attr, |
384 | char *buf) | |
1da177e4 | 385 | { |
86d47f12 | 386 | struct gl520_data *data = gl520_update_device(dev); |
1da177e4 LT |
387 | return sprintf(buf, "%d\n", data->fan_off); |
388 | } | |
389 | ||
86d47f12 JD |
390 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, |
391 | const char *buf, size_t count) | |
1da177e4 | 392 | { |
86d47f12 JD |
393 | struct i2c_client *client = to_i2c_client(dev); |
394 | struct gl520_data *data = i2c_get_clientdata(client); | |
395 | int n = to_sensor_dev_attr(attr)->index; | |
1da177e4 | 396 | u8 r; |
43da3d12 GR |
397 | unsigned long v; |
398 | int err; | |
399 | ||
400 | err = kstrtoul(buf, 10, &v); | |
401 | if (err) | |
402 | return err; | |
1da177e4 | 403 | |
9a61bf63 | 404 | mutex_lock(&data->update_lock); |
86d47f12 JD |
405 | r = FAN_TO_REG(v, data->fan_div[n]); |
406 | data->fan_min[n] = r; | |
1da177e4 | 407 | |
86d47f12 JD |
408 | if (n == 0) |
409 | gl520_write_value(client, GL520_REG_FAN_MIN, | |
410 | (gl520_read_value(client, GL520_REG_FAN_MIN) | |
411 | & ~0xff00) | (r << 8)); | |
1da177e4 | 412 | else |
86d47f12 JD |
413 | gl520_write_value(client, GL520_REG_FAN_MIN, |
414 | (gl520_read_value(client, GL520_REG_FAN_MIN) | |
415 | & ~0xff) | r); | |
1da177e4 LT |
416 | |
417 | data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK); | |
86d47f12 JD |
418 | if (data->fan_min[n] == 0) |
419 | data->alarm_mask &= (n == 0) ? ~0x20 : ~0x40; | |
1da177e4 | 420 | else |
86d47f12 | 421 | data->alarm_mask |= (n == 0) ? 0x20 : 0x40; |
1da177e4 LT |
422 | data->beep_mask &= data->alarm_mask; |
423 | gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask); | |
424 | ||
9a61bf63 | 425 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
426 | return count; |
427 | } | |
428 | ||
86d47f12 JD |
429 | static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr, |
430 | const char *buf, size_t count) | |
1da177e4 | 431 | { |
86d47f12 JD |
432 | struct i2c_client *client = to_i2c_client(dev); |
433 | struct gl520_data *data = i2c_get_clientdata(client); | |
434 | int n = to_sensor_dev_attr(attr)->index; | |
1da177e4 | 435 | u8 r; |
43da3d12 GR |
436 | unsigned long v; |
437 | int err; | |
438 | ||
439 | err = kstrtoul(buf, 10, &v); | |
440 | if (err) | |
441 | return err; | |
1da177e4 LT |
442 | |
443 | switch (v) { | |
43da3d12 GR |
444 | case 1: |
445 | r = 0; | |
446 | break; | |
447 | case 2: | |
448 | r = 1; | |
449 | break; | |
450 | case 4: | |
451 | r = 2; | |
452 | break; | |
453 | case 8: | |
454 | r = 3; | |
455 | break; | |
1da177e4 | 456 | default: |
43da3d12 GR |
457 | dev_err(&client->dev, |
458 | "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", v); | |
1da177e4 LT |
459 | return -EINVAL; |
460 | } | |
461 | ||
9a61bf63 | 462 | mutex_lock(&data->update_lock); |
86d47f12 | 463 | data->fan_div[n] = r; |
1da177e4 | 464 | |
86d47f12 JD |
465 | if (n == 0) |
466 | gl520_write_value(client, GL520_REG_FAN_DIV, | |
467 | (gl520_read_value(client, GL520_REG_FAN_DIV) | |
468 | & ~0xc0) | (r << 6)); | |
1da177e4 | 469 | else |
86d47f12 JD |
470 | gl520_write_value(client, GL520_REG_FAN_DIV, |
471 | (gl520_read_value(client, GL520_REG_FAN_DIV) | |
472 | & ~0x30) | (r << 4)); | |
1da177e4 | 473 | |
9a61bf63 | 474 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
475 | return count; |
476 | } | |
477 | ||
86d47f12 JD |
478 | static ssize_t set_fan_off(struct device *dev, struct device_attribute *attr, |
479 | const char *buf, size_t count) | |
1da177e4 | 480 | { |
86d47f12 JD |
481 | struct i2c_client *client = to_i2c_client(dev); |
482 | struct gl520_data *data = i2c_get_clientdata(client); | |
43da3d12 GR |
483 | u8 r; |
484 | unsigned long v; | |
485 | int err; | |
486 | ||
487 | err = kstrtoul(buf, 10, &v); | |
488 | if (err) | |
489 | return err; | |
490 | ||
491 | r = (v ? 1 : 0); | |
1da177e4 | 492 | |
9a61bf63 | 493 | mutex_lock(&data->update_lock); |
1da177e4 | 494 | data->fan_off = r; |
86d47f12 JD |
495 | gl520_write_value(client, GL520_REG_FAN_OFF, |
496 | (gl520_read_value(client, GL520_REG_FAN_OFF) | |
497 | & ~0x0c) | (r << 2)); | |
9a61bf63 | 498 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
499 | return count; |
500 | } | |
501 | ||
86d47f12 JD |
502 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan_input, NULL, 0); |
503 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, get_fan_input, NULL, 1); | |
504 | static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR, | |
505 | get_fan_min, set_fan_min, 0); | |
506 | static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR, | |
507 | get_fan_min, set_fan_min, 1); | |
508 | static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, | |
509 | get_fan_div, set_fan_div, 0); | |
510 | static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, | |
511 | get_fan_div, set_fan_div, 1); | |
512 | static DEVICE_ATTR(fan1_off, S_IRUGO | S_IWUSR, | |
513 | get_fan_off, set_fan_off); | |
514 | ||
1da177e4 | 515 | #define TEMP_FROM_REG(val) (((val) - 130) * 1000) |
2a844c14 | 516 | #define TEMP_TO_REG(val) clamp_val(((((val) < 0 ? \ |
43da3d12 | 517 | (val) - 500 : (val) + 500) / 1000) + 130), 0, 255) |
1da177e4 | 518 | |
86d47f12 JD |
519 | static ssize_t get_temp_input(struct device *dev, struct device_attribute *attr, |
520 | char *buf) | |
1da177e4 | 521 | { |
86d47f12 JD |
522 | int n = to_sensor_dev_attr(attr)->index; |
523 | struct gl520_data *data = gl520_update_device(dev); | |
524 | ||
525 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_input[n])); | |
1da177e4 LT |
526 | } |
527 | ||
86d47f12 JD |
528 | static ssize_t get_temp_max(struct device *dev, struct device_attribute *attr, |
529 | char *buf) | |
1da177e4 | 530 | { |
86d47f12 JD |
531 | int n = to_sensor_dev_attr(attr)->index; |
532 | struct gl520_data *data = gl520_update_device(dev); | |
533 | ||
534 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[n])); | |
1da177e4 LT |
535 | } |
536 | ||
43da3d12 GR |
537 | static ssize_t get_temp_max_hyst(struct device *dev, |
538 | struct device_attribute *attr, char *buf) | |
1da177e4 | 539 | { |
86d47f12 JD |
540 | int n = to_sensor_dev_attr(attr)->index; |
541 | struct gl520_data *data = gl520_update_device(dev); | |
542 | ||
543 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[n])); | |
1da177e4 LT |
544 | } |
545 | ||
86d47f12 JD |
546 | static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, |
547 | const char *buf, size_t count) | |
1da177e4 | 548 | { |
86d47f12 JD |
549 | struct i2c_client *client = to_i2c_client(dev); |
550 | struct gl520_data *data = i2c_get_clientdata(client); | |
551 | int n = to_sensor_dev_attr(attr)->index; | |
43da3d12 GR |
552 | long v; |
553 | int err; | |
554 | ||
555 | err = kstrtol(buf, 10, &v); | |
556 | if (err) | |
557 | return err; | |
1da177e4 | 558 | |
9a61bf63 | 559 | mutex_lock(&data->update_lock); |
86d47f12 JD |
560 | data->temp_max[n] = TEMP_TO_REG(v); |
561 | gl520_write_value(client, GL520_REG_TEMP_MAX[n], data->temp_max[n]); | |
9a61bf63 | 562 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
563 | return count; |
564 | } | |
565 | ||
86d47f12 JD |
566 | static ssize_t set_temp_max_hyst(struct device *dev, struct device_attribute |
567 | *attr, const char *buf, size_t count) | |
1da177e4 | 568 | { |
86d47f12 JD |
569 | struct i2c_client *client = to_i2c_client(dev); |
570 | struct gl520_data *data = i2c_get_clientdata(client); | |
571 | int n = to_sensor_dev_attr(attr)->index; | |
43da3d12 GR |
572 | long v; |
573 | int err; | |
574 | ||
575 | err = kstrtol(buf, 10, &v); | |
576 | if (err) | |
577 | return err; | |
1da177e4 | 578 | |
9a61bf63 | 579 | mutex_lock(&data->update_lock); |
86d47f12 JD |
580 | data->temp_max_hyst[n] = TEMP_TO_REG(v); |
581 | gl520_write_value(client, GL520_REG_TEMP_MAX_HYST[n], | |
582 | data->temp_max_hyst[n]); | |
9a61bf63 | 583 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
584 | return count; |
585 | } | |
586 | ||
86d47f12 JD |
587 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, get_temp_input, NULL, 0); |
588 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, get_temp_input, NULL, 1); | |
589 | static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, | |
590 | get_temp_max, set_temp_max, 0); | |
591 | static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR, | |
592 | get_temp_max, set_temp_max, 1); | |
593 | static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, | |
594 | get_temp_max_hyst, set_temp_max_hyst, 0); | |
595 | static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR, | |
596 | get_temp_max_hyst, set_temp_max_hyst, 1); | |
597 | ||
598 | static ssize_t get_alarms(struct device *dev, struct device_attribute *attr, | |
599 | char *buf) | |
1da177e4 | 600 | { |
86d47f12 | 601 | struct gl520_data *data = gl520_update_device(dev); |
1da177e4 LT |
602 | return sprintf(buf, "%d\n", data->alarms); |
603 | } | |
604 | ||
86d47f12 JD |
605 | static ssize_t get_beep_enable(struct device *dev, struct device_attribute |
606 | *attr, char *buf) | |
1da177e4 | 607 | { |
86d47f12 | 608 | struct gl520_data *data = gl520_update_device(dev); |
1da177e4 LT |
609 | return sprintf(buf, "%d\n", data->beep_enable); |
610 | } | |
611 | ||
86d47f12 JD |
612 | static ssize_t get_beep_mask(struct device *dev, struct device_attribute *attr, |
613 | char *buf) | |
1da177e4 | 614 | { |
86d47f12 | 615 | struct gl520_data *data = gl520_update_device(dev); |
1da177e4 LT |
616 | return sprintf(buf, "%d\n", data->beep_mask); |
617 | } | |
618 | ||
86d47f12 JD |
619 | static ssize_t set_beep_enable(struct device *dev, struct device_attribute |
620 | *attr, const char *buf, size_t count) | |
1da177e4 | 621 | { |
86d47f12 JD |
622 | struct i2c_client *client = to_i2c_client(dev); |
623 | struct gl520_data *data = i2c_get_clientdata(client); | |
43da3d12 GR |
624 | u8 r; |
625 | unsigned long v; | |
626 | int err; | |
627 | ||
628 | err = kstrtoul(buf, 10, &v); | |
629 | if (err) | |
630 | return err; | |
631 | ||
632 | r = (v ? 0 : 1); | |
1da177e4 | 633 | |
9a61bf63 | 634 | mutex_lock(&data->update_lock); |
1da177e4 | 635 | data->beep_enable = !r; |
86d47f12 JD |
636 | gl520_write_value(client, GL520_REG_BEEP_ENABLE, |
637 | (gl520_read_value(client, GL520_REG_BEEP_ENABLE) | |
638 | & ~0x04) | (r << 2)); | |
9a61bf63 | 639 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
640 | return count; |
641 | } | |
642 | ||
86d47f12 JD |
643 | static ssize_t set_beep_mask(struct device *dev, struct device_attribute *attr, |
644 | const char *buf, size_t count) | |
1da177e4 | 645 | { |
86d47f12 JD |
646 | struct i2c_client *client = to_i2c_client(dev); |
647 | struct gl520_data *data = i2c_get_clientdata(client); | |
43da3d12 GR |
648 | unsigned long r; |
649 | int err; | |
650 | ||
651 | err = kstrtoul(buf, 10, &r); | |
652 | if (err) | |
653 | return err; | |
f28dc2f7 | 654 | |
9a61bf63 | 655 | mutex_lock(&data->update_lock); |
1da177e4 LT |
656 | r &= data->alarm_mask; |
657 | data->beep_mask = r; | |
86d47f12 | 658 | gl520_write_value(client, GL520_REG_BEEP_MASK, r); |
9a61bf63 | 659 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
660 | return count; |
661 | } | |
662 | ||
86d47f12 JD |
663 | static DEVICE_ATTR(alarms, S_IRUGO, get_alarms, NULL); |
664 | static DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR, | |
665 | get_beep_enable, set_beep_enable); | |
666 | static DEVICE_ATTR(beep_mask, S_IRUGO | S_IWUSR, | |
667 | get_beep_mask, set_beep_mask); | |
668 | ||
e86a7760 JD |
669 | static ssize_t get_alarm(struct device *dev, struct device_attribute *attr, |
670 | char *buf) | |
671 | { | |
672 | int bit_nr = to_sensor_dev_attr(attr)->index; | |
673 | struct gl520_data *data = gl520_update_device(dev); | |
674 | ||
675 | return sprintf(buf, "%d\n", (data->alarms >> bit_nr) & 1); | |
676 | } | |
677 | ||
678 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, get_alarm, NULL, 0); | |
679 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, get_alarm, NULL, 1); | |
680 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, get_alarm, NULL, 2); | |
681 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, get_alarm, NULL, 3); | |
682 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, get_alarm, NULL, 4); | |
683 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, get_alarm, NULL, 5); | |
684 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, get_alarm, NULL, 6); | |
685 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, get_alarm, NULL, 7); | |
686 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, get_alarm, NULL, 7); | |
687 | ||
688 | static ssize_t get_beep(struct device *dev, struct device_attribute *attr, | |
689 | char *buf) | |
690 | { | |
691 | int bitnr = to_sensor_dev_attr(attr)->index; | |
692 | struct gl520_data *data = gl520_update_device(dev); | |
693 | ||
694 | return sprintf(buf, "%d\n", (data->beep_mask >> bitnr) & 1); | |
695 | } | |
696 | ||
697 | static ssize_t set_beep(struct device *dev, struct device_attribute *attr, | |
698 | const char *buf, size_t count) | |
699 | { | |
700 | struct i2c_client *client = to_i2c_client(dev); | |
701 | struct gl520_data *data = i2c_get_clientdata(client); | |
702 | int bitnr = to_sensor_dev_attr(attr)->index; | |
703 | unsigned long bit; | |
704 | ||
43da3d12 GR |
705 | int err; |
706 | ||
707 | err = kstrtoul(buf, 10, &bit); | |
708 | if (err) | |
709 | return err; | |
e86a7760 JD |
710 | if (bit & ~1) |
711 | return -EINVAL; | |
712 | ||
713 | mutex_lock(&data->update_lock); | |
714 | data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK); | |
715 | if (bit) | |
716 | data->beep_mask |= (1 << bitnr); | |
717 | else | |
718 | data->beep_mask &= ~(1 << bitnr); | |
719 | gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask); | |
720 | mutex_unlock(&data->update_lock); | |
721 | return count; | |
722 | } | |
723 | ||
724 | static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 0); | |
725 | static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 1); | |
726 | static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 2); | |
727 | static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 3); | |
728 | static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 4); | |
729 | static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 5); | |
730 | static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 6); | |
731 | static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 7); | |
732 | static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 7); | |
733 | ||
87808be4 JD |
734 | static struct attribute *gl520_attributes[] = { |
735 | &dev_attr_cpu0_vid.attr, | |
736 | ||
86d47f12 JD |
737 | &sensor_dev_attr_in0_input.dev_attr.attr, |
738 | &sensor_dev_attr_in0_min.dev_attr.attr, | |
739 | &sensor_dev_attr_in0_max.dev_attr.attr, | |
e86a7760 JD |
740 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
741 | &sensor_dev_attr_in0_beep.dev_attr.attr, | |
86d47f12 JD |
742 | &sensor_dev_attr_in1_input.dev_attr.attr, |
743 | &sensor_dev_attr_in1_min.dev_attr.attr, | |
744 | &sensor_dev_attr_in1_max.dev_attr.attr, | |
e86a7760 JD |
745 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
746 | &sensor_dev_attr_in1_beep.dev_attr.attr, | |
86d47f12 JD |
747 | &sensor_dev_attr_in2_input.dev_attr.attr, |
748 | &sensor_dev_attr_in2_min.dev_attr.attr, | |
749 | &sensor_dev_attr_in2_max.dev_attr.attr, | |
e86a7760 JD |
750 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
751 | &sensor_dev_attr_in2_beep.dev_attr.attr, | |
86d47f12 JD |
752 | &sensor_dev_attr_in3_input.dev_attr.attr, |
753 | &sensor_dev_attr_in3_min.dev_attr.attr, | |
754 | &sensor_dev_attr_in3_max.dev_attr.attr, | |
e86a7760 JD |
755 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
756 | &sensor_dev_attr_in3_beep.dev_attr.attr, | |
86d47f12 JD |
757 | |
758 | &sensor_dev_attr_fan1_input.dev_attr.attr, | |
759 | &sensor_dev_attr_fan1_min.dev_attr.attr, | |
760 | &sensor_dev_attr_fan1_div.dev_attr.attr, | |
e86a7760 JD |
761 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
762 | &sensor_dev_attr_fan1_beep.dev_attr.attr, | |
87808be4 | 763 | &dev_attr_fan1_off.attr, |
86d47f12 JD |
764 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
765 | &sensor_dev_attr_fan2_min.dev_attr.attr, | |
766 | &sensor_dev_attr_fan2_div.dev_attr.attr, | |
e86a7760 JD |
767 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
768 | &sensor_dev_attr_fan2_beep.dev_attr.attr, | |
87808be4 | 769 | |
86d47f12 JD |
770 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
771 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
772 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, | |
e86a7760 JD |
773 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
774 | &sensor_dev_attr_temp1_beep.dev_attr.attr, | |
87808be4 JD |
775 | |
776 | &dev_attr_alarms.attr, | |
777 | &dev_attr_beep_enable.attr, | |
778 | &dev_attr_beep_mask.attr, | |
779 | NULL | |
780 | }; | |
781 | ||
782 | static const struct attribute_group gl520_group = { | |
783 | .attrs = gl520_attributes, | |
784 | }; | |
785 | ||
f445a9af | 786 | static struct attribute *gl520_attributes_in4[] = { |
86d47f12 JD |
787 | &sensor_dev_attr_in4_input.dev_attr.attr, |
788 | &sensor_dev_attr_in4_min.dev_attr.attr, | |
789 | &sensor_dev_attr_in4_max.dev_attr.attr, | |
e86a7760 JD |
790 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
791 | &sensor_dev_attr_in4_beep.dev_attr.attr, | |
f445a9af GR |
792 | NULL |
793 | }; | |
87808be4 | 794 | |
f445a9af | 795 | static struct attribute *gl520_attributes_temp2[] = { |
86d47f12 JD |
796 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
797 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
798 | &sensor_dev_attr_temp2_max_hyst.dev_attr.attr, | |
e86a7760 JD |
799 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
800 | &sensor_dev_attr_temp2_beep.dev_attr.attr, | |
87808be4 JD |
801 | NULL |
802 | }; | |
803 | ||
f445a9af GR |
804 | static const struct attribute_group gl520_group_in4 = { |
805 | .attrs = gl520_attributes_in4, | |
806 | }; | |
807 | ||
808 | static const struct attribute_group gl520_group_temp2 = { | |
809 | .attrs = gl520_attributes_temp2, | |
87808be4 JD |
810 | }; |
811 | ||
1da177e4 LT |
812 | |
813 | /* | |
814 | * Real code | |
815 | */ | |
816 | ||
a23a9fe1 | 817 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
310ec792 | 818 | static int gl520_detect(struct i2c_client *client, struct i2c_board_info *info) |
1da177e4 | 819 | { |
a23a9fe1 | 820 | struct i2c_adapter *adapter = client->adapter; |
1da177e4 LT |
821 | |
822 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | | |
823 | I2C_FUNC_SMBUS_WORD_DATA)) | |
a23a9fe1 | 824 | return -ENODEV; |
1da177e4 LT |
825 | |
826 | /* Determine the chip type. */ | |
52df6440 JD |
827 | if ((gl520_read_value(client, GL520_REG_CHIP_ID) != 0x20) || |
828 | ((gl520_read_value(client, GL520_REG_REVISION) & 0x7f) != 0x00) || | |
829 | ((gl520_read_value(client, GL520_REG_CONF) & 0x80) != 0x00)) { | |
830 | dev_dbg(&client->dev, "Unknown chip type, skipping\n"); | |
831 | return -ENODEV; | |
1da177e4 LT |
832 | } |
833 | ||
a23a9fe1 | 834 | strlcpy(info->type, "gl520sm", I2C_NAME_SIZE); |
1da177e4 | 835 | |
a23a9fe1 JD |
836 | return 0; |
837 | } | |
838 | ||
df51f2d7 AL |
839 | /* Called when we have found a new GL520SM. */ |
840 | static void gl520_init_client(struct i2c_client *client) | |
841 | { | |
842 | struct gl520_data *data = i2c_get_clientdata(client); | |
843 | u8 oldconf, conf; | |
844 | ||
845 | conf = oldconf = gl520_read_value(client, GL520_REG_CONF); | |
846 | ||
847 | data->alarm_mask = 0xff; | |
848 | data->vrm = vid_which_vrm(); | |
849 | ||
850 | if (extra_sensor_type == 1) | |
851 | conf &= ~0x10; | |
852 | else if (extra_sensor_type == 2) | |
853 | conf |= 0x10; | |
854 | data->two_temps = !(conf & 0x10); | |
855 | ||
856 | /* If IRQ# is disabled, we can safely force comparator mode */ | |
857 | if (!(conf & 0x20)) | |
858 | conf &= 0xf7; | |
859 | ||
860 | /* Enable monitoring if needed */ | |
861 | conf |= 0x40; | |
862 | ||
863 | if (conf != oldconf) | |
864 | gl520_write_value(client, GL520_REG_CONF, conf); | |
865 | ||
866 | gl520_update_device(&(client->dev)); | |
867 | ||
868 | if (data->fan_min[0] == 0) | |
869 | data->alarm_mask &= ~0x20; | |
870 | if (data->fan_min[1] == 0) | |
871 | data->alarm_mask &= ~0x40; | |
872 | ||
873 | data->beep_mask &= data->alarm_mask; | |
874 | gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask); | |
875 | } | |
876 | ||
a23a9fe1 JD |
877 | static int gl520_probe(struct i2c_client *client, |
878 | const struct i2c_device_id *id) | |
879 | { | |
880 | struct gl520_data *data; | |
881 | int err; | |
882 | ||
647ff514 GR |
883 | data = devm_kzalloc(&client->dev, sizeof(struct gl520_data), |
884 | GFP_KERNEL); | |
885 | if (!data) | |
886 | return -ENOMEM; | |
a23a9fe1 JD |
887 | |
888 | i2c_set_clientdata(client, data); | |
889 | mutex_init(&data->update_lock); | |
1da177e4 LT |
890 | |
891 | /* Initialize the GL520SM chip */ | |
f28dc2f7 | 892 | gl520_init_client(client); |
1da177e4 LT |
893 | |
894 | /* Register sysfs hooks */ | |
43da3d12 GR |
895 | err = sysfs_create_group(&client->dev.kobj, &gl520_group); |
896 | if (err) | |
647ff514 | 897 | return err; |
1da177e4 | 898 | |
f445a9af GR |
899 | if (data->two_temps) |
900 | err = sysfs_create_group(&client->dev.kobj, &gl520_group_temp2); | |
901 | else | |
902 | err = sysfs_create_group(&client->dev.kobj, &gl520_group_in4); | |
1da177e4 | 903 | |
f445a9af GR |
904 | if (err) |
905 | goto exit_remove_files; | |
1da177e4 | 906 | |
f28dc2f7 | 907 | data->hwmon_dev = hwmon_device_register(&client->dev); |
1beeffe4 TJ |
908 | if (IS_ERR(data->hwmon_dev)) { |
909 | err = PTR_ERR(data->hwmon_dev); | |
87808be4 JD |
910 | goto exit_remove_files; |
911 | } | |
1da177e4 LT |
912 | |
913 | return 0; | |
914 | ||
87808be4 | 915 | exit_remove_files: |
f28dc2f7 | 916 | sysfs_remove_group(&client->dev.kobj, &gl520_group); |
f445a9af GR |
917 | sysfs_remove_group(&client->dev.kobj, &gl520_group_in4); |
918 | sysfs_remove_group(&client->dev.kobj, &gl520_group_temp2); | |
1da177e4 LT |
919 | return err; |
920 | } | |
921 | ||
a23a9fe1 | 922 | static int gl520_remove(struct i2c_client *client) |
1da177e4 | 923 | { |
943b0830 | 924 | struct gl520_data *data = i2c_get_clientdata(client); |
1da177e4 | 925 | |
1beeffe4 | 926 | hwmon_device_unregister(data->hwmon_dev); |
87808be4 | 927 | sysfs_remove_group(&client->dev.kobj, &gl520_group); |
f445a9af GR |
928 | sysfs_remove_group(&client->dev.kobj, &gl520_group_in4); |
929 | sysfs_remove_group(&client->dev.kobj, &gl520_group_temp2); | |
943b0830 | 930 | |
1da177e4 LT |
931 | return 0; |
932 | } | |
933 | ||
df51f2d7 AL |
934 | static const struct i2c_device_id gl520_id[] = { |
935 | { "gl520sm", 0 }, | |
936 | { } | |
937 | }; | |
938 | MODULE_DEVICE_TABLE(i2c, gl520_id); | |
1da177e4 | 939 | |
df51f2d7 AL |
940 | static struct i2c_driver gl520_driver = { |
941 | .class = I2C_CLASS_HWMON, | |
942 | .driver = { | |
943 | .name = "gl520sm", | |
944 | }, | |
945 | .probe = gl520_probe, | |
946 | .remove = gl520_remove, | |
947 | .id_table = gl520_id, | |
948 | .detect = gl520_detect, | |
949 | .address_list = normal_i2c, | |
950 | }; | |
1da177e4 | 951 | |
f0967eea | 952 | module_i2c_driver(gl520_driver); |
1da177e4 LT |
953 | |
954 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, " | |
96de0e25 | 955 | "Kyösti Mälkki <kmalkki@cc.hut.fi>, " |
1da177e4 LT |
956 | "Maarten Deprez <maartendeprez@users.sourceforge.net>"); |
957 | MODULE_DESCRIPTION("GL520SM driver"); | |
958 | MODULE_LICENSE("GPL"); |