Skip to contentSkip to conversation

driver for emc1023

lm-sensors Anish Patel 3 participants 7 messages Jan 17, 2011 Hardware monitoring

lore
Share & tools
lore
#0 lore raw

here is the driver for the emc1023 and the makefile patch.
i've spent a couple hours now trying to figure out how to create a patch for a new file, and can't find anything. if someone would care to point me in the right direction i'll resubmit, but at this point i am tired of trying to figure this out. Doc/SubmittingDrivers and SubmittingPatches is no help on how to do this.

/* emc1023.c
*
* Copyright (C) 2011 Anish K. Patel <[email protected]>
* based heavily on tmp401.c
*
* 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. *
*
* Driver for the SMSC EMC1023 SMBUS temperature sensor IC.
* examples taken from datasheet
*
* legacy format (LF)
* registers for temperature store them in an 11bit value,
* 2's complement form
* the high byte holds the sign and the whole number part
* the low byte holds the fraction in the upper 3 bits.
* goes from -63.125 to 127.875

 *         bit 7 - 0.500
 *         bit 6 - 0.250
 *         bit 5 - 0.125
 *         	   temp data    junk bits  		 hex
 * ie Diode fault = 10000000000|00000 			 400
 *    -63 	  = 11000001000|00000			 608	
 *    -63.875	  = 11000000001|00000 	 		 601
 *    0	  	  = 00000000000|00000	 		 000
 *    1		  = 00000001000|00000	 		 008
 *    127	  = 01111111000|00000			 3F8
 *    127.875	  = 01111111111|00000			 3FF

*
* for the extend format (EF) add 64d to read the data *
*
* todo - clean up the fraction part, implement EF temp
* *possibly make EF reporting default with module flag
*
*/

#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>
#include <linux/sysfs.h>

/* Addresses to scan */
static const unsigned short normal_i2c[] = { 0x48,0x49,0x4c,0x4d, I2C_CLIENT_END };

enum chips { emc1023_1 , emc1023_2, emc1023_3, emc1023_4 };

/*
* The EMC1023 registers, note some registers have different addresses for
* reading and writing
* device id - seems to be the 8,9,c,d corresponding to address of IC
* ie devid 0x08 ic addr 0x48, etc
*/
#define EMC1023_ONE_SHOT_CMD 0x0F
#define EMC1023_RMT1_IF_RW 0x27
#define EMC1023_RMT2_IF_RW 0x28
#define EMC1023_STATUS 0x02
#define EMC1023_CONFIG_READ 0x03
#define EMC1023_CONFIG_WRITE 0x09
#define EMC1023_DEVICE_ID_REG 0xED
#define EMC1023_MANUFACTURER_ID_REG 0xFE
#define EMC1023_PRODUCT_ID_REG 0xFF

#define EMC1023_MANUFACTURER_ID 0x5D

#define EMC1023_DEVICE_ID_1 0x0C
#define EMC1023_DEVICE_ID_2 0x0D
#define EMC1023_DEVICE_ID_3 0x08
#define EMC1023_DEVICE_ID_4 0x09
static const u8 EMC1023_DEVICE_ID[] = { 0x08, 0x09,

						    0x0c, 0x0d };

static const u8 EMC1023_LF_TEMP_HIGH_BYTE[3] = { 0x00, 0x01, 0xF8 };
static const u8 EMC1023_LF_TEMP_LOW_BYTE[3] = { 0x23, 0x10, 0xF9 };

static const u8 EMC1023_EF_TEMP_HIGH_BYTE[2] = { 0xFA, 0XFC };
static const u8 EMC1023_EF_TEMP_LOW_BYTE[2] = { 0xFB, 0XFD };

/*
* Driver data (common to all clients)
*/

static const struct i2c_device_id emc1023_id[] = {
	{ "emc1023_1", emc1023_1 },
	{ "emc1023_2", emc1023_2 },
	{ "emc1023_3", emc1023_3 },
	{ "emc1023_4", emc1023_4 },
	{ }
};

