diff options
Diffstat (limited to 'drivers/hwmon/thmc50.c')
-rw-r--r-- | drivers/hwmon/thmc50.c | 440 |
1 files changed, 440 insertions, 0 deletions
diff --git a/drivers/hwmon/thmc50.c b/drivers/hwmon/thmc50.c new file mode 100644 index 000000000000..9395b52d9b99 --- /dev/null +++ b/drivers/hwmon/thmc50.c | |||
@@ -0,0 +1,440 @@ | |||
1 | /* | ||
2 | thmc50.c - Part of lm_sensors, Linux kernel modules for hardware | ||
3 | monitoring | ||
4 | Copyright (C) 2007 Krzysztof Helt <krzysztof.h1@wp.pl> | ||
5 | Based on 2.4 driver by Frodo Looijaard <frodol@dds.nl> and | ||
6 | Philip Edelbrock <phil@netroedge.com> | ||
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 | |||
23 | #include <linux/module.h> | ||
24 | #include <linux/init.h> | ||
25 | #include <linux/slab.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 | MODULE_LICENSE("GPL"); | ||
33 | |||
34 | /* Addresses to scan */ | ||
35 | static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; | ||
36 | |||
37 | /* Insmod parameters */ | ||
38 | I2C_CLIENT_INSMOD_2(thmc50, adm1022); | ||
39 | I2C_CLIENT_MODULE_PARM(adm1022_temp3, "List of adapter,address pairs " | ||
40 | "to enable 3rd temperature (ADM1022 only)"); | ||
41 | |||
42 | /* Many THMC50 constants specified below */ | ||
43 | |||
44 | /* The THMC50 registers */ | ||
45 | #define THMC50_REG_CONF 0x40 | ||
46 | #define THMC50_REG_COMPANY_ID 0x3E | ||
47 | #define THMC50_REG_DIE_CODE 0x3F | ||
48 | #define THMC50_REG_ANALOG_OUT 0x19 | ||
49 | |||
50 | const static u8 THMC50_REG_TEMP[] = { 0x27, 0x26, 0x20 }; | ||
51 | const static u8 THMC50_REG_TEMP_MIN[] = { 0x3A, 0x38, 0x2C }; | ||
52 | const static u8 THMC50_REG_TEMP_MAX[] = { 0x39, 0x37, 0x2B }; | ||
53 | |||
54 | #define THMC50_REG_CONF_nFANOFF 0x20 | ||
55 | |||
56 | /* Each client has this additional data */ | ||
57 | struct thmc50_data { | ||
58 | struct i2c_client client; | ||
59 | struct class_device *class_dev; | ||
60 | |||
61 | struct mutex update_lock; | ||
62 | enum chips type; | ||
63 | unsigned long last_updated; /* In jiffies */ | ||
64 | char has_temp3; /* !=0 if it is ADM1022 in temp3 mode */ | ||
65 | char valid; /* !=0 if following fields are valid */ | ||
66 | |||
67 | /* Register values */ | ||
68 | s8 temp_input[3]; | ||
69 | s8 temp_max[3]; | ||
70 | s8 temp_min[3]; | ||
71 | u8 analog_out; | ||
72 | }; | ||
73 | |||
74 | static int thmc50_attach_adapter(struct i2c_adapter *adapter); | ||
75 | static int thmc50_detach_client(struct i2c_client *client); | ||
76 | static void thmc50_init_client(struct i2c_client *client); | ||
77 | static struct thmc50_data *thmc50_update_device(struct device *dev); | ||
78 | |||
79 | static struct i2c_driver thmc50_driver = { | ||
80 | .driver = { | ||
81 | .name = "thmc50", | ||
82 | }, | ||
83 | .attach_adapter = thmc50_attach_adapter, | ||
84 | .detach_client = thmc50_detach_client, | ||
85 | }; | ||
86 | |||
87 | static ssize_t show_analog_out(struct device *dev, | ||
88 | struct device_attribute *attr, char *buf) | ||
89 | { | ||
90 | struct thmc50_data *data = thmc50_update_device(dev); | ||
91 | return sprintf(buf, "%d\n", data->analog_out); | ||
92 | } | ||
93 | |||
94 | static ssize_t set_analog_out(struct device *dev, | ||
95 | struct device_attribute *attr, | ||
96 | const char *buf, size_t count) | ||
97 | { | ||
98 | struct i2c_client *client = to_i2c_client(dev); | ||
99 | struct thmc50_data *data = i2c_get_clientdata(client); | ||
100 | int tmp = simple_strtoul(buf, NULL, 10); | ||
101 | int config; | ||
102 | |||
103 | mutex_lock(&data->update_lock); | ||
104 | data->analog_out = SENSORS_LIMIT(tmp, 0, 255); | ||
105 | i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT, | ||
106 | data->analog_out); | ||
107 | |||
108 | config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF); | ||
109 | if (data->analog_out == 0) | ||
110 | config &= ~THMC50_REG_CONF_nFANOFF; | ||
111 | else | ||
112 | config |= THMC50_REG_CONF_nFANOFF; | ||
113 | i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config); | ||
114 | |||
115 | mutex_unlock(&data->update_lock); | ||
116 | return count; | ||
117 | } | ||
118 | |||
119 | /* There is only one PWM mode = DC */ | ||
120 | static ssize_t show_pwm_mode(struct device *dev, struct device_attribute *attr, | ||
121 | char *buf) | ||
122 | { | ||
123 | return sprintf(buf, "0\n"); | ||
124 | } | ||
125 | |||
126 | /* Temperatures */ | ||
127 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, | ||
128 | char *buf) | ||
129 | { | ||
130 | int nr = to_sensor_dev_attr(attr)->index; | ||
131 | struct thmc50_data *data = thmc50_update_device(dev); | ||
132 | return sprintf(buf, "%d\n", data->temp_input[nr] * 1000); | ||
133 | } | ||
134 | |||
135 | static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr, | ||
136 | char *buf) | ||
137 | { | ||
138 | int nr = to_sensor_dev_attr(attr)->index; | ||
139 | struct thmc50_data *data = thmc50_update_device(dev); | ||
140 | return sprintf(buf, "%d\n", data->temp_min[nr] * 1000); | ||
141 | } | ||
142 | |||
143 | static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr, | ||
144 | const char *buf, size_t count) | ||
145 | { | ||
146 | int nr = to_sensor_dev_attr(attr)->index; | ||
147 | struct i2c_client *client = to_i2c_client(dev); | ||
148 | struct thmc50_data *data = i2c_get_clientdata(client); | ||
149 | int val = simple_strtol(buf, NULL, 10); | ||
150 | |||
151 | mutex_lock(&data->update_lock); | ||
152 | data->temp_min[nr] = SENSORS_LIMIT(val / 1000, -128, 127); | ||
153 | i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MIN[nr], | ||
154 | data->temp_min[nr]); | ||
155 | mutex_unlock(&data->update_lock); | ||
156 | return count; | ||
157 | } | ||
158 | |||
159 | static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr, | ||
160 | char *buf) | ||
161 | { | ||
162 | int nr = to_sensor_dev_attr(attr)->index; | ||
163 | struct thmc50_data *data = thmc50_update_device(dev); | ||
164 | return sprintf(buf, "%d\n", data->temp_max[nr] * 1000); | ||
165 | } | ||
166 | |||
167 | static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, | ||
168 | const char *buf, size_t count) | ||
169 | { | ||
170 | int nr = to_sensor_dev_attr(attr)->index; | ||
171 | struct i2c_client *client = to_i2c_client(dev); | ||
172 | struct thmc50_data *data = i2c_get_clientdata(client); | ||
173 | int val = simple_strtol(buf, NULL, 10); | ||
174 | |||
175 | mutex_lock(&data->update_lock); | ||
176 | data->temp_max[nr] = SENSORS_LIMIT(val / 1000, -128, 127); | ||
177 | i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MAX[nr], | ||
178 | data->temp_max[nr]); | ||
179 | mutex_unlock(&data->update_lock); | ||
180 | return count; | ||
181 | } | ||
182 | |||
183 | #define temp_reg(offset) \ | ||
184 | static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \ | ||
185 | NULL, offset - 1); \ | ||
186 | static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \ | ||
187 | show_temp_min, set_temp_min, offset - 1); \ | ||
188 | static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ | ||
189 | show_temp_max, set_temp_max, offset - 1); | ||
190 | |||
191 | temp_reg(1); | ||
192 | temp_reg(2); | ||
193 | temp_reg(3); | ||
194 | |||
195 | static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_analog_out, | ||
196 | set_analog_out, 0); | ||
197 | static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL, 0); | ||
198 | |||
199 | static struct attribute *thmc50_attributes[] = { | ||
200 | &sensor_dev_attr_temp1_max.dev_attr.attr, | ||
201 | &sensor_dev_attr_temp1_min.dev_attr.attr, | ||
202 | &sensor_dev_attr_temp1_input.dev_attr.attr, | ||
203 | &sensor_dev_attr_temp2_max.dev_attr.attr, | ||
204 | &sensor_dev_attr_temp2_min.dev_attr.attr, | ||
205 | &sensor_dev_attr_temp2_input.dev_attr.attr, | ||
206 | &sensor_dev_attr_pwm1.dev_attr.attr, | ||
207 | &sensor_dev_attr_pwm1_mode.dev_attr.attr, | ||
208 | NULL | ||
209 | }; | ||
210 | |||
211 | static const struct attribute_group thmc50_group = { | ||
212 | .attrs = thmc50_attributes, | ||
213 | }; | ||
214 | |||
215 | /* for ADM1022 3rd temperature mode */ | ||
216 | static struct attribute *adm1022_attributes[] = { | ||
217 | &sensor_dev_attr_temp3_max.dev_attr.attr, | ||
218 | &sensor_dev_attr_temp3_min.dev_attr.attr, | ||
219 | &sensor_dev_attr_temp3_input.dev_attr.attr, | ||
220 | NULL | ||
221 | }; | ||
222 | |||
223 | static const struct attribute_group adm1022_group = { | ||
224 | .attrs = adm1022_attributes, | ||
225 | }; | ||
226 | |||
227 | static int thmc50_detect(struct i2c_adapter *adapter, int address, int kind) | ||
228 | { | ||
229 | unsigned company; | ||
230 | unsigned revision; | ||
231 | unsigned config; | ||
232 | struct i2c_client *client; | ||
233 | struct thmc50_data *data; | ||
234 | struct device *dev; | ||
235 | int err = 0; | ||
236 | const char *type_name = ""; | ||
237 | |||
238 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { | ||
239 | pr_debug("thmc50: detect failed, " | ||
240 | "smbus byte data not supported!\n"); | ||
241 | goto exit; | ||
242 | } | ||
243 | |||
244 | /* OK. For now, we presume we have a valid client. We now create the | ||
245 | client structure, even though we cannot fill it completely yet. | ||
246 | But it allows us to access thmc50 registers. */ | ||
247 | if (!(data = kzalloc(sizeof(struct thmc50_data), GFP_KERNEL))) { | ||
248 | pr_debug("thmc50: detect failed, kzalloc failed!\n"); | ||
249 | err = -ENOMEM; | ||
250 | goto exit; | ||
251 | } | ||
252 | |||
253 | client = &data->client; | ||
254 | i2c_set_clientdata(client, data); | ||
255 | client->addr = address; | ||
256 | client->adapter = adapter; | ||
257 | client->driver = &thmc50_driver; | ||
258 | dev = &client->dev; | ||
259 | |||
260 | pr_debug("thmc50: Probing for THMC50 at 0x%2X on bus %d\n", | ||
261 | client->addr, i2c_adapter_id(client->adapter)); | ||
262 | |||
263 | /* Now, we do the remaining detection. */ | ||
264 | company = i2c_smbus_read_byte_data(client, THMC50_REG_COMPANY_ID); | ||
265 | revision = i2c_smbus_read_byte_data(client, THMC50_REG_DIE_CODE); | ||
266 | config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF); | ||
267 | |||
268 | if (kind == 0) | ||
269 | kind = thmc50; | ||
270 | else if (kind < 0) { | ||
271 | err = -ENODEV; | ||
272 | if (revision >= 0xc0 && ((config & 0x10) == 0)) { | ||
273 | if (company == 0x49) { | ||
274 | kind = thmc50; | ||
275 | err = 0; | ||
276 | } else if (company == 0x41) { | ||
277 | kind = adm1022; | ||
278 | err = 0; | ||
279 | } | ||
280 | } | ||
281 | } | ||
282 | if (err == -ENODEV) { | ||
283 | pr_debug("thmc50: Detection of THMC50/ADM1022 failed\n"); | ||
284 | goto exit_free; | ||
285 | } | ||
286 | pr_debug("thmc50: Detected %s (version %x, revision %x)\n", | ||
287 | type_name, (revision >> 4) - 0xc, revision & 0xf); | ||
288 | data->type = kind; | ||
289 | |||
290 | if (kind == thmc50) | ||
291 | type_name = "thmc50"; | ||
292 | else if (kind == adm1022) { | ||
293 | int id = i2c_adapter_id(client->adapter); | ||
294 | int i; | ||
295 | |||
296 | type_name = "adm1022"; | ||
297 | data->has_temp3 = (config >> 7) & 1; /* config MSB */ | ||
298 | for (i = 0; i + 1 < adm1022_temp3_num; i += 2) | ||
299 | if (adm1022_temp3[i] == id && | ||
300 | adm1022_temp3[i + 1] == address) { | ||
301 | /* enable 2nd remote temp */ | ||
302 | data->has_temp3 = 1; | ||
303 | break; | ||
304 | } | ||
305 | } | ||
306 | |||
307 | /* Fill in the remaining client fields & put it into the global list */ | ||
308 | strlcpy(client->name, type_name, I2C_NAME_SIZE); | ||
309 | mutex_init(&data->update_lock); | ||
310 | |||
311 | /* Tell the I2C layer a new client has arrived */ | ||
312 | if ((err = i2c_attach_client(client))) | ||
313 | goto exit_free; | ||
314 | |||
315 | thmc50_init_client(client); | ||
316 | |||
317 | /* Register sysfs hooks */ | ||
318 | if ((err = sysfs_create_group(&client->dev.kobj, &thmc50_group))) | ||
319 | goto exit_detach; | ||
320 | |||
321 | /* Register ADM1022 sysfs hooks */ | ||
322 | if (data->type == adm1022) | ||
323 | if ((err = sysfs_create_group(&client->dev.kobj, | ||
324 | &adm1022_group))) | ||
325 | goto exit_remove_sysfs_thmc50; | ||
326 | |||
327 | /* Register a new directory entry with module sensors */ | ||
328 | data->class_dev = hwmon_device_register(&client->dev); | ||
329 | if (IS_ERR(data->class_dev)) { | ||
330 | err = PTR_ERR(data->class_dev); | ||
331 | goto exit_remove_sysfs; | ||
332 | } | ||
333 | |||
334 | return 0; | ||
335 | |||
336 | exit_remove_sysfs: | ||
337 | if (data->type == adm1022) | ||
338 | sysfs_remove_group(&client->dev.kobj, &adm1022_group); | ||
339 | exit_remove_sysfs_thmc50: | ||
340 | sysfs_remove_group(&client->dev.kobj, &thmc50_group); | ||
341 | exit_detach: | ||
342 | i2c_detach_client(client); | ||
343 | exit_free: | ||
344 | kfree(data); | ||
345 | exit: | ||
346 | return err; | ||
347 | } | ||
348 | |||
349 | static int thmc50_attach_adapter(struct i2c_adapter *adapter) | ||
350 | { | ||
351 | if (!(adapter->class & I2C_CLASS_HWMON)) | ||
352 | return 0; | ||
353 | return i2c_probe(adapter, &addr_data, thmc50_detect); | ||
354 | } | ||
355 | |||
356 | static int thmc50_detach_client(struct i2c_client *client) | ||
357 | { | ||
358 | struct thmc50_data *data = i2c_get_clientdata(client); | ||
359 | int err; | ||
360 | |||
361 | hwmon_device_unregister(data->class_dev); | ||
362 | sysfs_remove_group(&client->dev.kobj, &thmc50_group); | ||
363 | if (data->type == adm1022) | ||
364 | sysfs_remove_group(&client->dev.kobj, &adm1022_group); | ||
365 | |||
366 | if ((err = i2c_detach_client(client))) | ||
367 | return err; | ||
368 | |||
369 | kfree(data); | ||
370 | |||
371 | return 0; | ||
372 | } | ||
373 | |||
374 | static void thmc50_init_client(struct i2c_client *client) | ||
375 | { | ||
376 | struct thmc50_data *data = i2c_get_clientdata(client); | ||
377 | int config; | ||
378 | |||
379 | data->analog_out = i2c_smbus_read_byte_data(client, | ||
380 | THMC50_REG_ANALOG_OUT); | ||
381 | /* set up to at least 1 */ | ||
382 | if (data->analog_out == 0) { | ||
383 | data->analog_out = 1; | ||
384 | i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT, | ||
385 | data->analog_out); | ||
386 | } | ||
387 | config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF); | ||
388 | config |= 0x1; /* start the chip if it is in standby mode */ | ||
389 | if (data->has_temp3) | ||
390 | config |= 0x80; /* enable 2nd remote temp */ | ||
391 | i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config); | ||
392 | } | ||
393 | |||
394 | static struct thmc50_data *thmc50_update_device(struct device *dev) | ||
395 | { | ||
396 | struct i2c_client *client = to_i2c_client(dev); | ||
397 | struct thmc50_data *data = i2c_get_clientdata(client); | ||
398 | int timeout = HZ / 5 + (data->type == thmc50 ? HZ : 0); | ||
399 | |||
400 | mutex_lock(&data->update_lock); | ||
401 | |||
402 | if (time_after(jiffies, data->last_updated + timeout) | ||
403 | || !data->valid) { | ||
404 | |||
405 | int temps = data->has_temp3 ? 3 : 2; | ||
406 | int i; | ||
407 | for (i = 0; i < temps; i++) { | ||
408 | data->temp_input[i] = i2c_smbus_read_byte_data(client, | ||
409 | THMC50_REG_TEMP[i]); | ||
410 | data->temp_max[i] = i2c_smbus_read_byte_data(client, | ||
411 | THMC50_REG_TEMP_MAX[i]); | ||
412 | data->temp_min[i] = i2c_smbus_read_byte_data(client, | ||
413 | THMC50_REG_TEMP_MIN[i]); | ||
414 | } | ||
415 | data->analog_out = | ||
416 | i2c_smbus_read_byte_data(client, THMC50_REG_ANALOG_OUT); | ||
417 | data->last_updated = jiffies; | ||
418 | data->valid = 1; | ||
419 | } | ||
420 | |||
421 | mutex_unlock(&data->update_lock); | ||
422 | |||
423 | return data; | ||
424 | } | ||
425 | |||
426 | static int __init sm_thmc50_init(void) | ||
427 | { | ||
428 | return i2c_add_driver(&thmc50_driver); | ||
429 | } | ||
430 | |||
431 | static void __exit sm_thmc50_exit(void) | ||
432 | { | ||
433 | i2c_del_driver(&thmc50_driver); | ||
434 | } | ||
435 | |||
436 | MODULE_AUTHOR("Krzysztof Helt <krzysztof.h1@wp.pl>"); | ||
437 | MODULE_DESCRIPTION("THMC50 driver"); | ||
438 | |||
439 | module_init(sm_thmc50_init); | ||
440 | module_exit(sm_thmc50_exit); | ||