max1668 support revisited

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi All.

I have written a kernel driver for the Maxim part MAX1668, which I
have attached to this email. I have tested this code on a MAX1805 and
it seems to work.

I'm not really sure how to go about publishing it and was hoping for
some advice from this list.

Kind regards,
David


-- 
David George
Karoo Array Telescope
Tel: +27 11 442-2434
Email: david.george@xxxxxxxxx
/*

    Copyright (c) 2011 David George <david.george@xxxxxxxxx>

    based on adm1021.c
    some credit to Christoph Scheurer, but largely a rewrite

    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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/err.h>
#include <linux/mutex.h>

/* Addresses to scan */
static unsigned short normal_i2c[] = {
  0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };

enum chips {
  max1668, max1805, max1989 };

/* max1668 constants specified below */

/* The max1668 registers */
/* Read-only */

/* max1805 does not have REMOTE3 and REMOTE4 */
/* dont differentiate between local and remote */
#define MAX1668_REG_TEMP(nr)            (nr)
#define MAX1668_REG_STATUS1             0x05
#define MAX1668_REG_STATUS2             0x06
#define MAX1668_REG_MAN_ID              0xfe
/* 0x4d = Maxim */
#define MAX1668_REG_DEV_ID              0xff
/* MAX1668 = 0x03, MAX1805 = 0x05, MAX1989 = 0x0b */

/* These use different addresses for reading/writing */
#define MAX1668_REG_CONFIG_R            0x07
#define MAX1668_REG_CONFIG_W            0x12

/* limits */

/* write high limits */
#define MAX1668_REG_LIMH_WR(nr)		(0x13 + 2 * (nr))
/* write low limits */
#define MAX1668_REG_LIML_WR(nr)		(0x14 + 2 * (nr))

/* read high limits */
#define MAX1668_REG_LIMH_RD(nr)		(0x08 + 2 * (nr))
/* read low limits */
#define MAX1668_REG_LIML_RD(nr)		(0x09 + 2 * (nr))

/* Initial values */

/* Each client has this additional data */
struct max1668_data {
	struct device *hwmon_dev;
	enum chips type;

	struct mutex update_lock;
	char valid;		/* !=0 if following fields are valid */
	unsigned long last_updated;	/* In jiffies */

  /* 1x local and 4x remote */
	int temp_max[5];
	int temp_min[5];
	int temp[5];
	u16 alarms;
};

static int max1668_probe(struct i2c_client *client,
			 const struct i2c_device_id *id);
static int max1668_detect(struct i2c_client *client,
			  struct i2c_board_info *info);
static int max1668_remove(struct i2c_client *client);
static struct max1668_data *max1668_update_device(struct device *dev);

/* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
static int read_only;

static const struct i2c_device_id max1668_id[] = {
	{ "max1668", max1668 },
	{ "max1805", max1805 },
	{ "max1989", max1989 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, max1668_id);

/* This is the driver that will be inserted */
static struct i2c_driver max1668_driver = {
	.class		= I2C_CLASS_HWMON,
	.driver = {
		.name	= "max1668",
	},
	.probe		= max1668_probe,
	.remove		= max1668_remove,
	.id_table	= max1668_id,
	.detect		= max1668_detect,
	.address_list	= normal_i2c,
};

static ssize_t show_temp(struct device *dev,
			 struct device_attribute *devattr, char *buf)
{
	int index = to_sensor_dev_attr(devattr)->index;
	struct max1668_data *data = max1668_update_device(dev);

	return sprintf(buf, "%d\n", data->temp[index]);
}

static ssize_t show_temp_max(struct device *dev,
			     struct device_attribute *devattr, char *buf)
{
	int index = to_sensor_dev_attr(devattr)->index;
	struct max1668_data *data = max1668_update_device(dev);

	return sprintf(buf, "%d\n", data->temp_max[index]);
}

static ssize_t show_temp_min(struct device *dev,
			     struct device_attribute *devattr, char *buf)
{
	int index = to_sensor_dev_attr(devattr)->index;
	struct max1668_data *data = max1668_update_device(dev);

	return sprintf(buf, "%d\n", data->temp_min[index]);
}

static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
			  char *buf)
{
	int index = to_sensor_dev_attr(attr)->index;
	struct max1668_data *data = max1668_update_device(dev);
	return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
}

static ssize_t show_alarms(struct device *dev,
			   struct device_attribute *attr,
			   char *buf)
{
	struct max1668_data *data = max1668_update_device(dev);
	return sprintf(buf, "%u\n", data->alarms);
}