MODULE_DEVICE_TABLE(i2c, emc1023_id);

/*
* Client data (each client gets its own)
*/

struct emc1023_data {
	struct device *hwmon_dev;
	struct mutex update_lock;
	char valid; /* zero until following fields are valid */
	unsigned long last_updated; /* in jiffies */
	enum chips kind;
	/* register values */
	u8 status;
	u8 config;
	u16 temp;
	u8 temp_high[3];
	u8 temp_low[3];
};

/*
* Sysfs attr show / store functions
*/

static int emc1023_register_to_temp(u8 reg_high,u8 reg_low)

{
	int temphigh = reg_high;
	//since we only need the top 3bits from reg_low shift over 5
	//and then do the magic below to pop temp low with correct data
	int templow = (reg_low >> 5);

	if ( temphigh > 128 )
		temphigh = ((temphigh % 128 ) - 128)*1000;
	else
		temphigh *= 1000;
	//get fraction part and populate temp low with info (ugly)
	templow =(((templow & 4 ) >> 2) * 500 ) + 
	         (((templow & 2 ) >> 1) * 250 ) + 
		 ((templow & 1 ) * 125);
	
	return (temphigh+templow);
}

static struct emc1023_data *emc1023_update_device_reg16(
struct i2c_client *client, struct emc1023_data *data)

{
	int i;

	for (i=0; i < 3; i++) {
		data->temp_high[i] = i2c_smbus_read_byte_data(client,
			EMC1023_LF_TEMP_HIGH_BYTE[i]);
		data->temp_low[i] = i2c_smbus_read_byte_data(client,
			EMC1023_LF_TEMP_LOW_BYTE[i]);

	}
	return data;
}

static struct emc1023_data *emc1023_update_device(struct device *dev)

{
	struct i2c_client *client = to_i2c_client(dev);
	struct emc1023_data *data = i2c_get_clientdata(client);

	mutex_lock(&data->update_lock);

	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
		data->status = i2c_smbus_read_byte_data(client, EMC1023_STATUS);
		data->config = i2c_smbus_read_byte_data(client,	EMC1023_CONFIG_READ);
		emc1023_update_device_reg16(client, data);

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

	mutex_unlock(&data->update_lock);

	return data;
}

static ssize_t show_temp_value(struct device *dev,
struct device_attribute *devattr, char *buf)

{
	int index = to_sensor_dev_attr(devattr)->index;
	struct emc1023_data *data = emc1023_update_device(dev);
	
	return sprintf(buf, "%d\n",
		emc1023_register_to_temp(data->temp_high[index], data->temp_low[index]));
}

/*static ssize_t show_status(struct device *dev,
struct device_attribute *devattr, char *buf)

{
	//int mask = to_sensor_dev_attr(devattr)->index;
	struct emc1023_data *data = emc1023_update_device(dev);

	if ( (data->status >> 7) == 1 )
		return sprintf(buf, "ready\n");
	else
		return sprintf(buf, "not ready\n");
}*/

static struct sensor_device_attribute emc1023_attr[] = {
SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_value, NULL, 0), SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_value, NULL, 1), SENSOR_ATTR(temp3_input, S_IRUGO, show_temp_value, NULL, 2),
};

/*
* Begin non sysfs callback code (aka Real code)
*/

static void emc1023_init_client(struct i2c_client *client)

{
	int config;

	/* Start conversions (disable shutdown if necessary) */
	config = i2c_smbus_read_byte_data(client, EMC1023_CONFIG_READ);
	if (config < 0) {
		dev_warn(&client->dev, "Initialization failed!\n");
		return;
	}

}

static int emc1023_detect(struct i2c_client *client,
struct i2c_board_info *info)

{
	//int i=0;
	struct i2c_adapter *adapter = client->adapter;
	u8 reg;
	enum chips kind;
	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
		return -ENODEV;

	/* Detect and identify the chip */
	reg = i2c_smbus_read_byte_data(client, EMC1023_MANUFACTURER_ID_REG);
	if (reg != EMC1023_MANUFACTURER_ID)
		return -ENODEV;

