Hi Darrick: * Darrick J. Wong <djwong at us.ibm.com> [2007-07-31 11:37:07 -0700]: > New driver to read FB-DIMM temperature sensors on systems with the > Intel 5000 series chipsets, and now with all subsequent patches rolled > into one. > > Signed-off-by: Darrick J. Wong <djwong at us.ibm.com> > --- > > drivers/hwmon/Kconfig | 10 + > drivers/hwmon/Makefile | 1 > drivers/hwmon/i5k_amb.c | 529 +++++++++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 540 insertions(+), 0 deletions(-) > > diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig > index 0fe3c52..00dc6fb 100644 > --- a/drivers/hwmon/Kconfig > +++ b/drivers/hwmon/Kconfig > @@ -215,6 +215,16 @@ config SENSORS_DS1621 > This driver can also be built as a module. If so, the module > will be called ds1621. > > +config SENSORS_I5K_AMB > + tristate "FB-DIMM AMB temperature sensor on Intel 5000 series chipsets" > + depends on I2C && EXPERIMENTAL ...and X86, presumably. > + help > + If you say yes here you get support for FB-DIMM AMB temperature > + monitoring chips on systems with the Intel 5000 series chipset. > + > + This driver can also be built as a module. If so, the module > + will be called i5k_amb. > + > config SENSORS_F71805F > tristate "Fintek F71805F/FG and F71872F/FG" > depends on EXPERIMENTAL > diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile > index bd75b44..a29805e 100644 > --- a/drivers/hwmon/Makefile > +++ b/drivers/hwmon/Makefile > @@ -35,6 +35,7 @@ obj-$(CONFIG_SENSORS_FSCPOS) += fscpos.o > obj-$(CONFIG_SENSORS_GL518SM) += gl518sm.o > obj-$(CONFIG_SENSORS_GL520SM) += gl520sm.o > obj-$(CONFIG_SENSORS_HDAPS) += hdaps.o > +obj-$(CONFIG_SENSORS_I5K_AMB) += i5k_amb.o > obj-$(CONFIG_SENSORS_IT87) += it87.o > obj-$(CONFIG_SENSORS_K8TEMP) += k8temp.o > obj-$(CONFIG_SENSORS_LM63) += lm63.o > diff --git a/drivers/hwmon/i5k_amb.c b/drivers/hwmon/i5k_amb.c > new file mode 100644 > index 0000000..4e5072c > --- /dev/null > +++ b/drivers/hwmon/i5k_amb.c > @@ -0,0 +1,529 @@ > +/* > + * A hwmon driver for the Intel 5000 series chipset FB-DIMM AMB > + * temperature sensors > + * Copyright (C) 2007 IBM > + * > + * Author: Darrick J. Wong <djwong at us.ibm.com> > + * > + * This program is free software; you can redistribute it and/or modify > + * it under the terms of the GNU General Public License as published by > + * the Free Software Foundation; either version 2 of the License, or > + * (at your option) any later version. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU General Public License for more details. > + * > + * You should have received a copy of the GNU General Public License > + * along with this program; if not, write to the Free Software > + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA > + */ > + > +#include <linux/module.h> > +#include <linux/jiffies.h> > +#include <linux/hwmon.h> > +#include <linux/hwmon-sysfs.h> > +#include <linux/err.h> > +#include <linux/mutex.h> > +#include <linux/delay.h> > +#include <linux/log2.h> > +#include <linux/pci.h> > +#include <linux/platform_device.h> > + > +#define DRVNAME "i5k_amb" > + > +#define PCI_VENDOR_INTEL 0x8086 > +#define PCI_DEVICE_INTEL_5K_MEM 0x25F0 > +#define PCI_DEVICE_INTEL_5K_FBD0 0x25F5 > +#define PCI_DEVICE_INTEL_5K_FBD1 0x25F6 > + PCI_VENDOR_ID_INTEL is already defined. The rest should be added to include/linux/pci_ids.h, not here. You can do that in the same patch. > +#define I5K_REG_AMB_BASE_ADDR 0x48 > +#define I5K_REG_AMB_LEN_ADDR 0x50 > +#define I5K_REG_CHAN0_PRESENCE_ADDR 0x64 > +#define I5K_REG_CHAN1_PRESENCE_ADDR 0x66 > + > +#define AMB_REG_TEMP_MIN_ADDR 0x80 > +#define AMB_REG_TEMP_MID_ADDR 0x81 > +#define AMB_REG_TEMP_MAX_ADDR 0x82 > +#define AMB_REG_TEMP_STATUS_ADDR 0x84 > +#define AMB_REG_TEMP_ADDR 0x85 > + > +#define AMB_CONFIG_SIZE 2048 > +#define AMB_FUNC_3_OFFSET 768 > + > +#define AMB_REG_TEMP_STATUS(amb) ((amb) * AMB_CONFIG_SIZE + \ > + AMB_FUNC_3_OFFSET + AMB_REG_TEMP_STATUS_ADDR) > +#define AMB_REG_TEMP_MIN(amb) ((amb) * AMB_CONFIG_SIZE + \ > + AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MIN_ADDR) > +#define AMB_REG_TEMP_MID(amb) ((amb) * AMB_CONFIG_SIZE + \ > + AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MID_ADDR) > +#define AMB_REG_TEMP_MAX(amb) ((amb) * AMB_CONFIG_SIZE + \ > + AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MAX_ADDR) > +#define AMB_REG_TEMP(amb) ((amb) * AMB_CONFIG_SIZE + \ > + AMB_FUNC_3_OFFSET + AMB_REG_TEMP_ADDR) > + > +#define MAX_MEM_CHANNELS 4 > +#define MAX_AMBS_PER_CHANNEL 16 > +#define MAX_AMBS (MAX_MEM_CHANNELS * \ > + MAX_AMBS_PER_CHANNEL) > +/* > + * Ugly hack: For some reason the highest bit is set if there > + * are _any_ DIMMs in the channel. Attempting to read from > + * this "high-order" AMB results in a memory bus error, so > + * for now we'll just ignore that top bit, even though that > + * might prevent us from seeing the 16th DIMM in the channel. > + */ > +#define REAL_MAX_AMBS_PER_CHANNEL 15 > +#define KNOBS_PER_AMB 5 > + > +#define AMB_NUM_FROM_REG(byte_num, bit_num) ((byte_num) * \ > + MAX_AMBS_PER_CHANNEL) + (bit_num) > + > +struct i5k_amb_data { > + struct class_device *class_dev; > + struct resource *resource; > + > + unsigned long amb_base; > + unsigned long amb_len; > + u16 amb_present[MAX_MEM_CHANNELS]; > + void __iomem *amb_mmio; > + struct sensor_device_attribute *attrs; > + unsigned int num_attrs; > +}; > + > +static ssize_t show_name(struct device *dev, struct device_attribute *devattr, > + char *buf) > +{ > + return sprintf(buf, "%s\n", DRVNAME); > +} > + > + > +static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0); > + Just DEVICE_ATTR is enough here. > +static struct platform_device *amb_pdev = NULL; > + Initialization to 0/NULL is unnecessary. > +static u8 amb_read_byte(struct i5k_amb_data *data, unsigned long offset) > +{ > + return (*(u8*)(data->amb_mmio + offset)); return ioread8(data->amb_mmio + offset); > +} > + > +static void amb_write_byte(struct i5k_amb_data *data, unsigned long offset, > + u8 val) > +{ > + (*(u8*)(data->amb_mmio + offset)) = val; iowrite8(val, data->amb_mmio + offset); > +} > + > +static ssize_t show_amb_alarm(struct device *dev, > + struct device_attribute *devattr, > + char *buf) > +{ > + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); > + struct i5k_amb_data *data = dev_get_drvdata(dev); > + > + if ( !(amb_read_byte(data, AMB_REG_TEMP_STATUS(attr->index)) & 0x20) && > + (amb_read_byte(data, AMB_REG_TEMP_STATUS(attr->index)) & 0x8) ) > + return sprintf(buf, "1\n"); > + else > + return sprintf(buf, "0\n"); > +} > + > +static ssize_t store_amb_min(struct device *dev, > + struct device_attribute *devattr, > + const char *buf, > + size_t count) > +{ > + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); > + struct i5k_amb_data *data = dev_get_drvdata(dev); > + int temp = simple_strtol(buf, NULL, 10) / 500; > + > + if (temp > 255) > + temp = 255; > + > + amb_write_byte(data, AMB_REG_TEMP_MIN(attr->index), temp); > + return count; > +} See Documentation/hwmon/sysfs_interface: sysfs attribute writes interpretation Summary: use long instead of int, and range check on both ends. > + > +static ssize_t store_amb_mid(struct device *dev, > + struct device_attribute *devattr, > + const char *buf, > + size_t count) > +{ > + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); > + struct i5k_amb_data *data = dev_get_drvdata(dev); > + int temp = simple_strtol(buf, NULL, 10) / 500; > + > + if (temp > 255) > + temp = 255; > + > + amb_write_byte(data, AMB_REG_TEMP_MID(attr->index), temp); > + return count; > +} Ditto. > + > +static ssize_t store_amb_max(struct device *dev, > + struct device_attribute *devattr, > + const char *buf, > + size_t count) > +{ > + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); > + struct i5k_amb_data *data = dev_get_drvdata(dev); > + int temp = simple_strtol(buf, NULL, 10) / 500; > + > + if (temp > 255) > + temp = 255; > + > + amb_write_byte(data, AMB_REG_TEMP_MAX(attr->index), temp); > + return count; > +} Ditto. > + > +static ssize_t show_amb_min(struct device *dev, > + struct device_attribute *devattr, > + char *buf) > +{ > + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); > + struct i5k_amb_data *data = dev_get_drvdata(dev); > + return sprintf(buf, "%d\n", > + 500 * amb_read_byte(data, AMB_REG_TEMP_MIN(attr->index))); > +} > + > +static ssize_t show_amb_mid(struct device *dev, > + struct device_attribute *devattr, > + char *buf) > +{ > + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); > + struct i5k_amb_data *data = dev_get_drvdata(dev); > + return sprintf(buf, "%d\n", > + 500 * amb_read_byte(data, AMB_REG_TEMP_MID(attr->index))); > +} > + > +static ssize_t show_amb_max(struct device *dev, > + struct device_attribute *devattr, > + char *buf) > +{ > + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); > + struct i5k_amb_data *data = dev_get_drvdata(dev); > + return sprintf(buf, "%d\n", > + 500 * amb_read_byte(data, AMB_REG_TEMP_MAX(attr->index))); > +} > + > +static ssize_t show_amb_temp(struct device *dev, > + struct device_attribute *devattr, > + char *buf) > +{ > + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); > + struct i5k_amb_data *data = dev_get_drvdata(dev); > + return sprintf(buf, "%d\n", > + 500 * amb_read_byte(data, AMB_REG_TEMP(attr->index))); > +} > + > +static int __devinit i5k_amb_hwmon_init(struct platform_device *pdev) > +{ > + int i, j, k, d = 0; > + u16 c; > + char *n; > + int res = 0; > + int num_ambs = 0; > + struct i5k_amb_data *data = platform_get_drvdata(pdev); > + > + /* Count the number of AMBs found */ > + for (i = 0; i < MAX_MEM_CHANNELS; i++) { > + c = data->amb_present[i]; > + for (j = 0; j < REAL_MAX_AMBS_PER_CHANNEL; j++) { > + if (c & 0x1) > + num_ambs++; > + c >>= 1; > + } > + } > + > + /* Set up sysfs stuff */ > + data->attrs = kzalloc(sizeof(*data->attrs) * num_ambs * KNOBS_PER_AMB, > + GFP_KERNEL); > + if (!data->attrs) > + return -ENOMEM; > + data->num_attrs = 0; > + > + for (i = 0; i < MAX_MEM_CHANNELS; i++) { > + c = data->amb_present[i]; > + for (j = 0; j < REAL_MAX_AMBS_PER_CHANNEL; j++, c >>= 1) { > + k = AMB_NUM_FROM_REG(i, j); > + if (!(c & 0x1)) > + continue; > + d++; > + > + /* Temperature sysfs knob */ > + n = kmalloc(13, GFP_KERNEL); Magic constant. > + sprintf(n, "temp%d_input", d); Is it possible, at least in theory, for d > 99? > + data->attrs[data->num_attrs].dev_attr.attr.name = n; > + data->attrs[data->num_attrs].dev_attr.attr.mode = > + S_IRUGO; > + data->attrs[data->num_attrs].dev_attr.show = > + show_amb_temp; > + data->attrs[data->num_attrs].index = k; > + res = device_create_file(&pdev->dev, > + &data->attrs[data->num_attrs].dev_attr); > + if (res) > + goto exit_remove; > + data->num_attrs++; > + > + /* Temperature min sysfs knob */ > + n = kmalloc(13, GFP_KERNEL); Magic constant, and not even correct this time, etc. > + sprintf(n, "temp%d_min", d); > + data->attrs[data->num_attrs].dev_attr.attr.name = n; > + data->attrs[data->num_attrs].dev_attr.attr.mode = > + S_IWUSR | S_IRUGO; > + data->attrs[data->num_attrs].dev_attr.show = > + show_amb_min; > + data->attrs[data->num_attrs].dev_attr.store = > + store_amb_min; > + data->attrs[data->num_attrs].index = k; > + res = device_create_file(&pdev->dev, > + &data->attrs[data->num_attrs].dev_attr); > + if (res) > + goto exit_remove; > + data->num_attrs++; > + > + /* Temperature mid sysfs knob */ > + n = kmalloc(13, GFP_KERNEL); > + sprintf(n, "temp%d_mid", d); > + data->attrs[data->num_attrs].dev_attr.attr.name = n; > + data->attrs[data->num_attrs].dev_attr.attr.mode = > + S_IWUSR | S_IRUGO; > + data->attrs[data->num_attrs].dev_attr.show = > + show_amb_mid; > + data->attrs[data->num_attrs].dev_attr.store = > + store_amb_mid; > + data->attrs[data->num_attrs].index = k; > + res = device_create_file(&pdev->dev, > + &data->attrs[data->num_attrs].dev_attr); > + if (res) > + goto exit_remove; > + data->num_attrs++; > + > + /* Temperature max sysfs knob */ > + n = kmalloc(13, GFP_KERNEL); > + sprintf(n, "temp%d_max", d); > + data->attrs[data->num_attrs].dev_attr.attr.name = n; > + data->attrs[data->num_attrs].dev_attr.attr.mode = > + S_IWUSR | S_IRUGO; > + data->attrs[data->num_attrs].dev_attr.show = > + show_amb_max; > + data->attrs[data->num_attrs].dev_attr.store = > + store_amb_max; > + data->attrs[data->num_attrs].index = k; > + res = device_create_file(&pdev->dev, > + &data->attrs[data->num_attrs].dev_attr); > + if (res) > + goto exit_remove; > + data->num_attrs++; > + > + /* Temperature alarm sysfs knob */ > + n = kmalloc(13, GFP_KERNEL); > + sprintf(n, "temp%d_alarm", d); > + data->attrs[data->num_attrs].dev_attr.attr.name = n; > + data->attrs[data->num_attrs].dev_attr.attr.mode = > + S_IRUGO; > + data->attrs[data->num_attrs].dev_attr.show = > + show_amb_alarm; > + data->attrs[data->num_attrs].index = k; > + res = device_create_file(&pdev->dev, > + &data->attrs[data->num_attrs].dev_attr); > + if (res) > + goto exit_remove; > + data->num_attrs++; > + } > + } > + > + res = device_create_file(&pdev->dev, &sensor_dev_attr_name.dev_attr); > + if (res) > + goto exit_remove; > + > + data->class_dev = hwmon_device_register(&pdev->dev); > + if (IS_ERR(data->class_dev)) { > + res = PTR_ERR(data->class_dev); > + goto exit_remove; > + } > + > + return res; > + > +exit_remove: > + device_remove_file(&pdev->dev, &sensor_dev_attr_name.dev_attr); > + for (i = 0; i < data->num_attrs; i++) { > + device_remove_file(&pdev->dev, &data->attrs[i].dev_attr); > + kfree(data->attrs[i].dev_attr.attr.name); > + } > + kfree(data->attrs); Hmm, would be nice if data->attrs included space for all the strings, too. Then you could kmalloc/kfree in one shot. > + > + return res; > +} > + > +static int __devinit i5k_amb_add(void) > +{ > + int res = -ENODEV; > + > + /* only ever going to be one of these */ > + amb_pdev = platform_device_alloc(DRVNAME, 0); > + if (!amb_pdev) > + return -ENOMEM; > + > + res = platform_device_add(amb_pdev); > + if (res) > + goto err; > + return 0; > + > +err: > + platform_device_put(amb_pdev); > + return res; > +} > + > +static int __devinit i5k_amb_probe(struct platform_device *pdev) > +{ > + struct pci_dev *pcidev; > + struct i5k_amb_data *data; > + struct resource *reso; > + u32 val32; > + u16 val16; > + int res = -ENOMEM; -ENODEV seems to be a better default for this function. > + > + data = kzalloc(sizeof(*data), GFP_KERNEL); > + if (!data) > + return -ENOMEM; > + > + /* Figure out where the AMB registers live */ > + pcidev = pci_get_device(PCI_VENDOR_INTEL, PCI_DEVICE_INTEL_5K_MEM, > + NULL); > + if (!pcidev) > + goto out; > + > + if (pci_read_config_dword(pcidev, I5K_REG_AMB_BASE_ADDR, &val32)) > + goto err_free_dev; > + data->amb_base = val32; > + > + if (pci_read_config_dword(pcidev, I5K_REG_AMB_LEN_ADDR, &val32)) > + goto err_free_dev; > + data->amb_len = val32; > + > + pci_dev_put(pcidev); > + > + if (data->amb_len < AMB_CONFIG_SIZE * MAX_AMBS) { > + printk(KERN_ERR DRVNAME "AMB region too small!\n"); > + goto out; > + } > + > + > + /* Copy the DIMM presence map for the first two channels */ > + pcidev = pci_get_device(PCI_VENDOR_INTEL, PCI_DEVICE_INTEL_5K_FBD0, > + NULL); > + if (!pcidev) > + goto out; > + > + if (pci_read_config_word(pcidev, I5K_REG_CHAN0_PRESENCE_ADDR, &val16)) > + goto err_free_dev; > + data->amb_present[0] = val16; > + if (pci_read_config_word(pcidev, I5K_REG_CHAN1_PRESENCE_ADDR, &val16)) > + goto err_free_dev; > + data->amb_present[1] = val16; > + > + pci_dev_put(pcidev); > + > + /* Copy the DIMM presence map for the optional second two channels */ > + pcidev = pci_get_device(PCI_VENDOR_INTEL, PCI_DEVICE_INTEL_5K_FBD1, > + NULL); > + if (!pcidev) > + goto setup_resources; > + Abuse of goto. OK, maybe it's nit-picky... the accepted idiom in Linux is to use goto to unwind inits after an error. This is not an error condition, *and* this goto is easily replaced by an if statement. > + if (pci_read_config_word(pcidev, I5K_REG_CHAN0_PRESENCE_ADDR, &val16)) > + goto err_free_dev; > + data->amb_present[2] = val16; > + if (pci_read_config_word(pcidev, I5K_REG_CHAN1_PRESENCE_ADDR, &val16)) > + goto err_free_dev; > + data->amb_present[3] = val16; > + > + pci_dev_put(pcidev); > + > +setup_resources: > + /* Set up resource regions */ > + reso = request_mem_region(data->amb_base, data->amb_len, DRVNAME); > + if (!reso) { > + res = -EBUSY; > + goto out; > + } > + > + data->amb_mmio = ioremap_nocache(data->amb_base, data->amb_len); > + if (!data->amb_mmio) > + goto err2; -EBUSY > + > + platform_set_drvdata(pdev, data); > + > + res = i5k_amb_hwmon_init(pdev); > + if (res) > + goto err; > + > + return res; > + > +err: > + iounmap(data->amb_mmio); > + platform_set_drvdata(pdev, NULL); > +err2: > + release_mem_region(data->amb_base, data->amb_len); > + goto out; > +err_free_dev: > + pci_dev_put(pcidev); Careful: some of the goto paths that come through here don't hold a pcidev. > +out: > + kfree(data); > + return res; > +} > + Please rename those labels in some consistent way. > +static int __devexit i5k_amb_remove(struct platform_device *pdev) > +{ > + int i; > + struct i5k_amb_data *data = platform_get_drvdata(pdev); > + > + hwmon_device_unregister(data->class_dev); > + device_remove_file(&pdev->dev, &sensor_dev_attr_name.dev_attr); > + for (i = 0; i < data->num_attrs; i++) { > + device_remove_file(&pdev->dev, &data->attrs[i].dev_attr); > + kfree(data->attrs[i].dev_attr.attr.name); > + } > + iounmap(data->amb_mmio); > + release_mem_region(data->amb_base, data->amb_len); > + platform_set_drvdata(pdev, NULL); > + kfree(data); > + return 0; > +} > + > +static struct platform_driver i5k_amb_driver = { > + .driver = { > + .owner = THIS_MODULE, > + .name = DRVNAME, > + }, > + .probe = i5k_amb_probe, > + .remove = __devexit_p(i5k_amb_remove), > +}; > + > +static int __init i5k_amb_init(void) > +{ > + int res; > + > + res = platform_driver_register(&i5k_amb_driver); > + if (res) > + return res; > + > + res = i5k_amb_add(); > + if (res) > + platform_driver_unregister(&i5k_amb_driver); > + > + return res; > +} > + > +static void __exit i5k_amb_exit(void) > +{ > + platform_device_unregister(amb_pdev); > + platform_driver_unregister(&i5k_amb_driver); > +} > + > +MODULE_AUTHOR("Darrick J. Wong <djwong at us.ibm.com>"); > +MODULE_DESCRIPTION("Intel 5000 chipset FB-DIMM AMB temperature sensor"); > +MODULE_LICENSE("GPL"); > + > +module_init(i5k_amb_init); > +module_exit(i5k_amb_exit); Regards, -- Mark M. Hoffman mhoffman at lightlink.com