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