	reg = i2c_smbus_read_byte_data(client, EMC1023_DEVICE_ID_REG);

	//tried to cycle through array to match id.
	/*for(i=0;i<4;i++)
		if( reg == EMC1023_DEVICE_ID[i] )
		{
			kind = i;
			break;
		}*/
	switch (reg) {
	case EMC1023_DEVICE_ID_1:
		kind = emc1023_1;
		break;
	case EMC1023_DEVICE_ID_2:
		kind = emc1023_2;
		break;
	case EMC1023_DEVICE_ID_3:
		kind = emc1023_3;
		break;
	case EMC1023_DEVICE_ID_4:
		kind = emc1023_4;
		break;
	default:
		return -ENODEV;
	}

	strlcpy(info->type, emc1023_id[kind].name, I2C_NAME_SIZE);

	return 0;
}

static int emc1023_remove(struct i2c_client *client)

{
	struct emc1023_data *data = i2c_get_clientdata(client);
	int i;

	if (data->hwmon_dev)
		hwmon_device_unregister(data->hwmon_dev);

	for (i = 0; i < ARRAY_SIZE(emc1023_attr); i++)
		device_remove_file(&client->dev, &emc1023_attr[i].dev_attr);

	kfree(data);
	return 0;
}

static int emc1023_probe(struct i2c_client *client,
const struct i2c_device_id *id)

{
	int i, err = 0;
	struct emc1023_data *data;
	const char *names[] = { "EMC1023_1", "EMC1023_2", "EMC1023_3","EMC1023_4" };

	data = kzalloc(sizeof(struct emc1023_data), GFP_KERNEL);
	if (!data)
		return -ENOMEM;

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

	/* Initialize the EMC1023 chip */
	emc1023_init_client(client);

	/* Register sysfs hooks */
	for (i = 0; i < ARRAY_SIZE(emc1023_attr); i++) {
		err = device_create_file(&client->dev,
					 &emc1023_attr[i].dev_attr);
		if (err)
			goto exit_remove;
	}

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

	dev_info(&client->dev, "Detected emc1023 %s chip\n", names[data->kind]);

	return 0;

exit_remove:

	emc1023_remove(client); /* will also free data for us */
	return err;
}
static struct i2c_driver emc1023_driver = {
	.class		= I2C_CLASS_HWMON,
	.driver = {
		.name	= "emc1023",
	},
	.probe		= emc1023_probe,
	.remove		= emc1023_remove,
	.id_table	= emc1023_id,
	.detect		= emc1023_detect,
	.address_list	= normal_i2c,
};

static int __init emc1023_init(void)

{
	return i2c_add_driver(&emc1023_driver);
}

static void __exit emc1023_exit(void)

{
	i2c_del_driver(&emc1023_driver);
}

MODULE_AUTHOR("Anish K Patel <anishp(at)win-ent!com!>"); MODULE_DESCRIPTION("SMSC emc1023 temperature sensor driver"); MODULE_LICENSE("GPL");

module_init(emc1023_init);
module_exit(emc1023_exit);

drivers/hwmon/Kconfig +10 -0
diff -uprN -X linux-2.6.36.2/Documentation/dontdiff linux-2.6.36.2/drivers/hwmon/Kconfig linux-new/drivers/hwmon/Kconfig
@@ -310,6 +310,16 @@ config SENSORS_DS1621
 	  This driver can also be built as a module.  If so, the module
 	  will be called ds1621.
 
+config SENSORS_EMC1023
+	tristate "SMSC emc1023 Thermal Sensor"
+	depends on I2C
+	help
+	  If you say yes here you get support for the SMC1023 Thermal Sensor
+	  monitoring chip
+
+	  This driver can also be built as a module.  If so, the module 
+	  will be called emc1023
+
 config SENSORS_I5K_AMB
 	tristate "FB-DIMM AMB temperature sensor on Intel 5000 series chipsets"
 	depends on PCI && EXPERIMENTAL