static ssize_t set_temp_max(struct device *dev,
			    struct device_attribute *devattr,
			    const char *buf, size_t count)
{
	int index = to_sensor_dev_attr(devattr)->index;
	struct i2c_client *client = to_i2c_client(dev);
	struct max1668_data *data = i2c_get_clientdata(client);
	long temp = simple_strtol(buf, NULL, 10) / 1000;

	mutex_lock(&data->update_lock);
	data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127);
	if (!read_only)
		i2c_smbus_write_byte_data(client, MAX1668_REG_LIMH_WR(index),
					  data->temp_max[index]);
	mutex_unlock(&data->update_lock);

	return count;
}

static ssize_t set_temp_min(struct device *dev,
			    struct device_attribute *devattr,
			    const char *buf, size_t count)
{
	int index = to_sensor_dev_attr(devattr)->index;
	struct i2c_client *client = to_i2c_client(dev);
	struct max1668_data *data = i2c_get_clientdata(client);
	long temp = simple_strtol(buf, NULL, 10) / 1000;

	mutex_lock(&data->update_lock);
	data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127);
	if (!read_only)
		i2c_smbus_write_byte_data(client, MAX1668_REG_LIML_WR(index),
					  data->temp_min[index]);
	mutex_unlock(&data->update_lock);

	return count;
}

static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
			  set_temp_max, 0);
static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
			  set_temp_min, 0);
static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
			  set_temp_max, 1);
static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
			  set_temp_min, 1);
static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 0);
static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
			  set_temp_max, 2);
static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
			  set_temp_min, 2);
static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 1);
static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_max,
			  set_temp_max, 3);
static SENSOR_DEVICE_ATTR(temp4_min, S_IWUSR | S_IRUGO, show_temp_min,
			  set_temp_min, 3);
static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 1);
static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_temp_max,
			  set_temp_max, 4);
static SENSOR_DEVICE_ATTR(temp5_min, S_IWUSR | S_IRUGO, show_temp_min,
			  set_temp_min, 4);

static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 14);
static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 13);
static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 7);
static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 6);
static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 5);
static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, show_alarm, NULL, 3);
static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 2);
static SENSOR_DEVICE_ATTR(temp5_min_alarm, S_IRUGO, show_alarm, NULL, 1);
static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL, 0);

/* Open-circuit diode alarm */
static SENSOR_DEVICE_ATTR(temp_fault_open,  S_IRUGO, show_alarm, NULL, 12);
/* Any threshold alarm set */
static SENSOR_DEVICE_ATTR(temp_fault_alarm, S_IRUGO, show_alarm, NULL, 11);

static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);

static struct attribute *max1668_attributes[] = {
	&sensor_dev_attr_temp1_max.dev_attr.attr,
	&sensor_dev_attr_temp1_min.dev_attr.attr,
	&sensor_dev_attr_temp1_input.dev_attr.attr,
	&sensor_dev_attr_temp2_max.dev_attr.attr,
	&sensor_dev_attr_temp2_min.dev_attr.attr,
	&sensor_dev_attr_temp2_input.dev_attr.attr,
	&sensor_dev_attr_temp3_max.dev_attr.attr,
	&sensor_dev_attr_temp3_min.dev_attr.attr,
	&sensor_dev_attr_temp3_input.dev_attr.attr,
	&sensor_dev_attr_temp4_max.dev_attr.attr,
	&sensor_dev_attr_temp4_min.dev_attr.attr,
	&sensor_dev_attr_temp4_input.dev_attr.attr,
	&sensor_dev_attr_temp5_max.dev_attr.attr,
	&sensor_dev_attr_temp5_min.dev_attr.attr,
	&sensor_dev_attr_temp5_input.dev_attr.attr,

	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
	&sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
	&sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
	&sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
	&sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
	&sensor_dev_attr_temp5_min_alarm.dev_attr.attr,

	&sensor_dev_attr_temp_fault_open.dev_attr.attr,
	&sensor_dev_attr_temp_fault_alarm.dev_attr.attr,

	&dev_attr_alarms.attr,
	NULL
};

static const struct attribute_group max1668_group = {
	.attrs = max1668_attributes,
};

