Merge git://git.kernel.org/pub/scm/linux/kernel/git/joern/logfs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hwmon / i5k_amb.c
blobe880e2c3871d0e2098913c7b130c3a35de7e4f65
1 /*
2 * A hwmon driver for the Intel 5000 series chipset FB-DIMM AMB
3 * temperature sensors
4 * Copyright (C) 2007 IBM
6 * Author: Darrick J. Wong <djwong@us.ibm.com>
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/jiffies.h>
25 #include <linux/hwmon.h>
26 #include <linux/hwmon-sysfs.h>
27 #include <linux/err.h>
28 #include <linux/mutex.h>
29 #include <linux/delay.h>
30 #include <linux/log2.h>
31 #include <linux/pci.h>
32 #include <linux/platform_device.h>
33 #include <linux/slab.h>
35 #define DRVNAME "i5k_amb"
37 #define I5K_REG_AMB_BASE_ADDR 0x48
38 #define I5K_REG_AMB_LEN_ADDR 0x50
39 #define I5K_REG_CHAN0_PRESENCE_ADDR 0x64
40 #define I5K_REG_CHAN1_PRESENCE_ADDR 0x66
42 #define AMB_REG_TEMP_MIN_ADDR 0x80
43 #define AMB_REG_TEMP_MID_ADDR 0x81
44 #define AMB_REG_TEMP_MAX_ADDR 0x82
45 #define AMB_REG_TEMP_STATUS_ADDR 0x84
46 #define AMB_REG_TEMP_ADDR 0x85
48 #define AMB_CONFIG_SIZE 2048
49 #define AMB_FUNC_3_OFFSET 768
51 static unsigned long amb_reg_temp_status(unsigned int amb)
53 return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_STATUS_ADDR +
54 AMB_CONFIG_SIZE * amb;
57 static unsigned long amb_reg_temp_min(unsigned int amb)
59 return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MIN_ADDR +
60 AMB_CONFIG_SIZE * amb;
63 static unsigned long amb_reg_temp_mid(unsigned int amb)
65 return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MID_ADDR +
66 AMB_CONFIG_SIZE * amb;
69 static unsigned long amb_reg_temp_max(unsigned int amb)
71 return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MAX_ADDR +
72 AMB_CONFIG_SIZE * amb;
75 static unsigned long amb_reg_temp(unsigned int amb)
77 return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_ADDR +
78 AMB_CONFIG_SIZE * amb;
81 #define MAX_MEM_CHANNELS 4
82 #define MAX_AMBS_PER_CHANNEL 16
83 #define MAX_AMBS (MAX_MEM_CHANNELS * \
84 MAX_AMBS_PER_CHANNEL)
85 #define CHANNEL_SHIFT 4
86 #define DIMM_MASK 0xF
88 * Ugly hack: For some reason the highest bit is set if there
89 * are _any_ DIMMs in the channel. Attempting to read from
90 * this "high-order" AMB results in a memory bus error, so
91 * for now we'll just ignore that top bit, even though that
92 * might prevent us from seeing the 16th DIMM in the channel.
94 #define REAL_MAX_AMBS_PER_CHANNEL 15
95 #define KNOBS_PER_AMB 6
97 static unsigned long amb_num_from_reg(unsigned int byte_num, unsigned int bit)
99 return byte_num * MAX_AMBS_PER_CHANNEL + bit;
102 #define AMB_SYSFS_NAME_LEN 16
103 struct i5k_device_attribute {
104 struct sensor_device_attribute s_attr;
105 char name[AMB_SYSFS_NAME_LEN];
108 struct i5k_amb_data {
109 struct device *hwmon_dev;
111 unsigned long amb_base;
112 unsigned long amb_len;
113 u16 amb_present[MAX_MEM_CHANNELS];
114 void __iomem *amb_mmio;
115 struct i5k_device_attribute *attrs;
116 unsigned int num_attrs;
117 unsigned long chipset_id;
120 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
121 char *buf)
123 return sprintf(buf, "%s\n", DRVNAME);
127 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
129 static struct platform_device *amb_pdev;
131 static u8 amb_read_byte(struct i5k_amb_data *data, unsigned long offset)
133 return ioread8(data->amb_mmio + offset);
136 static void amb_write_byte(struct i5k_amb_data *data, unsigned long offset,
137 u8 val)
139 iowrite8(val, data->amb_mmio + offset);
142 static ssize_t show_amb_alarm(struct device *dev,
143 struct device_attribute *devattr,
144 char *buf)
146 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
147 struct i5k_amb_data *data = dev_get_drvdata(dev);
149 if (!(amb_read_byte(data, amb_reg_temp_status(attr->index)) & 0x20) &&
150 (amb_read_byte(data, amb_reg_temp_status(attr->index)) & 0x8))
151 return sprintf(buf, "1\n");
152 else
153 return sprintf(buf, "0\n");
156 static ssize_t store_amb_min(struct device *dev,
157 struct device_attribute *devattr,
158 const char *buf,
159 size_t count)
161 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
162 struct i5k_amb_data *data = dev_get_drvdata(dev);
163 unsigned long temp = simple_strtoul(buf, NULL, 10) / 500;
165 if (temp > 255)
166 temp = 255;
168 amb_write_byte(data, amb_reg_temp_min(attr->index), temp);
169 return count;
172 static ssize_t store_amb_mid(struct device *dev,
173 struct device_attribute *devattr,
174 const char *buf,
175 size_t count)
177 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
178 struct i5k_amb_data *data = dev_get_drvdata(dev);
179 unsigned long temp = simple_strtoul(buf, NULL, 10) / 500;
181 if (temp > 255)
182 temp = 255;
184 amb_write_byte(data, amb_reg_temp_mid(attr->index), temp);
185 return count;
188 static ssize_t store_amb_max(struct device *dev,
189 struct device_attribute *devattr,
190 const char *buf,
191 size_t count)
193 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
194 struct i5k_amb_data *data = dev_get_drvdata(dev);
195 unsigned long temp = simple_strtoul(buf, NULL, 10) / 500;
197 if (temp > 255)
198 temp = 255;
200 amb_write_byte(data, amb_reg_temp_max(attr->index), temp);
201 return count;
204 static ssize_t show_amb_min(struct device *dev,
205 struct device_attribute *devattr,
206 char *buf)
208 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
209 struct i5k_amb_data *data = dev_get_drvdata(dev);
210 return sprintf(buf, "%d\n",
211 500 * amb_read_byte(data, amb_reg_temp_min(attr->index)));
214 static ssize_t show_amb_mid(struct device *dev,
215 struct device_attribute *devattr,
216 char *buf)
218 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
219 struct i5k_amb_data *data = dev_get_drvdata(dev);
220 return sprintf(buf, "%d\n",
221 500 * amb_read_byte(data, amb_reg_temp_mid(attr->index)));
224 static ssize_t show_amb_max(struct device *dev,
225 struct device_attribute *devattr,
226 char *buf)
228 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
229 struct i5k_amb_data *data = dev_get_drvdata(dev);
230 return sprintf(buf, "%d\n",
231 500 * amb_read_byte(data, amb_reg_temp_max(attr->index)));
234 static ssize_t show_amb_temp(struct device *dev,
235 struct device_attribute *devattr,
236 char *buf)
238 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
239 struct i5k_amb_data *data = dev_get_drvdata(dev);
240 return sprintf(buf, "%d\n",
241 500 * amb_read_byte(data, amb_reg_temp(attr->index)));
244 static ssize_t show_label(struct device *dev,
245 struct device_attribute *devattr,
246 char *buf)
248 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
250 return sprintf(buf, "Ch. %d DIMM %d\n", attr->index >> CHANNEL_SHIFT,
251 attr->index & DIMM_MASK);
254 static int __devinit i5k_amb_hwmon_init(struct platform_device *pdev)
256 int i, j, k, d = 0;
257 u16 c;
258 int res = 0;
259 int num_ambs = 0;
260 struct i5k_amb_data *data = platform_get_drvdata(pdev);
262 /* Count the number of AMBs found */
263 /* ignore the high-order bit, see "Ugly hack" comment above */
264 for (i = 0; i < MAX_MEM_CHANNELS; i++)
265 num_ambs += hweight16(data->amb_present[i] & 0x7fff);
267 /* Set up sysfs stuff */
268 data->attrs = kzalloc(sizeof(*data->attrs) * num_ambs * KNOBS_PER_AMB,
269 GFP_KERNEL);
270 if (!data->attrs)
271 return -ENOMEM;
272 data->num_attrs = 0;
274 for (i = 0; i < MAX_MEM_CHANNELS; i++) {
275 c = data->amb_present[i];
276 for (j = 0; j < REAL_MAX_AMBS_PER_CHANNEL; j++, c >>= 1) {
277 struct i5k_device_attribute *iattr;
279 k = amb_num_from_reg(i, j);
280 if (!(c & 0x1))
281 continue;
282 d++;
284 /* sysfs label */
285 iattr = data->attrs + data->num_attrs;
286 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
287 "temp%d_label", d);
288 iattr->s_attr.dev_attr.attr.name = iattr->name;
289 iattr->s_attr.dev_attr.attr.mode = S_IRUGO;
290 iattr->s_attr.dev_attr.show = show_label;
291 iattr->s_attr.index = k;
292 res = device_create_file(&pdev->dev,
293 &iattr->s_attr.dev_attr);
294 if (res)
295 goto exit_remove;
296 data->num_attrs++;
298 /* Temperature sysfs knob */
299 iattr = data->attrs + data->num_attrs;
300 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
301 "temp%d_input", d);
302 iattr->s_attr.dev_attr.attr.name = iattr->name;
303 iattr->s_attr.dev_attr.attr.mode = S_IRUGO;
304 iattr->s_attr.dev_attr.show = show_amb_temp;
305 iattr->s_attr.index = k;
306 res = device_create_file(&pdev->dev,
307 &iattr->s_attr.dev_attr);
308 if (res)
309 goto exit_remove;
310 data->num_attrs++;
312 /* Temperature min sysfs knob */
313 iattr = data->attrs + data->num_attrs;
314 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
315 "temp%d_min", d);
316 iattr->s_attr.dev_attr.attr.name = iattr->name;
317 iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
318 iattr->s_attr.dev_attr.show = show_amb_min;
319 iattr->s_attr.dev_attr.store = store_amb_min;
320 iattr->s_attr.index = k;
321 res = device_create_file(&pdev->dev,
322 &iattr->s_attr.dev_attr);
323 if (res)
324 goto exit_remove;
325 data->num_attrs++;
327 /* Temperature mid sysfs knob */
328 iattr = data->attrs + data->num_attrs;
329 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
330 "temp%d_mid", d);
331 iattr->s_attr.dev_attr.attr.name = iattr->name;
332 iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
333 iattr->s_attr.dev_attr.show = show_amb_mid;
334 iattr->s_attr.dev_attr.store = store_amb_mid;
335 iattr->s_attr.index = k;
336 res = device_create_file(&pdev->dev,
337 &iattr->s_attr.dev_attr);
338 if (res)
339 goto exit_remove;
340 data->num_attrs++;
342 /* Temperature max sysfs knob */
343 iattr = data->attrs + data->num_attrs;
344 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
345 "temp%d_max", d);
346 iattr->s_attr.dev_attr.attr.name = iattr->name;
347 iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
348 iattr->s_attr.dev_attr.show = show_amb_max;
349 iattr->s_attr.dev_attr.store = store_amb_max;
350 iattr->s_attr.index = k;
351 res = device_create_file(&pdev->dev,
352 &iattr->s_attr.dev_attr);
353 if (res)
354 goto exit_remove;
355 data->num_attrs++;
357 /* Temperature alarm sysfs knob */
358 iattr = data->attrs + data->num_attrs;
359 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
360 "temp%d_alarm", d);
361 iattr->s_attr.dev_attr.attr.name = iattr->name;
362 iattr->s_attr.dev_attr.attr.mode = S_IRUGO;
363 iattr->s_attr.dev_attr.show = show_amb_alarm;
364 iattr->s_attr.index = k;
365 res = device_create_file(&pdev->dev,
366 &iattr->s_attr.dev_attr);
367 if (res)
368 goto exit_remove;
369 data->num_attrs++;
373 res = device_create_file(&pdev->dev, &dev_attr_name);
374 if (res)
375 goto exit_remove;
377 data->hwmon_dev = hwmon_device_register(&pdev->dev);
378 if (IS_ERR(data->hwmon_dev)) {
379 res = PTR_ERR(data->hwmon_dev);
380 goto exit_remove;
383 return res;
385 exit_remove:
386 device_remove_file(&pdev->dev, &dev_attr_name);
387 for (i = 0; i < data->num_attrs; i++)
388 device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
389 kfree(data->attrs);
391 return res;
394 static int __devinit i5k_amb_add(void)
396 int res = -ENODEV;
398 /* only ever going to be one of these */
399 amb_pdev = platform_device_alloc(DRVNAME, 0);
400 if (!amb_pdev)
401 return -ENOMEM;
403 res = platform_device_add(amb_pdev);
404 if (res)
405 goto err;
406 return 0;
408 err:
409 platform_device_put(amb_pdev);
410 return res;
413 static int __devinit i5k_find_amb_registers(struct i5k_amb_data *data,
414 unsigned long devid)
416 struct pci_dev *pcidev;
417 u32 val32;
418 int res = -ENODEV;
420 /* Find AMB register memory space */
421 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
422 devid,
423 NULL);
424 if (!pcidev)
425 return -ENODEV;
427 if (pci_read_config_dword(pcidev, I5K_REG_AMB_BASE_ADDR, &val32))
428 goto out;
429 data->amb_base = val32;
431 if (pci_read_config_dword(pcidev, I5K_REG_AMB_LEN_ADDR, &val32))
432 goto out;
433 data->amb_len = val32;
435 /* Is it big enough? */
436 if (data->amb_len < AMB_CONFIG_SIZE * MAX_AMBS) {
437 dev_err(&pcidev->dev, "AMB region too small!\n");
438 goto out;
441 data->chipset_id = devid;
443 res = 0;
444 out:
445 pci_dev_put(pcidev);
446 return res;
449 static int __devinit i5k_channel_probe(u16 *amb_present, unsigned long dev_id)
451 struct pci_dev *pcidev;
452 u16 val16;
453 int res = -ENODEV;
455 /* Copy the DIMM presence map for these two channels */
456 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
457 if (!pcidev)
458 return -ENODEV;
460 if (pci_read_config_word(pcidev, I5K_REG_CHAN0_PRESENCE_ADDR, &val16))
461 goto out;
462 amb_present[0] = val16;
464 if (pci_read_config_word(pcidev, I5K_REG_CHAN1_PRESENCE_ADDR, &val16))
465 goto out;
466 amb_present[1] = val16;
468 res = 0;
470 out:
471 pci_dev_put(pcidev);
472 return res;
475 static unsigned long i5k_channel_pci_id(struct i5k_amb_data *data,
476 unsigned long channel)
478 switch (data->chipset_id) {
479 case PCI_DEVICE_ID_INTEL_5000_ERR:
480 return PCI_DEVICE_ID_INTEL_5000_FBD0 + channel;
481 case PCI_DEVICE_ID_INTEL_5400_ERR:
482 return PCI_DEVICE_ID_INTEL_5400_FBD0 + channel;
483 default:
484 BUG();
488 static unsigned long chipset_ids[] = {
489 PCI_DEVICE_ID_INTEL_5000_ERR,
490 PCI_DEVICE_ID_INTEL_5400_ERR,
494 static struct pci_device_id i5k_amb_ids[] __devinitdata = {
495 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5000_ERR) },
496 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5400_ERR) },
497 { 0, }
499 MODULE_DEVICE_TABLE(pci, i5k_amb_ids);
501 static int __devinit i5k_amb_probe(struct platform_device *pdev)
503 struct i5k_amb_data *data;
504 struct resource *reso;
505 int i;
506 int res = -ENODEV;
508 data = kzalloc(sizeof(*data), GFP_KERNEL);
509 if (!data)
510 return -ENOMEM;
512 /* Figure out where the AMB registers live */
513 i = 0;
514 do {
515 res = i5k_find_amb_registers(data, chipset_ids[i]);
516 i++;
517 } while (res && chipset_ids[i]);
519 if (res)
520 goto err;
522 /* Copy the DIMM presence map for the first two channels */
523 res = i5k_channel_probe(&data->amb_present[0],
524 i5k_channel_pci_id(data, 0));
525 if (res)
526 goto err;
528 /* Copy the DIMM presence map for the optional second two channels */
529 i5k_channel_probe(&data->amb_present[2],
530 i5k_channel_pci_id(data, 1));
532 /* Set up resource regions */
533 reso = request_mem_region(data->amb_base, data->amb_len, DRVNAME);
534 if (!reso) {
535 res = -EBUSY;
536 goto err;
539 data->amb_mmio = ioremap_nocache(data->amb_base, data->amb_len);
540 if (!data->amb_mmio) {
541 res = -EBUSY;
542 goto err_map_failed;
545 platform_set_drvdata(pdev, data);
547 res = i5k_amb_hwmon_init(pdev);
548 if (res)
549 goto err_init_failed;
551 return res;
553 err_init_failed:
554 iounmap(data->amb_mmio);
555 platform_set_drvdata(pdev, NULL);
556 err_map_failed:
557 release_mem_region(data->amb_base, data->amb_len);
558 err:
559 kfree(data);
560 return res;
563 static int __devexit i5k_amb_remove(struct platform_device *pdev)
565 int i;
566 struct i5k_amb_data *data = platform_get_drvdata(pdev);
568 hwmon_device_unregister(data->hwmon_dev);
569 device_remove_file(&pdev->dev, &dev_attr_name);
570 for (i = 0; i < data->num_attrs; i++)
571 device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
572 kfree(data->attrs);
573 iounmap(data->amb_mmio);
574 release_mem_region(data->amb_base, data->amb_len);
575 platform_set_drvdata(pdev, NULL);
576 kfree(data);
577 return 0;
580 static struct platform_driver i5k_amb_driver = {
581 .driver = {
582 .owner = THIS_MODULE,
583 .name = DRVNAME,
585 .probe = i5k_amb_probe,
586 .remove = __devexit_p(i5k_amb_remove),
589 static int __init i5k_amb_init(void)
591 int res;
593 res = platform_driver_register(&i5k_amb_driver);
594 if (res)
595 return res;
597 res = i5k_amb_add();
598 if (res)
599 platform_driver_unregister(&i5k_amb_driver);
601 return res;
604 static void __exit i5k_amb_exit(void)
606 platform_device_unregister(amb_pdev);
607 platform_driver_unregister(&i5k_amb_driver);
610 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
611 MODULE_DESCRIPTION("Intel 5000 chipset FB-DIMM AMB temperature sensor");
612 MODULE_LICENSE("GPL");
614 module_init(i5k_amb_init);
615 module_exit(i5k_amb_exit);