drivers/hwmon/Makefile +1 -0
diff -uprN -X linux-2.6.36.2/Documentation/dontdiff linux-2.6.36.2/drivers/hwmon/Makefile linux-new/drivers/hwmon/Makefile
@@ -107,6 +107,7 @@ obj-$(CONFIG_SENSORS_W83627EHF)	+= w8362
 obj-$(CONFIG_SENSORS_W83L785TS)	+= w83l785ts.o
 obj-$(CONFIG_SENSORS_W83L786NG)	+= w83l786ng.o
 obj-$(CONFIG_SENSORS_WM831X)	+= wm831x-hwmon.o
+obj-$(CONFIG_SENSORS_EMC1023)	+= emc1023.o
 obj-$(CONFIG_SENSORS_WM8350)	+= wm8350-hwmon.o
 
 ifeq ($(CONFIG_HWMON_DEBUG_CHIP),y)

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#1 lore raw
Quoted 7 lines · Anish Patel

here is the driver for the emc1023 and the makefile patch. i've spent a couple hours now trying to figure out how to create a patch for a new file, and can't find anything.
if someone would care to point me in the right direction i'll resubmit, but at this point i am tired of trying to figure this out. Doc/SubmittingDrivers and SubmittingPatches is no help on how to do this.

Nowadays, pretty much everyone uses git.

Clone a new kernel tree from
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git.

Make your changes. Commit locally into your git repository. If necessary, use "git rebase -i" to merge multiple commits into one when you are ready to submit the driver.

Generate a patch file, for example for the most recent commit, with "git format-patch -s HEAD~1".

As for the driver itself, please run it through scripts/checkpatch.pl and fix all reported errors and warnings. You can use scripts/cleanfile to fix some of the problems.

Couple of additional comments. This is not a complete review - I'll look at the code in more detail after the coding style errors are fixed.

You don't need to enumerate different chip IDs. From a driver (and user) perspective, it is irrelevant if the chip variant is -1, -2, -3, or -4. So you are adding a lot of complexity for very little gain. It might make sense, though, to support other chips of the same family (1023, 1043, 1053, 1063) if there is no difference in register access. For that, it would make sense to have separate entries in i2c_device_id, but you still won't need to keep ->kind around unless programming is different for any of the chips supported. Such as:

static const struct i2c_device_id emc1023_id[] = {
        { "emc1023", 0 },
        { "emc1043", 0 },
        { "emc1053", 0 },
        { "emc1063", 0 },
        { }
};

Then identify the actual chip in emc1023_detect based on the Product ID and assign above names as detected.

In this context, we'll have to sort out the product ID issues, since 0x0c and 0x0d point to emc1043, not to emc1023.

Please use sysfs_create_group()/sysfs_remove_group() to register and remove sysfs hooks.

Thanks,
Guenter

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#2 lore raw
In reply to Anish Patel
Quoted 9 lines · Guenter Roeck
Quoted 6 lines · Anish Patel

here is the driver for the emc1023 and the makefile patch. i've spent a couple hours now trying to figure out how to create a patch for a new file, and can't find anything.
if someone would care to point me in the right direction i'll resubmit, but at this point i am tired of trying to figure this out. Doc/SubmittingDrivers and SubmittingPatches is no help on how to do this.

Nowadays, pretty much everyone uses git.

I very much doubt it.

Quoted 2 lines

Clone a new kernel tree from
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git.

You're suggesting Anish to download over 1 GB of kernel data history just to write one small patch?

Quoted 6 lines

Make your changes. Commit locally into your git repository. If necessary, use "git rebase -i" to merge multiple commits into one when you are ready to submit the driver.

Generate a patch file, for example for the most recent commit, with "git format-patch -s HEAD~1".

Even me have never used these git commands. I doubt that pointing an inexperienced developer in that direction is going to work.