/* Return 0 if detection is successful, -ENODEV otherwise */
static int max1668_detect(struct i2c_client *client,
			  struct i2c_board_info *info)
{
	struct i2c_adapter *adapter = client->adapter;
	const char *type_name;
	int status, config, man_id, dev_id;

	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
		pr_debug("max1668: detect failed, "
			 "smbus byte data not supported!\n");
		return -ENODEV;
	}

	status = (i2c_smbus_read_byte_data(client, MAX1668_REG_STATUS1) << 8) |
	          i2c_smbus_read_byte_data(client, MAX1668_REG_STATUS2);

	config = i2c_smbus_read_byte_data(client, MAX1668_REG_CONFIG_R);

	/* Determine the chip type. */
	man_id = i2c_smbus_read_byte_data(client, MAX1668_REG_MAN_ID);
	dev_id = i2c_smbus_read_byte_data(client, MAX1668_REG_DEV_ID);

  type_name = NULL;

  if (man_id == 0x4d) {
    switch (dev_id){
       case 0x3: 
         type_name = "max1668";
         break;
       case 0x5: 
         type_name = "max1805";
         break;
       case 0xb: 
         type_name = "max1989";
         break;
    }
  }
	/* Check for unsupported part */
	if (!type_name) {
		pr_debug("max1668: unsupported part - manid %x, devid %x\n", man_id, dev_id);
		return -ENODEV;
	}

	pr_debug("max1668: Detected chip %s at adapter %d, address 0x%02x.\n",
		 type_name, i2c_adapter_id(adapter), client->addr);
	strlcpy(info->type, type_name, I2C_NAME_SIZE);

	return 0;
}

static int max1668_probe(struct i2c_client *client,
			 const struct i2c_device_id *id)
{
	struct max1668_data *data;
	int err;

	data = kzalloc(sizeof(struct max1668_data), GFP_KERNEL);
	if (!data) {
		pr_debug("max1668: detect failed, kzalloc failed!\n");
		err = -ENOMEM;
		goto error0;
	}

	i2c_set_clientdata(client, data);
	data->type = id->driver_data;
	mutex_init(&data->update_lock);

	/* Register sysfs hooks */
	if ((err = sysfs_create_group(&client->dev.kobj, &max1668_group)))
		goto error1;

	data->hwmon_dev = hwmon_device_register(&client->dev);
	if (IS_ERR(data->hwmon_dev)) {
		err = PTR_ERR(data->hwmon_dev);
		goto error3;
	}

	return 0;

error3:
	sysfs_remove_group(&client->dev.kobj, &max1668_group);
error1:
	kfree(data);
error0:
	return err;
}

static int max1668_remove(struct i2c_client *client)
{
	struct max1668_data *data = i2c_get_clientdata(client);

	hwmon_device_unregister(data->hwmon_dev);
	sysfs_remove_group(&client->dev.kobj, &max1668_group);

	kfree(data);
	return 0;
}

static struct max1668_data *max1668_update_device(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct max1668_data *data = i2c_get_clientdata(client);

	mutex_lock(&data->update_lock);

	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
	    || !data->valid) {
		int i;

		dev_dbg(&client->dev, "Starting max1668 update\n");

		for (i = 0; i < 5; i++) {
			data->temp[i] = 1000 *
				(s8) i2c_smbus_read_byte_data(
					client, MAX1668_REG_TEMP(i));
			data->temp_max[i] = 1000 *
				(s8) i2c_smbus_read_byte_data(
					client, MAX1668_REG_LIMH_RD(i));
			data->temp_min[i] = 1000 *
				(s8) i2c_smbus_read_byte_data(
					client, MAX1668_REG_LIML_RD(i));
		}
		data->alarms =
       ((i2c_smbus_read_byte_data(client, MAX1668_REG_STATUS1) & 0x78) << 8) |
         i2c_smbus_read_byte_data(client, MAX1668_REG_STATUS2);

		data->last_updated = jiffies;
		data->valid = 1;
	}

	mutex_unlock(&data->update_lock);

	return data;
}

static int __init sensors_max1668_init(void)
{
	return i2c_add_driver(&max1668_driver);
}

static void __exit sensors_max1668_exit(void)
{
	i2c_del_driver(&max1668_driver);
}

MODULE_AUTHOR ("David George <david.george@xxxxxxxxx>");
MODULE_DESCRIPTION("max1668 driver");
MODULE_LICENSE("GPL");

module_param(read_only, bool, 0);
MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");

module_init(sensors_max1668_init)
module_exit(sensors_max1668_exit)
_______________________________________________
lm-sensors mailing list
lm-sensors@xxxxxxxxxxxxxx
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

[Index of Archives]     [Linux Kernel]     [Linux Hardware Monitoring]     [Linux USB Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]

  Powered by Linux