If anything, quilt is one order of magnitude easier to deal with IMHO, and it doesn't require downloading anything Anish doesn't already have (short of quilt itself, but it's pretty small.)

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#3 lore raw
In reply to Anish Patel
Quoted 9 lines · Jean Delvare
Quoted 8 lines · Guenter Roeck
Quoted 6 lines · Anish Patel

here is the driver for the emc1023 and the makefile patch. i've spent a couple hours now trying to figure out how to create a patch for a new file, and can't find anything.
if someone would care to point me in the right direction i'll resubmit, but at this point i am tired of trying to figure this out. Doc/SubmittingDrivers and SubmittingPatches is no help on how to do this.

Quoted 4 lines
Quoted 1 line

Nowadays, pretty much everyone uses git.

I very much doubt it.

Ok, point taken.

Quoted 6 lines
Quoted 2 lines

Clone a new kernel tree from
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git.

You're suggesting Anish to download over 1 GB of kernel data history just to write one small patch?

Guess so. Didn't really occur to me that might be a problem.

Quoted 11 lines
Quoted 7 lines

Make your changes. Commit locally into your git repository. If necessary, use "git rebase -i" to merge multiple commits into one when you are ready to submit the driver.

Generate a patch file, for example for the most recent commit, with "git format-patch -s HEAD~1".

Even me have never used these git commands. I doubt that pointing an inexperienced developer in that direction is going to work.

Hey, even I learned to use it ;)

Quoted 4 lines

If anything, quilt is one order of magnitude easier to deal with IMHO, and it doesn't require downloading anything Anish doesn't already have (short of quilt itself, but it's pretty small.)

Fine with me too. For whatever reason, I never got it to work, though, so I ended up sticking with git. Guess it is for the individual to decide if git or quilt is easier to deal with ...

Cheers,
Guenter

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#4 lore raw
In reply to Anish Patel
Quoted 2 lines · Guenter Roeck
Quoted 1 line · Anish Patel

here is the driver for the emc1023 and the makefile patch.

Couple of additional comments.

The chip reports fault conditions for the external sensors. Would be great if you could add attribute files to report it (tempX_fault).

Also, we'll need Documentation/hwmon/emc1023.

Thanks,
Guenter

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#5 lore raw
In reply to Anish Patel
Quoted 5 lines · Guenter Roeck
Quoted 2 lines · Guenter Roeck
Quoted 1 line · Anish Patel

here is the driver for the emc1023 and the makefile patch.

Couple of additional comments.

Quoted 10 lines

The chip reports fault conditions for the external sensors. Would be great if you could add attribute files to report it (tempX_fault).

Also, we'll need Documentation/hwmon/emc1023.

Thanks,
Guenter

ok, i will try to add them. is there some document that i can look at that kinda tells me whats avaiable for functions?
right now i just modified the heck out of the tmp401 driver.

thanks!

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#6 lore raw
In reply to Anish Patel
Quoted 11 lines · Anish Patel
Quoted 10 lines · Guenter Roeck
Quoted 2 lines · Guenter Roeck
Quoted 1 line · Anish Patel

here is the driver for the emc1023 and the makefile patch.

Couple of additional comments.

The chip reports fault conditions for the external sensors. Would be great if you could add attribute files to report it (tempX_fault).

Also, we'll need Documentation/hwmon/emc1023.

Quoted 9 lines
Quoted 5 lines

Thanks,
Guenter

ok, i will try to add them. is there some document that i can look at that kinda tells me whats avaiable for functions?
right now i just modified the heck out of the tmp401 driver.

You can add something like

SENSOR_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0x01), SENSOR_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0x02),

In show_temp_fault(), extract the bit (0x1 or 0x2) and use it to return the fault status.

static ssize_t show_temp_fault(struct device *dev,
struct device_attribute *devattr, char *buf)

{
        struct emc1023_data *data = emc1023_update_device(dev);
        unsigned int bit = to_sensor_dev_attr(devattr)->index;

	if (IS_ERR(data))
		return PTR_ERR(data);

        return sprintf(buf, "%u\n", !!(data->status & bit));
}

Which reminds me ... in recent drivers, we try to return error codes from the update function, similar to what I did above. Look into ltc4261.c to get some hints how to do that.

Thanks,
Guenter

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors