All of lore.kernel.org
 help / color / mirror / Atom feed
* Adm1030 driver
@ 2005-05-19  6:24 Cedric Pradalier
  2005-05-19  6:24 ` Jean Delvare
                   ` (20 more replies)
  0 siblings, 21 replies; 22+ messages in thread
From: Cedric Pradalier @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

Hi,

	I use debian on an ibook (2.2) and the chip which control the fan and
monitor temperature is an adm1030. I made a little kernel module for
2.6.4 which provides access to the chip through sysfs (temperature
reading and read/set the parameter of the integrated fan control
algorithm). I've seen that this chip driver is in development and I
wonder if my driver can help. 

Please let me know.

-- 
Cedric

"[Of course] I'm French! Why do think I have this outrageous 
accent, you silly king-a?!"  Monty Python and the Holy Grail

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Adm1030 driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
  2005-05-19  6:24 ` Jean Delvare
@ 2005-05-19  6:24 ` Mark Studebaker
  2005-05-19  6:24 ` Cedric Pradalier
                   ` (18 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: Mark Studebaker @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

I think the entry on our new drivers page is quite old;
would you like to try and contact those people?
I'll try and find an email address.

Khali do you remember any recent info on this chip?

Cedric Pradalier wrote:
> Hi,
> 
> 	I use debian on an ibook (2.2) and the chip which control the fan and
> monitor temperature is an adm1030. I made a little kernel module for
> 2.6.4 which provides access to the chip through sysfs (temperature
> reading and read/set the parameter of the integrated fan control
> algorithm). I've seen that this chip driver is in development and I
> wonder if my driver can help. 
> 
> Please let me know.
> 

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Adm1030 driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
@ 2005-05-19  6:24 ` Jean Delvare
  2005-05-19  6:24 ` Mark Studebaker
                   ` (19 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: Jean Delvare @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

> I think the entry on our new drivers page is quite old;
> would you like to try and contact those people?
> I'll try and find an email address.
> 
> Khali do you remember any recent info on this chip?

The ADM1030 and ADM1031 are mostly compatible and belong to the same
driver. The ADM1031 has one more temperature channel and some fan
filtering capability.

The original thread is here:
http://archives.andrew.net.au/lm-sensors/msg00393.html

I don't know if ADM finally sent the eval board to George or not. I
tried contacting George and Aaron in November 2003 but had no reply. I
fear we can consider them lost, and the eval board with them if there
ever was one.

So I guess we can consider Cedric's work as the only code available at
the moment. Cedric, can you submit your work to us for a review?
Prefered form would be a patch against 2.6.5-rc1 or later. Make sure you
follow the sysfs naming conventions as found in
Documentation/i2c/sysfs-interface of the same Linux tree.

Thanks.

-- 
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Adm1030 driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
                   ` (5 preceding siblings ...)
  2005-05-19  6:24 ` Mark Studebaker
@ 2005-05-19  6:24 ` Cedric Pradalier
  2005-05-19  6:24 ` ADM1030 Driver Alexandre d'Alton
                   ` (13 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: Cedric Pradalier @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

On Sat, 27 Mar 2004 13:27:02 +0100, 
Jean Delvare gracefully wrote:
>
>The ADM1030 and ADM1031 are mostly compatible and belong to the same
>driver. The ADM1031 has one more temperature channel and some fan
>filtering capability.
>
>The original thread is here:
>http://archives.andrew.net.au/lm-sensors/msg00393.html
>
>I don't know if ADM finally sent the eval board to George or not. I
>tried contacting George and Aaron in November 2003 but had no reply. I
>fear we can consider them lost, and the eval board with them if there
>ever was one.
>
>So I guess we can consider Cedric's work as the only code available at
>the moment. Cedric, can you submit your work to us for a review?

Sure. The current version, which is quite limited, can be found at 
http://cedric.pradalier.free.fr/ibook2/index.html
I have the docs, so I'll be able to add functionalities if needed.

>Prefered form would be a patch against 2.6.5-rc1 or later. 
This is 2.4. But I'll try to improve.

> Make sure
>you follow the sysfs naming conventions as found in
>Documentation/i2c/sysfs-interface of the same Linux tree.

I'm sure I don't ! But since someone is interested, I'll try to adapt.

>
>Thanks.
>


-- 
Cedric

"[Of course] I'm French! Why do think I have this outrageous 
accent, you silly king-a?!"  Monty Python and the Holy Grail

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Adm1030 driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
                   ` (2 preceding siblings ...)
  2005-05-19  6:24 ` Cedric Pradalier
@ 2005-05-19  6:24 ` Jean Delvare
  2005-05-19  6:24 ` Mark Studebaker
                   ` (16 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: Jean Delvare @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

> Sure. The current version, which is quite limited, can be found at 
> http://cedric.pradalier.free.fr/ibook2/index.html
> I have the docs, so I'll be able to add functionalities if needed.

Mmm, as I read it, it doesn't belong to our i2c subsystem (well, it
makes use of it but is really different from the hardware monitoring
drivers we have). Usually our drivers are passive and simply export
their registers, with some conversions, to sysfs. Any active behavior
either belongs to the hardware, or to the userspace, but not to the
driver, as you have done if I'm not mistaking.

So to make it short, I don't think I'm qualified to review your code.

> > Make sure
> > you follow the sysfs naming conventions as found in
> > Documentation/i2c/sysfs-interface of the same Linux tree.
> 
> I'm sure I don't ! But since someone is interested, I'll try to adapt.

Oh well, this doesn't apply then. The sysfs interface is meant for our
driver design, and obviously doesn't apply to yours at the moment.
Unless you want to rewrite a driver that fits into our architecture.

-- 
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Adm1030 driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
                   ` (4 preceding siblings ...)
  2005-05-19  6:24 ` Mark Studebaker
@ 2005-05-19  6:24 ` Mark Studebaker
  2005-05-19  6:24 ` Cedric Pradalier
                   ` (14 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: Mark Studebaker @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

agreed. 
This doesn't conform to either our 2.4 or 2.6 driver template in several ways:
	- no chip detection
	- no bus functionality checking
	- no conformance to sysfs guidelines
	- uses i2c_master_send rather than i2c_smbus_xxx
	- thermal control loop in kernel space
	- Apple-specific and keywest-specific code

mds

Jean Delvare wrote:
>>Sure. The current version, which is quite limited, can be found at 
>>http://cedric.pradalier.free.fr/ibook2/index.html
>>I have the docs, so I'll be able to add functionalities if needed.
> 
> 
> Mmm, as I read it, it doesn't belong to our i2c subsystem (well, it
> makes use of it but is really different from the hardware monitoring
> drivers we have). Usually our drivers are passive and simply export
> their registers, with some conversions, to sysfs. Any active behavior
> either belongs to the hardware, or to the userspace, but not to the
> driver, as you have done if I'm not mistaking.
> 
> So to make it short, I don't think I'm qualified to review your code.
> 
> 
>>>Make sure
>>>you follow the sysfs naming conventions as found in
>>>Documentation/i2c/sysfs-interface of the same Linux tree.
>>
>>I'm sure I don't ! But since someone is interested, I'll try to adapt.
> 
> 
> Oh well, this doesn't apply then. The sysfs interface is meant for our
> driver design, and obviously doesn't apply to yours at the moment.
> Unless you want to rewrite a driver that fits into our architecture.
> 

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Adm1030 driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
                   ` (3 preceding siblings ...)
  2005-05-19  6:24 ` Jean Delvare
@ 2005-05-19  6:24 ` Mark Studebaker
  2005-05-19  6:24 ` Mark Studebaker
                   ` (15 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: Mark Studebaker @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

Note that there are three already-in-kernel drivers/macintosh/therm_*.c
which suffer from many of the same problems below.

interesting...



Mark Studebaker wrote:
> agreed. This doesn't conform to either our 2.4 or 2.6 driver template in 
> several ways:
>     - no chip detection
>     - no bus functionality checking
>     - no conformance to sysfs guidelines
>     - uses i2c_master_send rather than i2c_smbus_xxx
>     - thermal control loop in kernel space
>     - Apple-specific and keywest-specific code
> 
> mds
> 
> Jean Delvare wrote:
> 
>>> Sure. The current version, which is quite limited, can be found at 
>>> http://cedric.pradalier.free.fr/ibook2/index.html
>>> I have the docs, so I'll be able to add functionalities if needed.
>>
>>
>>
>> Mmm, as I read it, it doesn't belong to our i2c subsystem (well, it
>> makes use of it but is really different from the hardware monitoring
>> drivers we have). Usually our drivers are passive and simply export
>> their registers, with some conversions, to sysfs. Any active behavior
>> either belongs to the hardware, or to the userspace, but not to the
>> driver, as you have done if I'm not mistaking.
>>
>> So to make it short, I don't think I'm qualified to review your code.
>>
>>
>>>> Make sure
>>>> you follow the sysfs naming conventions as found in
>>>> Documentation/i2c/sysfs-interface of the same Linux tree.
>>>
>>>
>>> I'm sure I don't ! But since someone is interested, I'll try to adapt.
>>
>>
>>
>> Oh well, this doesn't apply then. The sysfs interface is meant for our
>> driver design, and obviously doesn't apply to yours at the moment.
>> Unless you want to rewrite a driver that fits into our architecture.
>>
> 
> 

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Adm1030 driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
  2005-05-19  6:24 ` Jean Delvare
  2005-05-19  6:24 ` Mark Studebaker
@ 2005-05-19  6:24 ` Cedric Pradalier
  2005-05-19  6:24 ` Jean Delvare
                   ` (17 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: Cedric Pradalier @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

On Sat, 27 Mar 2004 09:54:43 -0500, 
Mark Studebaker gracefully wrote:
>Note that there are three already-in-kernel drivers/macintosh/therm_*.c
>which suffer from many of the same problems below.
>
>interesting...

I knew that. I've made the remark to one of the macintosh guru. 

From a software engineering point of view, it seems strange to have a
set of device not so different but developped without collaboration.


>
>Mark Studebaker wrote:
>> agreed. This doesn't conform to either our 2.4 or 2.6 driver template
>in > several ways:
>>     - no chip detection
>>     - no bus functionality checking
>>     - no conformance to sysfs guidelines
>>     - uses i2c_master_send rather than i2c_smbus_xxx
>>     - thermal control loop in kernel space

Just as a remark, my driver is not managing thermal control loop, just
exporting the relevant chip registers to control the chip automatic
thermal control behavior.

>>     - Apple-specific and keywest-specific code
>> 
>> mds
>> 

So the work to adapt to the lm-sensor seems quite important. I'm afraid
I won't have time to do it right now. Please feel free to use my modest
development as a basis for a true lm-sensor driver.

Nevertheless, I feel I know quite well the adm1030/1 now. 
So, please don't hesitate to contact me for testing or information.

-- 
Cedric

"[Of course] I'm French! Why do think I have this outrageous 
accent, you silly king-a?!"  Monty Python and the Holy Grail

^ permalink raw reply	[flat|nested] 22+ messages in thread

* ADM1030 Driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
                   ` (9 preceding siblings ...)
  2005-05-19  6:24 ` Alexandre d'Alton
@ 2005-05-19  6:24 ` Alexandre d'Alton
  2005-05-19  6:24 ` Alexandre d'Alton
                   ` (9 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: Alexandre d'Alton @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

Hi everybody,

Here is the first version of the adm1030 sensor driver I've made. It is
tested on my MSI MegaPC 651 which is SiS chipset based.

The driver is written for the Linux 2.6.4 kernel, if you want to compile
it just add 
obj-m += adm1030.o 
to the drivers/i2c/chips/Makefile. It's ugly, but if you do not see a
lot of issues in the code, I'll prepare a patch for the latest 2.6.5-mm
kernel.

What the driver can do :
	- Read system temperature ( temp_local_in )
	- Read CPU Temperature ( temp_remote_in )
	- Set minimum CPU fan speed ( fan_rpm )
	- Read CPU fan speed (pwm_input)

What is it supposed to do but not verified correctly:
	- temp_local_min, temp_local_range are the values to start the fan and
the range to let the fan active. 
	- temp_remote_min, temp_remote_range are the values to start the fan
and the range to let the fan active. 

Please give me your feedbacks,

Thanks,

Alex.

The code is following inlined :

++++++++++++++++++++++++++ adm1030.c +++++++++++++++++++++++++++++++++++
/*
    adm1030.c - Part of lm_sensors, Linux kernel modules for hardware
             monitoring
    Based on lm75.c
    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
    Copyright (c) 2004 Alexandre d'Alton <alex@alexdalton.org>

    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/config.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/i2c-sensor.h>
#include "adm1030.h"


/* Addresses to scan */
static unsigned short normal_i2c[] = { I2C_CLIENT_END };
static unsigned short normal_i2c_range[] = { 0x2e, I2C_CLIENT_END };
static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };

/* Insmod parameters */
SENSORS_INSMOD_1(adm1030);


/* Each client has this additional data */
struct adm1030_data {
  struct semaphore	update_lock;
  char			valid;		/* !=0 if following fields are valid */
  unsigned long		last_updated;	/* In jiffies */
  u16			temp_local_in;	/* Register values */
  u16			temp_remote_in;
  u16			fan_rpm;
  u16		        conf1;
  u16			conf2;
  u16			ftac;
  u16			pwm;
  u16                   temp_local_min;
  u16                   temp_local_range;
  u16                   temp_remote_min;
  u16                   temp_remote_range;
};

static int adm1030_attach_adapter(struct i2c_adapter *adapter);
static int adm1030_detect(struct i2c_adapter *adapter, int address, int
kind);
static void adm1030_init_client(struct i2c_client *client);
static int adm1030_detach_client(struct i2c_client *client);
static int adm1030_read_value(struct i2c_client *client, u8 reg);
static int adm1030_write_value(struct i2c_client *client, u8 reg, u16
value);
static struct adm1030_data *adm1030_update_device(struct device *dev);


/* This is the driver that will be inserted */
static struct i2c_driver adm1030_driver = {
	.owner		= THIS_MODULE,
	.name		= "adm1030",
	.id		= I2C_DRIVERID_ADM1030,
	.flags		= I2C_DF_NOTIFY,
	.attach_adapter	= adm1030_attach_adapter,
	.detach_client	= adm1030_detach_client,
};

static int adm1030_id = 0;

#define show(value)	\
static ssize_t show_##value(struct device *dev, char *buf)		\
{									\
	struct adm1030_data *data = adm1030_update_device(dev);		\
	return sprintf(buf, "%d\n", data->value);	\
}
show(temp_local_in);
show(temp_remote_in);
show(conf1);
show(conf2);
show(ftac);
show(pwm);
show(temp_local_min);
show(temp_local_range);
show(temp_remote_min);
show(temp_remote_range);

static ssize_t show_fan_rpm(struct device *dev, char *buf)		
{									
  int fan_speed;
  struct adm1030_data *data = adm1030_update_device(dev);		
  fan_speed = (11250 * 60) / (data->fan_rpm * 8);
  return sprintf(buf, "%d\n", fan_speed);	
}

#define set(value, reg)	\
static ssize_t set_##value(struct device *dev, const char *buf, size_t
count)	\
{								\
	struct i2c_client *client = to_i2c_client(dev);		\
	struct adm1030_data *data = i2c_get_clientdata(client);	\
	int temp = simple_strtoul(buf, NULL, 10);		\
	data->value = temp;			\
	adm1030_write_value(client, reg, data->value);		\
	return count;						\
}

set(conf1, ADM1030_REG_CONF1);
set(conf2, ADM1030_REG_CONF2);
set(ftac, ADM1030_REG_FTAC);
set(pwm, ADM1030_REG_FSP);

void set_temp_min(struct i2c_client *client, int value, int reg)
{
  int val = adm1030_read_value(client, reg);
  val = ( ( ( value >> 2 ) & 0x1f ) << 3 ) | ( val & 0x7 ); 
  adm1030_write_value(client, reg, val);
}
void set_temp_range(struct i2c_client *client, int value, int reg)
{
  int val = adm1030_read_value(client, reg);
  switch (value) {
  case 5: value = 0; break;
  case 10: value = 1; break;
  case 20: value = 2; break;
  case 40: value = 3; break;
  case 80: value = 4; break;
  default: return;
  }
  val = (val&0xf8) | (value & 0x7); 
  adm1030_write_value(client, reg, val);
}

static ssize_t set_temp_local_min(struct device *dev, const char *buf,
size_t
				  count) 
{
  struct i2c_client * client = to_i2c_client(dev);
  int temp = simple_strtoul(buf, NULL, 10);		
  set_temp_min(client, temp, ADM1030_REG_LTR);
  return count;
}
static ssize_t set_temp_local_range(struct device *dev, const char *buf,
size_t
				  count) 
{
  struct i2c_client * client = to_i2c_client(dev);
  int temp = simple_strtoul(buf, NULL, 10);		
  set_temp_range(client, temp, ADM1030_REG_LTR);
  return count;
  
}
static ssize_t set_temp_remote_min(struct device *dev, const char *buf,
size_t
				  count) 
{
  struct i2c_client * client = to_i2c_client(dev);
  int temp = simple_strtoul(buf, NULL, 10);		
  set_temp_min(client, temp, ADM1030_REG_RTR);
  return count;  
}

static ssize_t set_temp_remote_range(struct device *dev, const char
*buf, size_t
				  count) 
{
  struct i2c_client * client = to_i2c_client(dev);
  int temp = simple_strtoul(buf, NULL, 10);		
  set_temp_range(client, temp, ADM1030_REG_RTR);
  return count;
  
}

//static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
set_temp_max);
//static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hyst,
set_temp_hyst);
static DEVICE_ATTR(conf1, S_IWUSR | S_IRUGO, show_conf1, set_conf1);
static DEVICE_ATTR(conf2, S_IWUSR | S_IRUGO, show_conf2, set_conf2);
static DEVICE_ATTR(ftac, S_IWUSR | S_IRUGO, show_ftac, set_ftac);
static DEVICE_ATTR(pwm, S_IWUSR | S_IRUGO, show_pwm, set_pwm);
static DEVICE_ATTR(temp_local_in, S_IRUGO, show_temp_local_in, NULL);
static DEVICE_ATTR(temp_remote_in, S_IRUGO, show_temp_remote_in, NULL);
static DEVICE_ATTR(fan_rpm, S_IRUGO , show_fan_rpm, NULL);
static DEVICE_ATTR(temp_local_min, S_IRUGO | S_IWUSR, 
		   show_temp_local_min, set_temp_local_min);
static DEVICE_ATTR(temp_local_range, S_IRUGO | S_IWUSR, 
		   show_temp_local_range, set_temp_local_range);
static DEVICE_ATTR(temp_remote_min, S_IRUGO | S_IWUSR, 
		   show_temp_remote_min, set_temp_remote_min);
static DEVICE_ATTR(temp_remote_range, S_IRUGO | S_IWUSR, 
		   show_temp_remote_range, set_temp_remote_range);

static int adm1030_attach_adapter(struct i2c_adapter *adapter)
{
	if (!(adapter->class & I2C_ADAP_CLASS_SMBUS))
		return 0;
	return i2c_detect(adapter, &addr_data, adm1030_detect);
}

/* This function is called by i2c_detect */
static int adm1030_detect(struct i2c_adapter *adapter, int address, int
kind)
{
	int id, co;
	struct i2c_client *new_client;
	struct adm1030_data *data;
	int err = 0;
	const char *name = "";

	/* Make sure we aren't probing the ISA bus!! This is just a safety
check
	   at this moment; i2c_detect really won't call us. */
#ifdef DEBUG
	if (i2c_is_isa_adapter(adapter)) {
		dev_dbg(&adapter->dev,
			"adm1030_detect called for an ISA bus adapter?!?\n");
		goto exit;
	}
#endif

	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
				     I2C_FUNC_SMBUS_WORD_DATA))
		goto exit;

	/* OK. For now, we presume we have a valid client. We now create the
	   client structure, even though we cannot fill it completely yet.
	   But it allows us to access lm75_{read,write}_value. */
	if (!(new_client = kmalloc(sizeof(struct i2c_client) +
				   sizeof(struct adm1030_data),
				   GFP_KERNEL))) {
		err = -ENOMEM;
		goto exit;
	}
	memset(new_client, 0x00, sizeof(struct i2c_client) +
				 sizeof(struct adm1030_data));

	data = (struct adm1030_data *) (new_client + 1);
	i2c_set_clientdata(new_client, data);
	new_client->addr = address;
	new_client->adapter = adapter;
	new_client->driver = &adm1030_driver;
	new_client->flags = 0;

	/* Now, we do the remaining detection. It is lousy. */
	if (kind < 0) {
		id = i2c_smbus_read_byte_data(new_client, 0x3d);
		co = i2c_smbus_read_byte_data(new_client, 0x3e);
		
		//		printk("read values@%x : %x %x\n", address, id, co);
		
		if((id != 0x30) && (co != 0x41))
		  goto exit_free;
	}

	/* Determine the chip type - only one kind supported! */
	if (kind <= 0)
		kind = adm1030;

	if (kind = adm1030) {
		name = "adm1030";
	}

	/* Fill in the remaining client fields and put it into the global list
*/
	strlcpy(new_client->name, name, I2C_NAME_SIZE);

	new_client->id = adm1030_id++;
	data->valid = 0;
	init_MUTEX(&data->update_lock);

	/* Tell the I2C layer a new client has arrived */
	if ((err = i2c_attach_client(new_client)))
		goto exit_free;

	/* Initialize the LM75 chip */
	adm1030_init_client(new_client);
	
	/* Register sysfs hooks */
	device_create_file(&new_client->dev, &dev_attr_temp_local_in);
	device_create_file(&new_client->dev, &dev_attr_temp_remote_in);
	device_create_file(&new_client->dev, &dev_attr_fan_rpm);

	/* Following entries are for debugging purpose only, commented
	 * out. These are access to HW configuration registers. 
	 */
	/*
	  device_create_file(&new_client->dev, &dev_attr_conf1);
	  device_create_file(&new_client->dev, &dev_attr_conf2);
	  device_create_file(&new_client->dev, &dev_attr_ftac);
	*/
	device_create_file(&new_client->dev, &dev_attr_pwm);

	device_create_file(&new_client->dev, &dev_attr_temp_local_min);
	device_create_file(&new_client->dev, &dev_attr_temp_local_range);
	device_create_file(&new_client->dev, &dev_attr_temp_remote_min);
	device_create_file(&new_client->dev, &dev_attr_temp_remote_range);

	return 0;

exit_free:
	kfree(new_client);
exit:
	return err;
}

static int adm1030_detach_client(struct i2c_client *client)
{
	i2c_detach_client(client);
	kfree(client);
	return 0;
}

static int adm1030_read_value(struct i2c_client *client, u8 reg)
{
  return i2c_smbus_read_byte_data(client, reg);
}

static int adm1030_write_value(struct i2c_client *client, u8 reg, u16
value)
{
  return i2c_smbus_write_byte_data(client, reg, value);
}

static void adm1030_init_client(struct i2c_client *client)
{
  int read_val;
	/* Initialize the ADM1030 chip (enables fan speed reading )*/
	adm1030_write_value(client, ADM1030_REG_CONF2, 0x3f);

	/* Increase the accuracy on rpm measurements */
	read_val = adm1030_read_value(client, ADM1030_REG_FCH);
	adm1030_write_value(client, ADM1030_REG_FCH, 0xc0 | read_val);

}

static struct adm1030_data *adm1030_update_device(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1030_data *data = i2c_get_clientdata(client);
	int temp_range;
	static unsigned short values[5] = {5, 10, 20, 40, 80};
	
	down(&data->update_lock);

	if ((jiffies - data->last_updated > HZ + HZ / 2) ||
	    (jiffies < data->last_updated) || !data->valid) {
		dev_dbg(&client->dev, "Starting adm1030 update\n");

		data->temp_local_in = adm1030_read_value(client,
ADM1030_REG_TEMP_INT);
		data->temp_remote_in = adm1030_read_value(client,
ADM1030_REG_TEMP_EXT);
		data->fan_rpm      = adm1030_read_value(client,
							  ADM1030_REG_FAN_SPEED);
		temp_range = adm1030_read_value(client,	ADM1030_REG_LTR);
		data->temp_local_min = ((temp_range >> 3) & 0x1f) * 4;
		data->temp_local_range = (values[temp_range&0x7]);

		temp_range = adm1030_read_value(client,	ADM1030_REG_RTR);
		data->temp_remote_min = ((temp_range >> 3) & 0x1f) * 4;
		data->temp_remote_range = (values[temp_range&0x7]);

		data->conf1 = adm1030_read_value(client, ADM1030_REG_CONF1);
		data->conf2 = adm1030_read_value(client, ADM1030_REG_CONF2);
		data->ftac      = adm1030_read_value(client, ADM1030_REG_FTAC);
		data->pwm      = 0xf & adm1030_read_value(client, ADM1030_REG_FSP);
		data->last_updated = jiffies;
		data->valid = 1;
	}

	up(&data->update_lock);

	return data;
}

static int __init sensors_adm1030_init(void)
{
	return i2c_add_driver(&adm1030_driver);
}

static void __exit sensors_adm1030_exit(void)
{
	i2c_del_driver(&adm1030_driver);
}

MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
MODULE_DESCRIPTION("ADM1030 driver");
MODULE_LICENSE("GPL");

module_init(sensors_adm1030_init);
module_exit(sensors_adm1030_exit);
+++++++++++++++++++++++++ end adm1030.c ++++++++++++++++++++++++++++++++


+++++++++++++++++++++++++++++ adm1030.h ++++++++++++++++++++++++++++++++
/*
    adm1030.h - Part of lm_sensors, Linux kernel modules for hardware
             monitoring
    Copyright (c) 2004 Alexandre d'Alton <alex@alexdalton.org>

    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.
*/

/*
    This file contains common code for encoding/decoding LM75 type
    temperature readings, which are emulated by many of the chips
    we support.  As the user is unlikely to load more than one driver
    which contains this code, we don't worry about the wasted space.
*/

#include <linux/i2c-sensor.h>

/* Many ADM1030 constants specified below */
/* The ADM1030 registers */
/* Configuration register 1 */
#define ADM1030_REG_CONF1		0x00
#define ADM1030_CONF1_MEN               0x01 /* monitoring enable */
#define ADM1030_CONF1_IEN               0x02 /* INT enable */
#define ADM1030_CONF1_TMODE             0x04 /* Tach/Ain mode */
#define ADM1030_CONF1_PINV              0x08 /* PWM Invert */
#define ADM1030_CONF1_FFE               0x10 /* Fan Fault Enable */
#define ADM1030_CONF1_PMODE             0x60 /* PWM Mode 00 = Remote
						control Fan (Select
						PWM when manual) 11 						Fastest Calc speed
						controls fan (Select
						RPM when manual)*/
#define ADM1030_CONF1_AEN               0x80 /* Auto / SW Control */

/* Configuration register 2 */
#define ADM1030_REG_CONF2		0x01
#define ADM1030_CONF2_PEN               0x01 /* PWM1 Enable */
#define ADM1030_CONF2_UN1               0x02 /* Unused */
#define ADM1030_CONF2_TIEN              0x04 /* TACH input Enable  */
#define ADM1030_CONF2_UN2               0x08 /* Unused */
#define ADM1030_CONF2_LTE               0x10 /* Local temp enable */
#define ADM1030_CONF2_RTE               0x20 /* Remote temp enable */
#define ADM1030_CONF2_UN3               0x40 /* Unused */
#define ADM1030_CONF2_SWR               0x80 /* SW Reset */

/* STATUS register 1 */
#define ADM1030_REG_STAT1		0x02
#define ADM1030_STAT1_ASP               0x01 /* Alarm Speed  */
#define ADM1030_STAT1_FF                0x02 /* Fan Fault */
#define ADM1030_STAT1_RTH               0x04 /* Remote Temp High  */
#define ADM1030_STAT1_RTL               0x08 /* Remote Temp Low */
#define ADM1030_STAT1_RTT               0x10 /* Remote Temp Therm */
#define ADM1030_STAT1_RDE               0x20 /* Remote Diode Error */
#define ADM1030_STAT1_LTH               0x40 /* Local Temp High */
#define ADM1030_STAT1_LTL               0x80 /* Loc Temp Low */

/* STATUS register 2 */
#define ADM1030_REG_STAT2		0x03
#define ADM1030_STAT2_UN1               0x3f /* Unused  */
#define ADM1030_STAT2_LTH               0x40 /* Local Temp Therm */
#define ADM1030_STAT2_LTL               0x80 /* Therm Status */

/* Extended Temp register  */
#define ADM1030_REG_EXT 		0x06
#define ADM1030_EXT_ASP                 0x07 /* Remote extended temp
range  */
#define ADM1030_EXT_LTH                 0x38 /* Unused  */
#define ADM1030_EXT_LTL                 0xc0 /* Local extended temp
range */

/* Fan charasteristic register  */
#define ADM1030_REG_FCH 		0x20
#define ADM1030_FCH_FSU                 0x07 /* Fan spin up  */
#define ADM1030_EXT_PWMF                0x38 /* PWM Freq  */
#define ADM1030_EXT_SPR                 0xc0 /* Speed range */

/* Fan Speed register  */
#define ADM1030_REG_FSP 		0x22
#define ADM1030_FSP_NSP                 0x0f /* Fan min speed  */

/* Fan Filter register  */
#define ADM1030_REG_FLT 		0x23
#define ADM1030_FLT_SUD                 0x80 /* Spin up disable  */
#define ADM1030_FLT_RR                  0x60 /* Ramp rate  */
#define ADM1030_FLT_ASR                 0x1c /* ADC Sample rate  */
#define ADM1030_FLT_UN1                 0x02 /* Unused  */
#define ADM1030_FLT_FFE                 0x01 /* Fan Filter enable */

/* Local Temp Tmin / Trange  */
#define ADM1030_REG_LTR 		0x24
/* Remote Temp Tmin / Trange */
#define ADM1030_REG_RTR 		0x25
#define ADM1030_TR_TM                  0xf8 /* Local Temp Tmin  */
#define ADM1030_TR_TR                  0x60 /* Local Temp Trange  */

/* Therm to Fan  */
#define ADM1030_REG_TTF 		0x3f
#define ADM1030_TTF_EN                  0x80 /* Therm to fan enable */

#define ADM1030_REG_FAN_SPEED   	0x08
#define ADM1030_REG_FTAC		0x10

#define ADM1030_REG_TEMP_INT		0x0a
#define ADM1030_REG_TEMP_EXT		0x0b


+++++++++++++++++++++++++ end adm1030.h ++++++++++++++++++++++++++++++++



^ permalink raw reply	[flat|nested] 22+ messages in thread

* ADM1030 Driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
                   ` (8 preceding siblings ...)
  2005-05-19  6:24 ` Jean Delvare
@ 2005-05-19  6:24 ` Alexandre d'Alton
  2005-05-19  6:24 ` Alexandre d'Alton
                   ` (10 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: Alexandre d'Alton @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

OK,

I just read the Documentation/i2c/porting-clients and sysfs-interface,
and I saw that I am far from the standard (in terms of naming and coding
conventions...) 
Will work on it tomorrow...

Alex.

On Wed, 2004-04-07 at 21:45, Alexandre d'Alton wrote:
> Hi everybody,
> 
> Here is the first version of the adm1030 sensor driver I've made. It is
> tested on my MSI MegaPC 651 which is SiS chipset based.
> 
> The driver is written for the Linux 2.6.4 kernel, if you want to compile
> it just add 
> obj-m += adm1030.o 
> to the drivers/i2c/chips/Makefile. It's ugly, but if you do not see a
> lot of issues in the code, I'll prepare a patch for the latest 2.6.5-mm
> kernel.
> 
> What the driver can do :
> 	- Read system temperature ( temp_local_in )
> 	- Read CPU Temperature ( temp_remote_in )
> 	- Set minimum CPU fan speed ( fan_rpm )
> 	- Read CPU fan speed (pwm_input)
> 
> What is it supposed to do but not verified correctly:
> 	- temp_local_min, temp_local_range are the values to start the fan and
> the range to let the fan active. 
> 	- temp_remote_min, temp_remote_range are the values to start the fan
> and the range to let the fan active. 
> 
> Please give me your feedbacks,
> 
> Thanks,
> 
> Alex.
> 
> The code is following inlined :
> 
> ++++++++++++++++++++++++++ adm1030.c +++++++++++++++++++++++++++++++++++
> /*
>     adm1030.c - Part of lm_sensors, Linux kernel modules for hardware
>              monitoring
>     Based on lm75.c
>     Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
>     Copyright (c) 2004 Alexandre d'Alton <alex@alexdalton.org>
> 
>     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/config.h>
> #include <linux/module.h>
> #include <linux/init.h>
> #include <linux/slab.h>
> #include <linux/i2c.h>
> #include <linux/i2c-sensor.h>
> #include "adm1030.h"
> 
> 
> /* Addresses to scan */
> static unsigned short normal_i2c[] = { I2C_CLIENT_END };
> static unsigned short normal_i2c_range[] = { 0x2e, I2C_CLIENT_END };
> static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
> static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
> 
> /* Insmod parameters */
> SENSORS_INSMOD_1(adm1030);
> 
> 
> /* Each client has this additional data */
> struct adm1030_data {
>   struct semaphore	update_lock;
>   char			valid;		/* !=0 if following fields are valid */
>   unsigned long		last_updated;	/* In jiffies */
>   u16			temp_local_in;	/* Register values */
>   u16			temp_remote_in;
>   u16			fan_rpm;
>   u16		        conf1;
>   u16			conf2;
>   u16			ftac;
>   u16			pwm;
>   u16                   temp_local_min;
>   u16                   temp_local_range;
>   u16                   temp_remote_min;
>   u16                   temp_remote_range;
> };
> 
> static int adm1030_attach_adapter(struct i2c_adapter *adapter);
> static int adm1030_detect(struct i2c_adapter *adapter, int address, int
> kind);
> static void adm1030_init_client(struct i2c_client *client);
> static int adm1030_detach_client(struct i2c_client *client);
> static int adm1030_read_value(struct i2c_client *client, u8 reg);
> static int adm1030_write_value(struct i2c_client *client, u8 reg, u16
> value);
> static struct adm1030_data *adm1030_update_device(struct device *dev);
> 
> 
> /* This is the driver that will be inserted */
> static struct i2c_driver adm1030_driver = {
> 	.owner		= THIS_MODULE,
> 	.name		= "adm1030",
> 	.id		= I2C_DRIVERID_ADM1030,
> 	.flags		= I2C_DF_NOTIFY,
> 	.attach_adapter	= adm1030_attach_adapter,
> 	.detach_client	= adm1030_detach_client,
> };
> 
> static int adm1030_id = 0;
> 
> #define show(value)	\
> static ssize_t show_##value(struct device *dev, char *buf)		\
> {									\
> 	struct adm1030_data *data = adm1030_update_device(dev);		\
> 	return sprintf(buf, "%d\n", data->value);	\
> }
> show(temp_local_in);
> show(temp_remote_in);
> show(conf1);
> show(conf2);
> show(ftac);
> show(pwm);
> show(temp_local_min);
> show(temp_local_range);
> show(temp_remote_min);
> show(temp_remote_range);
> 
> static ssize_t show_fan_rpm(struct device *dev, char *buf)		
> {									
>   int fan_speed;
>   struct adm1030_data *data = adm1030_update_device(dev);		
>   fan_speed = (11250 * 60) / (data->fan_rpm * 8);
>   return sprintf(buf, "%d\n", fan_speed);	
> }
> 
> #define set(value, reg)	\
> static ssize_t set_##value(struct device *dev, const char *buf, size_t
> count)	\
> {								\
> 	struct i2c_client *client = to_i2c_client(dev);		\
> 	struct adm1030_data *data = i2c_get_clientdata(client);	\
> 	int temp = simple_strtoul(buf, NULL, 10);		\
> 	data->value = temp;			\
> 	adm1030_write_value(client, reg, data->value);		\
> 	return count;						\
> }
> 
> set(conf1, ADM1030_REG_CONF1);
> set(conf2, ADM1030_REG_CONF2);
> set(ftac, ADM1030_REG_FTAC);
> set(pwm, ADM1030_REG_FSP);
> 
> void set_temp_min(struct i2c_client *client, int value, int reg)
> {
>   int val = adm1030_read_value(client, reg);
>   val = ( ( ( value >> 2 ) & 0x1f ) << 3 ) | ( val & 0x7 ); 
>   adm1030_write_value(client, reg, val);
> }
> void set_temp_range(struct i2c_client *client, int value, int reg)
> {
>   int val = adm1030_read_value(client, reg);
>   switch (value) {
>   case 5: value = 0; break;
>   case 10: value = 1; break;
>   case 20: value = 2; break;
>   case 40: value = 3; break;
>   case 80: value = 4; break;
>   default: return;
>   }
>   val = (val&0xf8) | (value & 0x7); 
>   adm1030_write_value(client, reg, val);
> }
> 
> static ssize_t set_temp_local_min(struct device *dev, const char *buf,
> size_t
> 				  count) 
> {
>   struct i2c_client * client = to_i2c_client(dev);
>   int temp = simple_strtoul(buf, NULL, 10);		
>   set_temp_min(client, temp, ADM1030_REG_LTR);
>   return count;
> }
> static ssize_t set_temp_local_range(struct device *dev, const char *buf,
> size_t
> 				  count) 
> {
>   struct i2c_client * client = to_i2c_client(dev);
>   int temp = simple_strtoul(buf, NULL, 10);		
>   set_temp_range(client, temp, ADM1030_REG_LTR);
>   return count;
>   
> }
> static ssize_t set_temp_remote_min(struct device *dev, const char *buf,
> size_t
> 				  count) 
> {
>   struct i2c_client * client = to_i2c_client(dev);
>   int temp = simple_strtoul(buf, NULL, 10);		
>   set_temp_min(client, temp, ADM1030_REG_RTR);
>   return count;  
> }
> 
> static ssize_t set_temp_remote_range(struct device *dev, const char
> *buf, size_t
> 				  count) 
> {
>   struct i2c_client * client = to_i2c_client(dev);
>   int temp = simple_strtoul(buf, NULL, 10);		
>   set_temp_range(client, temp, ADM1030_REG_RTR);
>   return count;
>   
> }
> 
> //static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
> set_temp_max);
> //static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hyst,
> set_temp_hyst);
> static DEVICE_ATTR(conf1, S_IWUSR | S_IRUGO, show_conf1, set_conf1);
> static DEVICE_ATTR(conf2, S_IWUSR | S_IRUGO, show_conf2, set_conf2);
> static DEVICE_ATTR(ftac, S_IWUSR | S_IRUGO, show_ftac, set_ftac);
> static DEVICE_ATTR(pwm, S_IWUSR | S_IRUGO, show_pwm, set_pwm);
> static DEVICE_ATTR(temp_local_in, S_IRUGO, show_temp_local_in, NULL);
> static DEVICE_ATTR(temp_remote_in, S_IRUGO, show_temp_remote_in, NULL);
> static DEVICE_ATTR(fan_rpm, S_IRUGO , show_fan_rpm, NULL);
> static DEVICE_ATTR(temp_local_min, S_IRUGO | S_IWUSR, 
> 		   show_temp_local_min, set_temp_local_min);
> static DEVICE_ATTR(temp_local_range, S_IRUGO | S_IWUSR, 
> 		   show_temp_local_range, set_temp_local_range);
> static DEVICE_ATTR(temp_remote_min, S_IRUGO | S_IWUSR, 
> 		   show_temp_remote_min, set_temp_remote_min);
> static DEVICE_ATTR(temp_remote_range, S_IRUGO | S_IWUSR, 
> 		   show_temp_remote_range, set_temp_remote_range);
> 
> static int adm1030_attach_adapter(struct i2c_adapter *adapter)
> {
> 	if (!(adapter->class & I2C_ADAP_CLASS_SMBUS))
> 		return 0;
> 	return i2c_detect(adapter, &addr_data, adm1030_detect);
> }
> 
> /* This function is called by i2c_detect */
> static int adm1030_detect(struct i2c_adapter *adapter, int address, int
> kind)
> {
> 	int id, co;
> 	struct i2c_client *new_client;
> 	struct adm1030_data *data;
> 	int err = 0;
> 	const char *name = "";
> 
> 	/* Make sure we aren't probing the ISA bus!! This is just a safety
> check
> 	   at this moment; i2c_detect really won't call us. */
> #ifdef DEBUG
> 	if (i2c_is_isa_adapter(adapter)) {
> 		dev_dbg(&adapter->dev,
> 			"adm1030_detect called for an ISA bus adapter?!?\n");
> 		goto exit;
> 	}
> #endif
> 
> 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
> 				     I2C_FUNC_SMBUS_WORD_DATA))
> 		goto exit;
> 
> 	/* OK. For now, we presume we have a valid client. We now create the
> 	   client structure, even though we cannot fill it completely yet.
> 	   But it allows us to access lm75_{read,write}_value. */
> 	if (!(new_client = kmalloc(sizeof(struct i2c_client) +
> 				   sizeof(struct adm1030_data),
> 				   GFP_KERNEL))) {
> 		err = -ENOMEM;
> 		goto exit;
> 	}
> 	memset(new_client, 0x00, sizeof(struct i2c_client) +
> 				 sizeof(struct adm1030_data));
> 
> 	data = (struct adm1030_data *) (new_client + 1);
> 	i2c_set_clientdata(new_client, data);
> 	new_client->addr = address;
> 	new_client->adapter = adapter;
> 	new_client->driver = &adm1030_driver;
> 	new_client->flags = 0;
> 
> 	/* Now, we do the remaining detection. It is lousy. */
> 	if (kind < 0) {
> 		id = i2c_smbus_read_byte_data(new_client, 0x3d);
> 		co = i2c_smbus_read_byte_data(new_client, 0x3e);
> 		
> 		//		printk("read values@%x : %x %x\n", address, id, co);
> 		
> 		if((id != 0x30) && (co != 0x41))
> 		  goto exit_free;
> 	}
> 
> 	/* Determine the chip type - only one kind supported! */
> 	if (kind <= 0)
> 		kind = adm1030;
> 
> 	if (kind = adm1030) {
> 		name = "adm1030";
> 	}
> 
> 	/* Fill in the remaining client fields and put it into the global list
> */
> 	strlcpy(new_client->name, name, I2C_NAME_SIZE);
> 
> 	new_client->id = adm1030_id++;
> 	data->valid = 0;
> 	init_MUTEX(&data->update_lock);
> 
> 	/* Tell the I2C layer a new client has arrived */
> 	if ((err = i2c_attach_client(new_client)))
> 		goto exit_free;
> 
> 	/* Initialize the LM75 chip */
> 	adm1030_init_client(new_client);
> 	
> 	/* Register sysfs hooks */
> 	device_create_file(&new_client->dev, &dev_attr_temp_local_in);
> 	device_create_file(&new_client->dev, &dev_attr_temp_remote_in);
> 	device_create_file(&new_client->dev, &dev_attr_fan_rpm);
> 
> 	/* Following entries are for debugging purpose only, commented
> 	 * out. These are access to HW configuration registers. 
> 	 */
> 	/*
> 	  device_create_file(&new_client->dev, &dev_attr_conf1);
> 	  device_create_file(&new_client->dev, &dev_attr_conf2);
> 	  device_create_file(&new_client->dev, &dev_attr_ftac);
> 	*/
> 	device_create_file(&new_client->dev, &dev_attr_pwm);
> 
> 	device_create_file(&new_client->dev, &dev_attr_temp_local_min);
> 	device_create_file(&new_client->dev, &dev_attr_temp_local_range);
> 	device_create_file(&new_client->dev, &dev_attr_temp_remote_min);
> 	device_create_file(&new_client->dev, &dev_attr_temp_remote_range);
> 
> 	return 0;
> 
> exit_free:
> 	kfree(new_client);
> exit:
> 	return err;
> }
> 
> static int adm1030_detach_client(struct i2c_client *client)
> {
> 	i2c_detach_client(client);
> 	kfree(client);
> 	return 0;
> }
> 
> static int adm1030_read_value(struct i2c_client *client, u8 reg)
> {
>   return i2c_smbus_read_byte_data(client, reg);
> }
> 
> static int adm1030_write_value(struct i2c_client *client, u8 reg, u16
> value)
> {
>   return i2c_smbus_write_byte_data(client, reg, value);
> }
> 
> static void adm1030_init_client(struct i2c_client *client)
> {
>   int read_val;
> 	/* Initialize the ADM1030 chip (enables fan speed reading )*/
> 	adm1030_write_value(client, ADM1030_REG_CONF2, 0x3f);
> 
> 	/* Increase the accuracy on rpm measurements */
> 	read_val = adm1030_read_value(client, ADM1030_REG_FCH);
> 	adm1030_write_value(client, ADM1030_REG_FCH, 0xc0 | read_val);
> 
> }
> 
> static struct adm1030_data *adm1030_update_device(struct device *dev)
> {
> 	struct i2c_client *client = to_i2c_client(dev);
> 	struct adm1030_data *data = i2c_get_clientdata(client);
> 	int temp_range;
> 	static unsigned short values[5] = {5, 10, 20, 40, 80};
> 	
> 	down(&data->update_lock);
> 
> 	if ((jiffies - data->last_updated > HZ + HZ / 2) ||
> 	    (jiffies < data->last_updated) || !data->valid) {
> 		dev_dbg(&client->dev, "Starting adm1030 update\n");
> 
> 		data->temp_local_in = adm1030_read_value(client,
> ADM1030_REG_TEMP_INT);
> 		data->temp_remote_in = adm1030_read_value(client,
> ADM1030_REG_TEMP_EXT);
> 		data->fan_rpm      = adm1030_read_value(client,
> 							  ADM1030_REG_FAN_SPEED);
> 		temp_range = adm1030_read_value(client,	ADM1030_REG_LTR);
> 		data->temp_local_min = ((temp_range >> 3) & 0x1f) * 4;
> 		data->temp_local_range = (values[temp_range&0x7]);
> 
> 		temp_range = adm1030_read_value(client,	ADM1030_REG_RTR);
> 		data->temp_remote_min = ((temp_range >> 3) & 0x1f) * 4;
> 		data->temp_remote_range = (values[temp_range&0x7]);
> 
> 		data->conf1 = adm1030_read_value(client, ADM1030_REG_CONF1);
> 		data->conf2 = adm1030_read_value(client, ADM1030_REG_CONF2);
> 		data->ftac      = adm1030_read_value(client, ADM1030_REG_FTAC);
> 		data->pwm      = 0xf & adm1030_read_value(client, ADM1030_REG_FSP);
> 		data->last_updated = jiffies;
> 		data->valid = 1;
> 	}
> 
> 	up(&data->update_lock);
> 
> 	return data;
> }
> 
> static int __init sensors_adm1030_init(void)
> {
> 	return i2c_add_driver(&adm1030_driver);
> }
> 
> static void __exit sensors_adm1030_exit(void)
> {
> 	i2c_del_driver(&adm1030_driver);
> }
> 
> MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
> MODULE_DESCRIPTION("ADM1030 driver");
> MODULE_LICENSE("GPL");
> 
> module_init(sensors_adm1030_init);
> module_exit(sensors_adm1030_exit);
> +++++++++++++++++++++++++ end adm1030.c ++++++++++++++++++++++++++++++++
> 
> 
> +++++++++++++++++++++++++++++ adm1030.h ++++++++++++++++++++++++++++++++
> /*
>     adm1030.h - Part of lm_sensors, Linux kernel modules for hardware
>              monitoring
>     Copyright (c) 2004 Alexandre d'Alton <alex@alexdalton.org>
> 
>     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.
> */
> 
> /*
>     This file contains common code for encoding/decoding LM75 type
>     temperature readings, which are emulated by many of the chips
>     we support.  As the user is unlikely to load more than one driver
>     which contains this code, we don't worry about the wasted space.
> */
> 
> #include <linux/i2c-sensor.h>
> 
> /* Many ADM1030 constants specified below */
> /* The ADM1030 registers */
> /* Configuration register 1 */
> #define ADM1030_REG_CONF1		0x00
> #define ADM1030_CONF1_MEN               0x01 /* monitoring enable */
> #define ADM1030_CONF1_IEN               0x02 /* INT enable */
> #define ADM1030_CONF1_TMODE             0x04 /* Tach/Ain mode */
> #define ADM1030_CONF1_PINV              0x08 /* PWM Invert */
> #define ADM1030_CONF1_FFE               0x10 /* Fan Fault Enable */
> #define ADM1030_CONF1_PMODE             0x60 /* PWM Mode 00 = Remote
> 						control Fan (Select
> 						PWM when manual) 11 > 						Fastest Calc speed
> 						controls fan (Select
> 						RPM when manual)*/
> #define ADM1030_CONF1_AEN               0x80 /* Auto / SW Control */
> 
> /* Configuration register 2 */
> #define ADM1030_REG_CONF2		0x01
> #define ADM1030_CONF2_PEN               0x01 /* PWM1 Enable */
> #define ADM1030_CONF2_UN1               0x02 /* Unused */
> #define ADM1030_CONF2_TIEN              0x04 /* TACH input Enable  */
> #define ADM1030_CONF2_UN2               0x08 /* Unused */
> #define ADM1030_CONF2_LTE               0x10 /* Local temp enable */
> #define ADM1030_CONF2_RTE               0x20 /* Remote temp enable */
> #define ADM1030_CONF2_UN3               0x40 /* Unused */
> #define ADM1030_CONF2_SWR               0x80 /* SW Reset */
> 
> /* STATUS register 1 */
> #define ADM1030_REG_STAT1		0x02
> #define ADM1030_STAT1_ASP               0x01 /* Alarm Speed  */
> #define ADM1030_STAT1_FF                0x02 /* Fan Fault */
> #define ADM1030_STAT1_RTH               0x04 /* Remote Temp High  */
> #define ADM1030_STAT1_RTL               0x08 /* Remote Temp Low */
> #define ADM1030_STAT1_RTT               0x10 /* Remote Temp Therm */
> #define ADM1030_STAT1_RDE               0x20 /* Remote Diode Error */
> #define ADM1030_STAT1_LTH               0x40 /* Local Temp High */
> #define ADM1030_STAT1_LTL               0x80 /* Loc Temp Low */
> 
> /* STATUS register 2 */
> #define ADM1030_REG_STAT2		0x03
> #define ADM1030_STAT2_UN1               0x3f /* Unused  */
> #define ADM1030_STAT2_LTH               0x40 /* Local Temp Therm */
> #define ADM1030_STAT2_LTL               0x80 /* Therm Status */
> 
> /* Extended Temp register  */
> #define ADM1030_REG_EXT 		0x06
> #define ADM1030_EXT_ASP                 0x07 /* Remote extended temp
> range  */
> #define ADM1030_EXT_LTH                 0x38 /* Unused  */
> #define ADM1030_EXT_LTL                 0xc0 /* Local extended temp
> range */
> 
> /* Fan charasteristic register  */
> #define ADM1030_REG_FCH 		0x20
> #define ADM1030_FCH_FSU                 0x07 /* Fan spin up  */
> #define ADM1030_EXT_PWMF                0x38 /* PWM Freq  */
> #define ADM1030_EXT_SPR                 0xc0 /* Speed range */
> 
> /* Fan Speed register  */
> #define ADM1030_REG_FSP 		0x22
> #define ADM1030_FSP_NSP                 0x0f /* Fan min speed  */
> 
> /* Fan Filter register  */
> #define ADM1030_REG_FLT 		0x23
> #define ADM1030_FLT_SUD                 0x80 /* Spin up disable  */
> #define ADM1030_FLT_RR                  0x60 /* Ramp rate  */
> #define ADM1030_FLT_ASR                 0x1c /* ADC Sample rate  */
> #define ADM1030_FLT_UN1                 0x02 /* Unused  */
> #define ADM1030_FLT_FFE                 0x01 /* Fan Filter enable */
> 
> /* Local Temp Tmin / Trange  */
> #define ADM1030_REG_LTR 		0x24
> /* Remote Temp Tmin / Trange */
> #define ADM1030_REG_RTR 		0x25
> #define ADM1030_TR_TM                  0xf8 /* Local Temp Tmin  */
> #define ADM1030_TR_TR                  0x60 /* Local Temp Trange  */
> 
> /* Therm to Fan  */
> #define ADM1030_REG_TTF 		0x3f
> #define ADM1030_TTF_EN                  0x80 /* Therm to fan enable */
> 
> #define ADM1030_REG_FAN_SPEED   	0x08
> #define ADM1030_REG_FTAC		0x10
> 
> #define ADM1030_REG_TEMP_INT		0x0a
> #define ADM1030_REG_TEMP_EXT		0x0b
> 
> 
> +++++++++++++++++++++++++ end adm1030.h ++++++++++++++++++++++++++++++++
> 
> 
> 

^ permalink raw reply	[flat|nested] 22+ messages in thread

* ADM1030 Driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
                   ` (7 preceding siblings ...)
  2005-05-19  6:24 ` ADM1030 Driver Alexandre d'Alton
@ 2005-05-19  6:24 ` Jean Delvare
  2005-05-19  6:24 ` Alexandre d'Alton
                   ` (11 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: Jean Delvare @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

Hi Alexandre,

See my comments on your ADM1030 driver inline.

> #include <linux/config.h>

You don't need this, do you?

> /* Addresses to scan */
> static unsigned short normal_i2c[] = { I2C_CLIENT_END };
> static unsigned short normal_i2c_range[] = { 0x2e, I2C_CLIENT_END };

That's not a valid range. You want "{ 0x2c, 0x2e, I2C_CLIENT_END };"
according to the datasheet.

> /* Each client has this additional data */
> struct adm1030_data {
>   struct semaphore	update_lock;
>   char			valid;		/* !=0 if following fields are valid */
>   unsigned long		last_updated;	/* In jiffies */
>   u16			temp_local_in;	/* Register values */
>   u16			temp_remote_in;
>   u16			fan_rpm;
>   u16		        conf1;
>   u16			conf2;
>   u16			ftac;
>   u16			pwm;
>   u16                   temp_local_min;
>   u16                   temp_local_range;
>   u16                   temp_remote_min;
>   u16                   temp_remote_range;
> };

This is likely to make the code impossible to refactor. A better
approach IMHO would be:

struct adm1030_data {
  struct semaphore	update_lock;
  char			valid;		/* !=0 if following fields are valid */
  unsigned long		last_updated;	/* In jiffies */
  u16			temp[2];	/* Register values */
  u16                   temp_min[2];
  u16                   temp_range[2];
  u16			fan_rpm;
  u16		        conf1;
  u16			conf2;
  u16			ftac;
  u16			pwm;
};

I guess you get the idea. That way you can hope to have the same code
handling both temperature channels. This becomes even more convenient if
you want to support the ADM1031 driver, which has a second remote
temperature channel. Same of course goes for the voltages and fans once
you start handling them.

This supposes that you define registers a bit differently, see below.

> /* This is the driver that will be inserted */
> static struct i2c_driver adm1030_driver = {
> 	.owner		= THIS_MODULE,
> 	.name		= "adm1030",
> 	.id		= I2C_DRIVERID_ADM1030,

I invite you not to declare any id. It's not needed for normal operation
and will make testing more difficult. It's always possible to add it
later if needed.

> 	.flags		= I2C_DF_NOTIFY,
> 	.attach_adapter	= adm1030_attach_adapter,
> 	.detach_client	= adm1030_detach_client,
> };

> #define show(value)	\
> static ssize_t show_##value(struct device *dev, char *buf)		\
> {									\
> 	struct adm1030_data *data = adm1030_update_device(dev);		\
> 	return sprintf(buf, "%d\n", data->value);	\
> }

%u, not %d.

> show(temp_local_in);
> show(temp_remote_in);
> show(conf1);
> show(conf2);
> show(ftac);
> show(pwm);
> show(temp_local_min);
> show(temp_local_range);
> show(temp_remote_min);
> show(temp_remote_range);

It's probably not correct for temperatures. According to
Documentation/i2c/sysfs-interface, temperatures are exported in
millidegrees Celcius.

Also, we usually do not export configuration registers to user-space.

What is ftac?

> static ssize_t show_fan_rpm(struct device *dev, char *buf)		
> {									
>   int fan_speed;
>   struct adm1030_data *data = adm1030_update_device(dev);		
>   fan_speed = (11250 * 60) / (data->fan_rpm * 8);
>   return sprintf(buf, "%d\n", fan_speed);	
> }

Is that "* 8" a hardcoded fan divisor? Looks bad.

You should define a macro for fan conversions. See w83781d.c for
example. More generally, you should follow w83781d or lm78.c instead of
lm75.c for examples, since these devices are more similar to the
adm1030.

> void set_temp_min(struct i2c_client *client, int value, int reg)
> {
>   int val = adm1030_read_value(client, reg);
>   val = ( ( ( value >> 2 ) & 0x1f ) << 3 ) | ( val & 0x7 ); 
>   adm1030_write_value(client, reg, val);
> }
> void set_temp_range(struct i2c_client *client, int value, int reg)
> {
>   int val = adm1030_read_value(client, reg);
>   switch (value) {
>   case 5: value = 0; break;
>   case 10: value = 1; break;
>   case 20: value = 2; break;
>   case 40: value = 3; break;
>   case 80: value = 4; break;
>   default: return;
>   }
>   val = (val&0xf8) | (value & 0x7); 
>   adm1030_write_value(client, reg, val);
> }

These functions should not return void. And they should return -1 in
case of error.

> static ssize_t set_temp_local_min(struct device *dev, const char *buf,
> size_t
> 				  count) 
> {
>   struct i2c_client * client = to_i2c_client(dev);
>   int temp = simple_strtoul(buf, NULL, 10);		
>   set_temp_min(client, temp, ADM1030_REG_LTR);
>   return count;
> }
> static ssize_t set_temp_local_range(struct device *dev, const char
> *buf, size_t
> 				  count) 
> {
>   struct i2c_client * client = to_i2c_client(dev);
>   int temp = simple_strtoul(buf, NULL, 10);		
>   set_temp_range(client, temp, ADM1030_REG_LTR);
>   return count;
>   
> }
> static ssize_t set_temp_remote_min(struct device *dev, const char
> *buf, size_t
> 				  count) 
> {
>   struct i2c_client * client = to_i2c_client(dev);
>   int temp = simple_strtoul(buf, NULL, 10);		
>   set_temp_min(client, temp, ADM1030_REG_RTR);
>   return count;  
> }
> 
> static ssize_t set_temp_remote_range(struct device *dev, const char
> *buf, size_t
> 				  count) 
> {
>   struct i2c_client * client = to_i2c_client(dev);
>   int temp = simple_strtoul(buf, NULL, 10);		
>   set_temp_range(client, temp, ADM1030_REG_RTR);
>   return count;
>   
> }

This can be considerably simplified (using macros) if you use arrays to
store the values, as adviced above. Again, lm78.c is a good example of
how to do that.

> 
> //static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
> set_temp_max);
> //static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
> show_temp_hyst, set_temp_hyst);
> static DEVICE_ATTR(conf1, S_IWUSR | S_IRUGO, show_conf1, set_conf1);
> static DEVICE_ATTR(conf2, S_IWUSR | S_IRUGO, show_conf2, set_conf2);
> static DEVICE_ATTR(ftac, S_IWUSR | S_IRUGO, show_ftac, set_ftac);
> static DEVICE_ATTR(pwm, S_IWUSR | S_IRUGO, show_pwm, set_pwm);
> static DEVICE_ATTR(temp_local_in, S_IRUGO, show_temp_local_in, NULL);
> static DEVICE_ATTR(temp_remote_in, S_IRUGO, show_temp_remote_in,
> NULL); static DEVICE_ATTR(fan_rpm, S_IRUGO , show_fan_rpm, NULL);
> static DEVICE_ATTR(temp_local_min, S_IRUGO | S_IWUSR, 
> 		   show_temp_local_min, set_temp_local_min);
> static DEVICE_ATTR(temp_local_range, S_IRUGO | S_IWUSR, 
> 		   show_temp_local_range, set_temp_local_range);
> static DEVICE_ATTR(temp_remote_min, S_IRUGO | S_IWUSR, 
> 		   show_temp_remote_min, set_temp_remote_min);
> static DEVICE_ATTR(temp_remote_range, S_IRUGO | S_IWUSR, 
> 		   show_temp_remote_range, set_temp_remote_range);

Names don't follow the sysfs-interface convention at all! Please
correct.

> static int adm1030_detect(struct i2c_adapter *adapter, int address,
> int kind)
> (...)
> 	/* Make sure we aren't probing the ISA bus!! This is just a
> 	safety
> check
> 	   at this moment; i2c_detect really won't call us. */
> #ifdef DEBUG
> 	if (i2c_is_isa_adapter(adapter)) {
> 		dev_dbg(&adapter->dev,
> 			"adm1030_detect called for an ISA bus adapter?!?\n");
> 		goto exit;
> 	}
> #endif

You can drop that test, it's useless.

> 	/* Now, we do the remaining detection. It is lousy. */

No, it is not lousy. The detection of the LM75 is, not yours, so the comment doesn't apply.

> 	if (kind < 0) {
> 		id = i2c_smbus_read_byte_data(new_client, 0x3d);
> 		co = i2c_smbus_read_byte_data(new_client, 0x3e);

These could be local variables.

> static int adm1030_detach_client(struct i2c_client *client)
> {
> 	i2c_detach_client(client);

Please check on error returned by i2c_detach_client. lm75.c doesn't, but
it's bad. See any other driver for the correct way to handle it.

> static void adm1030_init_client(struct i2c_client *client)
> {
>   int read_val;
> 	/* Initialize the ADM1030 chip (enables fan speed reading )*/
> 	adm1030_write_value(client, ADM1030_REG_CONF2, 0x3f);

Looks a bit agressive, but I would need to read the datasheet.

> 	/* Increase the accuracy on rpm measurements */
> 	read_val = adm1030_read_value(client, ADM1030_REG_FCH);
> 	adm1030_write_value(client, ADM1030_REG_FCH, 0xc0 | read_val);
> 
> }
> 
> static struct adm1030_data *adm1030_update_device(struct device *dev)
> {
> 	struct i2c_client *client = to_i2c_client(dev);
> 	struct adm1030_data *data = i2c_get_clientdata(client);
> 	int temp_range;
> 	static unsigned short values[5] = {5, 10, 20, 40, 80};

This can be computed (5 * (x << n)).


HEADER:


> #include <linux/i2c-sensor.h>

You don't need this, do you?

> #define ADM1030_REG_TEMP_INT		0x0a
> #define ADM1030_REG_TEMP_EXT		0x0b

For example you can #define ADM1031_REG_TEMP(nr)	(0x09 + (nr)).

Same for the rest. That way you should be able to treat most values
regardless of the channel number, which makes the code shorted and more
readable.

This was simply a first pass, more later if I find some time.

Thanks.

-- 
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* ADM1030 Driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
                   ` (6 preceding siblings ...)
  2005-05-19  6:24 ` Cedric Pradalier
@ 2005-05-19  6:24 ` Alexandre d'Alton
  2005-05-19  6:24 ` Jean Delvare
                   ` (12 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: Alexandre d'Alton @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

Jean,

The comments are meaningfull, I'll rework the code and send an update
soon.

Thanks for your time.

Alex.

On Wed, 2004-04-07 at 23:35, Jean Delvare wrote:
> Hi Alexandre,
> 
> See my comments on your ADM1030 driver inline.
> 
> > #include <linux/config.h>
> 
> You don't need this, do you?
> 
> > /* Addresses to scan */
> > static unsigned short normal_i2c[] = { I2C_CLIENT_END };
> > static unsigned short normal_i2c_range[] = { 0x2e, I2C_CLIENT_END };
> 
> That's not a valid range. You want "{ 0x2c, 0x2e, I2C_CLIENT_END };"
> according to the datasheet.
> 
> > /* Each client has this additional data */
> > struct adm1030_data {
> >   struct semaphore	update_lock;
> >   char			valid;		/* !=0 if following fields are valid */
> >   unsigned long		last_updated;	/* In jiffies */
> >   u16			temp_local_in;	/* Register values */
> >   u16			temp_remote_in;
> >   u16			fan_rpm;
> >   u16		        conf1;
> >   u16			conf2;
> >   u16			ftac;
> >   u16			pwm;
> >   u16                   temp_local_min;
> >   u16                   temp_local_range;
> >   u16                   temp_remote_min;
> >   u16                   temp_remote_range;
> > };
> 
> This is likely to make the code impossible to refactor. A better
> approach IMHO would be:
> 
> struct adm1030_data {
>   struct semaphore	update_lock;
>   char			valid;		/* !=0 if following fields are valid */
>   unsigned long		last_updated;	/* In jiffies */
>   u16			temp[2];	/* Register values */
>   u16                   temp_min[2];
>   u16                   temp_range[2];
>   u16			fan_rpm;
>   u16		        conf1;
>   u16			conf2;
>   u16			ftac;
>   u16			pwm;
> };
> 
> I guess you get the idea. That way you can hope to have the same code
> handling both temperature channels. This becomes even more convenient if
> you want to support the ADM1031 driver, which has a second remote
> temperature channel. Same of course goes for the voltages and fans once
> you start handling them.
> 
> This supposes that you define registers a bit differently, see below.
> 
> > /* This is the driver that will be inserted */
> > static struct i2c_driver adm1030_driver = {
> > 	.owner		= THIS_MODULE,
> > 	.name		= "adm1030",
> > 	.id		= I2C_DRIVERID_ADM1030,
> 
> I invite you not to declare any id. It's not needed for normal operation
> and will make testing more difficult. It's always possible to add it
> later if needed.
> 
> > 	.flags		= I2C_DF_NOTIFY,
> > 	.attach_adapter	= adm1030_attach_adapter,
> > 	.detach_client	= adm1030_detach_client,
> > };
> 
> > #define show(value)	\
> > static ssize_t show_##value(struct device *dev, char *buf)		\
> > {									\
> > 	struct adm1030_data *data = adm1030_update_device(dev);		\
> > 	return sprintf(buf, "%d\n", data->value);	\
> > }
> 
> %u, not %d.
> 
> > show(temp_local_in);
> > show(temp_remote_in);
> > show(conf1);
> > show(conf2);
> > show(ftac);
> > show(pwm);
> > show(temp_local_min);
> > show(temp_local_range);
> > show(temp_remote_min);
> > show(temp_remote_range);
> 
> It's probably not correct for temperatures. According to
> Documentation/i2c/sysfs-interface, temperatures are exported in
> millidegrees Celcius.
> 
> Also, we usually do not export configuration registers to user-space.
> 
> What is ftac?
> 
> > static ssize_t show_fan_rpm(struct device *dev, char *buf)		
> > {									
> >   int fan_speed;
> >   struct adm1030_data *data = adm1030_update_device(dev);		
> >   fan_speed = (11250 * 60) / (data->fan_rpm * 8);
> >   return sprintf(buf, "%d\n", fan_speed);	
> > }
> 
> Is that "* 8" a hardcoded fan divisor? Looks bad.
> 
> You should define a macro for fan conversions. See w83781d.c for
> example. More generally, you should follow w83781d or lm78.c instead of
> lm75.c for examples, since these devices are more similar to the
> adm1030.
> 
> > void set_temp_min(struct i2c_client *client, int value, int reg)
> > {
> >   int val = adm1030_read_value(client, reg);
> >   val = ( ( ( value >> 2 ) & 0x1f ) << 3 ) | ( val & 0x7 ); 
> >   adm1030_write_value(client, reg, val);
> > }
> > void set_temp_range(struct i2c_client *client, int value, int reg)
> > {
> >   int val = adm1030_read_value(client, reg);
> >   switch (value) {
> >   case 5: value = 0; break;
> >   case 10: value = 1; break;
> >   case 20: value = 2; break;
> >   case 40: value = 3; break;
> >   case 80: value = 4; break;
> >   default: return;
> >   }
> >   val = (val&0xf8) | (value & 0x7); 
> >   adm1030_write_value(client, reg, val);
> > }
> 
> These functions should not return void. And they should return -1 in
> case of error.
> 
> > static ssize_t set_temp_local_min(struct device *dev, const char *buf,
> > size_t
> > 				  count) 
> > {
> >   struct i2c_client * client = to_i2c_client(dev);
> >   int temp = simple_strtoul(buf, NULL, 10);		
> >   set_temp_min(client, temp, ADM1030_REG_LTR);
> >   return count;
> > }
> > static ssize_t set_temp_local_range(struct device *dev, const char
> > *buf, size_t
> > 				  count) 
> > {
> >   struct i2c_client * client = to_i2c_client(dev);
> >   int temp = simple_strtoul(buf, NULL, 10);		
> >   set_temp_range(client, temp, ADM1030_REG_LTR);
> >   return count;
> >   
> > }
> > static ssize_t set_temp_remote_min(struct device *dev, const char
> > *buf, size_t
> > 				  count) 
> > {
> >   struct i2c_client * client = to_i2c_client(dev);
> >   int temp = simple_strtoul(buf, NULL, 10);		
> >   set_temp_min(client, temp, ADM1030_REG_RTR);
> >   return count;  
> > }
> > 
> > static ssize_t set_temp_remote_range(struct device *dev, const char
> > *buf, size_t
> > 				  count) 
> > {
> >   struct i2c_client * client = to_i2c_client(dev);
> >   int temp = simple_strtoul(buf, NULL, 10);		
> >   set_temp_range(client, temp, ADM1030_REG_RTR);
> >   return count;
> >   
> > }
> 
> This can be considerably simplified (using macros) if you use arrays to
> store the values, as adviced above. Again, lm78.c is a good example of
> how to do that.
> 
> > 
> > //static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
> > set_temp_max);
> > //static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
> > show_temp_hyst, set_temp_hyst);
> > static DEVICE_ATTR(conf1, S_IWUSR | S_IRUGO, show_conf1, set_conf1);
> > static DEVICE_ATTR(conf2, S_IWUSR | S_IRUGO, show_conf2, set_conf2);
> > static DEVICE_ATTR(ftac, S_IWUSR | S_IRUGO, show_ftac, set_ftac);
> > static DEVICE_ATTR(pwm, S_IWUSR | S_IRUGO, show_pwm, set_pwm);
> > static DEVICE_ATTR(temp_local_in, S_IRUGO, show_temp_local_in, NULL);
> > static DEVICE_ATTR(temp_remote_in, S_IRUGO, show_temp_remote_in,
> > NULL); static DEVICE_ATTR(fan_rpm, S_IRUGO , show_fan_rpm, NULL);
> > static DEVICE_ATTR(temp_local_min, S_IRUGO | S_IWUSR, 
> > 		   show_temp_local_min, set_temp_local_min);
> > static DEVICE_ATTR(temp_local_range, S_IRUGO | S_IWUSR, 
> > 		   show_temp_local_range, set_temp_local_range);
> > static DEVICE_ATTR(temp_remote_min, S_IRUGO | S_IWUSR, 
> > 		   show_temp_remote_min, set_temp_remote_min);
> > static DEVICE_ATTR(temp_remote_range, S_IRUGO | S_IWUSR, 
> > 		   show_temp_remote_range, set_temp_remote_range);
> 
> Names don't follow the sysfs-interface convention at all! Please
> correct.
> 
> > static int adm1030_detect(struct i2c_adapter *adapter, int address,
> > int kind)
> > (...)
> > 	/* Make sure we aren't probing the ISA bus!! This is just a
> > 	safety
> > check
> > 	   at this moment; i2c_detect really won't call us. */
> > #ifdef DEBUG
> > 	if (i2c_is_isa_adapter(adapter)) {
> > 		dev_dbg(&adapter->dev,
> > 			"adm1030_detect called for an ISA bus adapter?!?\n");
> > 		goto exit;
> > 	}
> > #endif
> 
> You can drop that test, it's useless.
> 
> > 	/* Now, we do the remaining detection. It is lousy. */
> 
> No, it is not lousy. The detection of the LM75 is, not yours, so the comment doesn't apply.
> 
> > 	if (kind < 0) {
> > 		id = i2c_smbus_read_byte_data(new_client, 0x3d);
> > 		co = i2c_smbus_read_byte_data(new_client, 0x3e);
> 
> These could be local variables.
> 
> > static int adm1030_detach_client(struct i2c_client *client)
> > {
> > 	i2c_detach_client(client);
> 
> Please check on error returned by i2c_detach_client. lm75.c doesn't, but
> it's bad. See any other driver for the correct way to handle it.
> 
> > static void adm1030_init_client(struct i2c_client *client)
> > {
> >   int read_val;
> > 	/* Initialize the ADM1030 chip (enables fan speed reading )*/
> > 	adm1030_write_value(client, ADM1030_REG_CONF2, 0x3f);
> 
> Looks a bit agressive, but I would need to read the datasheet.
> 
> > 	/* Increase the accuracy on rpm measurements */
> > 	read_val = adm1030_read_value(client, ADM1030_REG_FCH);
> > 	adm1030_write_value(client, ADM1030_REG_FCH, 0xc0 | read_val);
> > 
> > }
> > 
> > static struct adm1030_data *adm1030_update_device(struct device *dev)
> > {
> > 	struct i2c_client *client = to_i2c_client(dev);
> > 	struct adm1030_data *data = i2c_get_clientdata(client);
> > 	int temp_range;
> > 	static unsigned short values[5] = {5, 10, 20, 40, 80};
> 
> This can be computed (5 * (x << n)).
> 
> 
> HEADER:
> 
> 
> > #include <linux/i2c-sensor.h>
> 
> You don't need this, do you?
> 
> > #define ADM1030_REG_TEMP_INT		0x0a
> > #define ADM1030_REG_TEMP_EXT		0x0b
> 
> For example you can #define ADM1031_REG_TEMP(nr)	(0x09 + (nr)).
> 
> Same for the rest. That way you should be able to treat most values
> regardless of the channel number, which makes the code shorted and more
> readable.
> 
> This was simply a first pass, more later if I find some time.
> 
> Thanks.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* ADM1030 Driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
                   ` (10 preceding siblings ...)
  2005-05-19  6:24 ` Alexandre d'Alton
@ 2005-05-19  6:24 ` Alexandre d'Alton
  2005-05-19  6:24 ` Alexandre d'Alton
                   ` (8 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: Alexandre d'Alton @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

All,

Updated version of the ADM1030 driver according to Jean's remarks.

	- removed adm1030.h header file
	- removed unused code.
	- cleaned registers accesses
	- try to respect the sysfs naming conventions.
	- renamed temp?_min to temp?_amin
	- added temp?_min, temp?_max and temp?_crit access

Thanks for the feedbacks.

Alex.




/*
    adm1030.c - Part of lm_sensors, Linux kernel modules for hardware
             monitoring
    Based on lm75.c
    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
    Copyright (c) 2004 Alexandre d'Alton <alex@alexdalton.org>

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


/* Many ADM1030 constants specified below */
#define ADM1030_CONF1_MEN               0x01 /* monitoring enable */
#define ADM1030_CONF1_AEN               0x80 /* Auto / SW Control */

#define ADM1030_REG_FAN_SPEED(nr)   	(0x08 + (nr))

#define ADM1030_REG_FAN_DIV(nr)		(0x20  + (nr))
#define ADM1030_REG_FTAC(nr)		(0x10 + (nr))
#define ADM1030_REG_FSP(nr)		(0x22 + (nr))


#define ADM1030_REG_TEMP_HLM(nr)		(0x14  + 4*(nr))
#define ADM1030_REG_TEMP_LLM(nr)		(0x15  + 4*(nr))
#define ADM1030_REG_TEMP_TLM(nr)		(0x16  + 4*(nr))

/* Temperature values (nr = 1 (local) / 2 (remote)) */
#define ADM1030_REG_TEMP(nr)		        (0xa + (nr))
#define ADM1030_REG_TEMP_RNG(nr)		(0x24 + (nr))

/* Temperature values (nr = 1 (local) / 2 (remote)) */
#define ADM1030_REG_CONF(nr)		        (nr)


/* Addresses to scan */
static unsigned short normal_i2c[] = { I2C_CLIENT_END };
static unsigned short normal_i2c_range[] = { 0x2c, 0x2e, I2C_CLIENT_END
};
static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };

/* Insmod parameters */
SENSORS_INSMOD_1(adm1030);

static u16 adm1030_fan_div = 1;
 
/* Each client has this additional data */
struct adm1030_data {
  struct semaphore	update_lock;
  char			valid;		/* !=0 if following fields are valid */
  unsigned int		last_updated;	/* In jiffies */
  unsigned int		fan_input[1];
  unsigned int	       	fan_div[1];
  unsigned int	       	pwm[1];
  unsigned int          temp_input[2];
  unsigned int          temp_min[2];
  unsigned int          temp_range[2];
  unsigned int          temp_low_limit[2];
  unsigned int          temp_high_limit[2];
  unsigned int          temp_therm_limit[2];
};

static int adm1030_attach_adapter(struct i2c_adapter *adapter);
static int adm1030_detect(struct i2c_adapter *adapter, int address, int
kind);
static void adm1030_init_client(struct i2c_client *client);
static int adm1030_detach_client(struct i2c_client *client);
static int adm1030_read_value(struct i2c_client *client, u8 reg);
static int adm1030_write_value(struct i2c_client *client, u8 reg,
unsigned int value);
static struct adm1030_data *adm1030_update_device(struct device *dev);


/* This is the driver that will be inserted */
static struct i2c_driver adm1030_driver = {
	.owner		= THIS_MODULE,
	.name		= "adm1030",
	.flags		= I2C_DF_NOTIFY,
	.attach_adapter	= adm1030_attach_adapter,
	.detach_client	= adm1030_detach_client,
};

static int adm1030_id = 0;

/* These macros converts registers values to sysfs-sensors compliant
 * values
 */
#define to_display_temp_low_limit(reg) ((reg) * 1000)
#define to_display_temp_high_limit(reg) ((reg) * 1000)
#define to_display_temp_therm_limit(reg) ((reg) * 1000)
#define to_display_temp_input(reg) ((reg) * 1000)
#define to_display_temp_min(reg)   (1000 * ((((reg) >> 3) & 0x1f) << 2))
#define to_display_temp_range(reg) (5000 * (1<<((reg)&0x07)))
#define to_display_fan_input(reg)  ((reg) ? (11250 * 60) / ((reg)
*               \
						    adm1030_fan_div) : 0)
#define to_display_fan_div(reg)    (1 << (((reg) & 0xc0)>>6))
#define to_display_pwm(reg)        ((reg)<<4)

/* These macros converts sysfs-sensors compliant values into
 * corresponding register value.
 */
#define to_reg_temp_min(reg, val) ((((val)/500) & 0xf8)|((reg) & 0x7))
#define to_reg_temp_range(reg, val) (((reg) & 0xf8) | \
(((val)<10000 ? 0 :                                   \
  (val)<20000 ? 1 :                                   \
  (val)<40000 ? 2 :                                   \
  (val)<80000 ? 3 : 4) & 7))
static inline int to_reg_fan_div(unsigned int reg, unsigned int val) 
{
  adm1030_fan_div = val;
  return ((reg & 0x3f) | 
	  ((val = 8 ? 0xc0 :                                            
	    val = 4 ? 0x80 :                                                
	    val = 2 ? 0x40 :                                                
	    val = 1 ? 0x00 : reg & 0xc0))); 
}
#define to_reg_pwm(reg, val) ((val) >> 4)
#define to_reg_temp_low_limit(reg, val) ((val) / 1000)
#define to_reg_temp_high_limit(reg, val) ((val) / 1000)
#define to_reg_temp_therm_limit(reg, val) ((val) / 1000)

#define show(value, nr)	\
static ssize_t show_##value##_##nr(struct device *dev, char *buf)		\
{									\
	struct adm1030_data *data = adm1030_update_device(dev);		\
	return sprintf(buf, "%u\n", to_display_##value(data->value[(nr)-1]));	\
}
show(pwm, 1);
show(temp_min, 1);
show(temp_min, 2);
show(temp_low_limit, 1);
show(temp_low_limit, 2);
show(temp_high_limit, 1);
show(temp_high_limit, 2);
show(temp_therm_limit, 1);
show(temp_therm_limit, 2);
show(temp_range, 1);
show(temp_range, 2);
show(temp_input, 1);
show(temp_input, 2);
show(fan_input, 1);
show(fan_div, 1);

#define set(value, reg, nr)	\
static ssize_t set_##value##_##nr(struct device *dev, const char *buf,
size_t count)	\
{								                \
	struct i2c_client *client = to_i2c_client(dev);		                \
	int temp = simple_strtoul(buf, NULL, 10);		                \
        int old_val = adm1030_read_value(client,
reg((nr)-1));                          \
	adm1030_write_value(client, reg((nr)-1), to_reg_##value(old_val,
temp));	\
	return count;						                \
}
set(temp_min,   ADM1030_REG_TEMP_RNG, 1);
set(temp_min,   ADM1030_REG_TEMP_RNG, 2);
set(temp_range, ADM1030_REG_TEMP_RNG, 1);
set(temp_range, ADM1030_REG_TEMP_RNG, 2);
set(pwm,        ADM1030_REG_FSP,      1);
set(fan_div,    ADM1030_REG_FAN_DIV,  1);

set(temp_low_limit,     ADM1030_REG_TEMP_LLM, 1);
set(temp_high_limit,    ADM1030_REG_TEMP_HLM, 1);
set(temp_therm_limit,   ADM1030_REG_TEMP_TLM, 1);
set(temp_low_limit,     ADM1030_REG_TEMP_LLM, 2);
set(temp_high_limit,    ADM1030_REG_TEMP_HLM, 2);
set(temp_therm_limit,   ADM1030_REG_TEMP_TLM, 2);

static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input_1, NULL);
static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input_2, NULL);

static DEVICE_ATTR(pwm,         S_IWUSR | S_IRUGO, show_pwm_1,
		   set_pwm_1);
static DEVICE_ATTR(fan_div,     S_IRUGO | S_IWUSR, show_fan_div_1,
		   set_fan_div_1);
static DEVICE_ATTR(fan_input,   S_IRUGO          , show_fan_input_1,   
		   NULL);
static DEVICE_ATTR(temp1_min,   S_IRUGO | S_IWUSR,
show_temp_low_limit_1,    
		   set_temp_low_limit_1);
static DEVICE_ATTR(temp1_max,   S_IRUGO | S_IWUSR,
show_temp_high_limit_1,    
		   set_temp_high_limit_1);
static DEVICE_ATTR(temp1_crit,   S_IRUGO | S_IWUSR,
show_temp_therm_limit_1,    
		   set_temp_therm_limit_1);
static DEVICE_ATTR(temp2_min,   S_IRUGO | S_IWUSR,
show_temp_low_limit_2,    
		   set_temp_low_limit_2);
static DEVICE_ATTR(temp2_max,   S_IRUGO | S_IWUSR,
show_temp_high_limit_2,    
		   set_temp_high_limit_2);
static DEVICE_ATTR(temp2_crit,   S_IRUGO | S_IWUSR,
show_temp_therm_limit_2,    
		   set_temp_therm_limit_2);
static DEVICE_ATTR(temp1_amin,   S_IRUGO | S_IWUSR, show_temp_min_1,    
		   set_temp_min_1);
static DEVICE_ATTR(temp1_range, S_IRUGO | S_IWUSR, show_temp_range_1, 
		   set_temp_range_1);
static DEVICE_ATTR(temp2_amin,   S_IRUGO | S_IWUSR, show_temp_min_2,    
		   set_temp_min_2);
static DEVICE_ATTR(temp2_range, S_IRUGO | S_IWUSR, show_temp_range_2,  
		   set_temp_range_2);

static int adm1030_attach_adapter(struct i2c_adapter *adapter)
{
	if (!(adapter->class & I2C_ADAP_CLASS_SMBUS))
		return 0;
	return i2c_detect(adapter, &addr_data, adm1030_detect);
}

/* This function is called by i2c_detect */
static int adm1030_detect(struct i2c_adapter *adapter, int address, int
kind)
{
	struct i2c_client *new_client;
	struct adm1030_data *data;
	int err = 0;
	const char *name = "";

	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
				     I2C_FUNC_SMBUS_WORD_DATA))
		goto exit;

	if (!(new_client = kmalloc(sizeof(struct i2c_client) +
				   sizeof(struct adm1030_data),
				   GFP_KERNEL))) {
		err = -ENOMEM;
		goto exit;
	}
	memset(new_client, 0x00, sizeof(struct i2c_client) +
				 sizeof(struct adm1030_data));

	data = (struct adm1030_data *) (new_client + 1);
	i2c_set_clientdata(new_client, data);
	new_client->addr = address;
	new_client->adapter = adapter;
	new_client->driver = &adm1030_driver;
	new_client->flags = 0;

	/* Now, we do the remaining detection. It is lousy. */
	if (kind < 0) {
	  int id, co;
	  id = i2c_smbus_read_byte_data(new_client, 0x3d);
	  co = i2c_smbus_read_byte_data(new_client, 0x3e);
	  
	  if((id != 0x30) && (co != 0x41))
	    goto exit_free;
	}

	/* Determine the chip type - only one kind supported! */
	if (kind <= 0)
		kind = adm1030;

	if (kind = adm1030) {
		name = "adm1030";
	}

	/* Fill in the remaining client fields and put it into the global list
*/
	strlcpy(new_client->name, name, I2C_NAME_SIZE);

	new_client->id = adm1030_id++;
	data->valid = 0;
	init_MUTEX(&data->update_lock);

	/* Tell the I2C layer a new client has arrived */
	if ((err = i2c_attach_client(new_client)))
		goto exit_free;

	/* Initialize the LM75 chip */
	adm1030_init_client(new_client);
	
	/* Register sysfs hooks */
	device_create_file(&new_client->dev, &dev_attr_temp1_input);
	device_create_file(&new_client->dev, &dev_attr_temp2_input);
	device_create_file(&new_client->dev, &dev_attr_fan_input);
	device_create_file(&new_client->dev, &dev_attr_fan_div);
	device_create_file(&new_client->dev, &dev_attr_pwm);
	device_create_file(&new_client->dev, &dev_attr_temp1_amin);
	device_create_file(&new_client->dev, &dev_attr_temp1_range);
	device_create_file(&new_client->dev, &dev_attr_temp1_min);
	device_create_file(&new_client->dev, &dev_attr_temp1_max);
	device_create_file(&new_client->dev, &dev_attr_temp1_crit);
	device_create_file(&new_client->dev, &dev_attr_temp2_amin);
	device_create_file(&new_client->dev, &dev_attr_temp2_range);
	device_create_file(&new_client->dev, &dev_attr_temp2_min);
	device_create_file(&new_client->dev, &dev_attr_temp2_max);
	device_create_file(&new_client->dev, &dev_attr_temp2_crit);

	return 0;

exit_free:
	kfree(new_client);
exit:
	return err;
}

static int adm1030_detach_client(struct i2c_client *client)
{
	i2c_detach_client(client);
	kfree(client);
	return 0;
}

static int adm1030_read_value(struct i2c_client *client, u8 reg)
{
  return i2c_smbus_read_byte_data(client, reg);
}

static int adm1030_write_value(struct i2c_client *client, u8 reg,
unsigned int value)
{
  return i2c_smbus_write_byte_data(client, reg, value);
}

static void adm1030_init_client(struct i2c_client *client)
{
  unsigned int read_val;
  /* Initialize the ADM1030 chip (enables fan speed reading )*/
  read_val = adm1030_read_value(client, ADM1030_REG_CONF(0));
  adm1030_write_value(client, ADM1030_REG_CONF(0), read_val |
		      ADM1030_CONF1_MEN);
  adm1030_fan_div = 
    to_display_fan_div(adm1030_read_value(client,
ADM1030_REG_FAN_DIV(0)));    

}

static struct adm1030_data *adm1030_update_device(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1030_data *data = i2c_get_clientdata(client);
	int chan;

	down(&data->update_lock);

	if ((jiffies - data->last_updated > HZ + HZ / 2) ||
	    (jiffies < data->last_updated) || !data->valid) {
		dev_dbg(&client->dev, "Starting adm1030 update\n");
		
		for (chan = 0; chan < 2; chan++){
		  data->temp_input[chan] 		    adm1030_read_value(client, 
				       ADM1030_REG_TEMP(chan));
		  data->temp_range[chan] = data->temp_min[chan] 		    adm1030_read_value(client, 
				       ADM1030_REG_TEMP_RNG(chan));
		  data->temp_low_limit[chan] = 
		    adm1030_read_value(client, 
				       ADM1030_REG_TEMP_LLM(chan));
		  data->temp_high_limit[chan] = 
		    adm1030_read_value(client, 
				       ADM1030_REG_TEMP_HLM(chan));
		  data->temp_therm_limit[chan] = 
		    adm1030_read_value(client, 
				       ADM1030_REG_TEMP_TLM(chan));

		}
		
		data->fan_div[0]   = 
		  adm1030_read_value(client,
				     ADM1030_REG_FAN_DIV(0)); 
		data->fan_input[0] 		  adm1030_read_value(client,
				     ADM1030_REG_FAN_SPEED(0));
		data->pwm[0]       = 0xf & adm1030_read_value(client,
ADM1030_REG_FSP(0));
		data->last_updated = jiffies;
		data->valid = 1;
	}

	up(&data->update_lock);

	return data;
}

static int __init sensors_adm1030_init(void)
{
	return i2c_add_driver(&adm1030_driver);
}

static void __exit sensors_adm1030_exit(void)
{
	i2c_del_driver(&adm1030_driver);
}

MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
MODULE_DESCRIPTION("ADM1030 driver");
MODULE_LICENSE("GPL");

module_init(sensors_adm1030_init);
module_exit(sensors_adm1030_exit);


^ permalink raw reply	[flat|nested] 22+ messages in thread

* ADM1030 Driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
                   ` (13 preceding siblings ...)
  2005-05-19  6:24 ` Alexandre d'Alton
@ 2005-05-19  6:24 ` Jean Delvare
  2005-05-19  6:24 ` Jean Delvare
                   ` (5 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: Jean Delvare @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

My comments inline.

> /* Many ADM1030 constants specified below */
> #define ADM1030_CONF1_MEN               0x01 /* monitoring enable */
> #define ADM1030_CONF1_AEN               0x80 /* Auto / SW Control */
> 
> #define ADM1030_REG_FAN_SPEED(nr)   	(0x08 + (nr))
> 
> #define ADM1030_REG_FAN_DIV(nr)		(0x20  + (nr))
> #define ADM1030_REG_FTAC(nr)		(0x10 + (nr))

ADM1030_REG_FAN_LOW or _MIN would be clearer.

> #define ADM1030_REG_FSP(nr)		(0x22 + (nr))

This is not correct, register 0x23 is something different. The normal
speed for fan 2 would obviously be stored in the upper nibble of the
same register. Also, the name isn't very explicit. I'd suggest
ADM1030_REG_FAN_NORMAL_SPEED.

> 
> 
> #define ADM1030_REG_TEMP_HLM(nr)		(0x14  + 4*(nr))
> #define ADM1030_REG_TEMP_LLM(nr)		(0x15  + 4*(nr))
> #define ADM1030_REG_TEMP_TLM(nr)		(0x16  + 4*(nr))

_HIGH, _LOW and (_THERM or _CRIT) prefered.


> /* Temperature values (nr = 1 (local) / 2 (remote)) */
> #define ADM1030_REG_TEMP(nr)		        (0xa + (nr))
> #define ADM1030_REG_TEMP_RNG(nr)		(0x24 + (nr))

RNG is a common acronym for "random number generator". You should use
"RANGE" instead, it's clearer IMHO.

> 
> /* Temperature values (nr = 1 (local) / 2 (remote)) */
> #define ADM1030_REG_CONF(nr)		        (nr)

Comment is bogus. And I don't see the point in using a parameter here,
since the two configuration registers have nothing in common. Just use
two defines.

I also note that the top macros use 0-1 for parameters, while the bottom
ones use 1-2. That's a bit confusing. Isn't it possible to tick to one
single convention?

You aren't taking advantage of the extended temperature resolution bits, are you?

> static u16 adm1030_fan_div = 1;

No. You should not need that, and it wouldn't be safe anymore for the
ADM1031 anyway (unless you declare a second variable). The correct way
to do it is that your to_display_fan_input() macro below should have two
parameters, not one. See asb100.c for a nice example.

> /* Each client has this additional data */
> struct adm1030_data {
>   struct semaphore	update_lock;
>   char			valid;		/* !=0 if following fields are valid */
>   unsigned int		last_updated;	/* In jiffies */
>   unsigned int		fan_input[1];
>   unsigned int	       	fan_div[1];
>   unsigned int	       	pwm[1];
>   unsigned int          temp_input[2];
>   unsigned int          temp_min[2];
>   unsigned int          temp_range[2];
>   unsigned int          temp_low_limit[2];
>   unsigned int          temp_high_limit[2];
>   unsigned int          temp_therm_limit[2];
> };

Indentation is broken.

> /* These macros converts registers values to sysfs-sensors compliant
>  * values
>  */
> #define to_display_temp_low_limit(reg) ((reg) * 1000)
> #define to_display_temp_high_limit(reg) ((reg) * 1000)
> #define to_display_temp_therm_limit(reg) ((reg) * 1000)
> #define to_display_temp_input(reg) ((reg) * 1000)
> #define to_display_temp_min(reg)   (1000 * ((((reg) >> 3) & 0x1f) << 2))
> #define to_display_temp_range(reg) (5000 * (1<<((reg)&0x07)))
> #define to_display_fan_input(reg)  ((reg) ? (11250 * 60) / ((reg)
> *               \
> 						    adm1030_fan_div) : 0)
> #define to_display_fan_div(reg)    (1 << (((reg) & 0xc0)>>6))
> #define to_display_pwm(reg)        ((reg)<<4)

Please use the usual names, see the other drivers for examples. Typical
names are TEMP_FROM_REG, TEMP_TO_REG, etc...

Do not define more macros than you need. For example, you obviously need
a single converter for temperature limits, don't define three.

> /* These macros converts sysfs-sensors compliant values into
>  * corresponding register value.
>  */
> #define to_reg_temp_min(reg, val) ((((val)/500) & 0xf8)|((reg) & 0x7))
> #define to_reg_temp_range(reg, val) (((reg) & 0xf8) | \
> (((val)<10000 ? 0 :                                   \
>   (val)<20000 ? 1 :                                   \
>   (val)<40000 ? 2 :                                   \
>   (val)<80000 ? 3 : 4) & 7))

It just me, or is this macro plain broken?

And usually, converters do not deal with the registers as you do. The
converters should return the "raw" values, and the caller can then
combine these with other values.

> static inline int to_reg_fan_div(unsigned int reg, unsigned int val) 
> {
>   adm1030_fan_div = val;
>   return ((reg & 0x3f) | 
> 	  ((val = 8 ? 0xc0 :                                            
> 	    val = 4 ? 0x80 :                                                
> 	    val = 2 ? 0x40 :                                                
> 	    val = 1 ? 0x00 : reg & 0xc0))); 
> }

Can be simplified.

> #define to_reg_pwm(reg, val) ((val) >> 4)
> #define to_reg_temp_low_limit(reg, val) ((val) / 1000)
> #define to_reg_temp_high_limit(reg, val) ((val) / 1000)
> #define to_reg_temp_therm_limit(reg, val) ((val) / 1000)

See my comments above (no register, no multiple macros doing the same thing).

> #define show(value, nr)	\
> static ssize_t show_##value##_##nr(struct device *dev, char *buf)		\
> {									\
> 	struct adm1030_data *data = adm1030_update_device(dev);		\
> 	return sprintf(buf, "%u\n", to_display_##value(data->value[(nr)-1]));	\
> }
> show(pwm, 1);
> show(temp_min, 1);
> show(temp_min, 2);
> show(temp_low_limit, 1);
> show(temp_low_limit, 2);
> show(temp_high_limit, 1);
> show(temp_high_limit, 2);
> show(temp_therm_limit, 1);
> show(temp_therm_limit, 2);
> show(temp_range, 1);
> show(temp_range, 2);
> show(temp_input, 1);
> show(temp_input, 2);
> show(fan_input, 1);
> show(fan_div, 1);
> 
> #define set(value, reg, nr)	\
> static ssize_t set_##value##_##nr(struct device *dev, const char *buf,
> size_t count)	\
> {								                \
> 	struct i2c_client *client = to_i2c_client(dev);		                \
> 	int temp = simple_strtoul(buf, NULL, 10);		                \
>         int old_val = adm1030_read_value(client,
> reg((nr)-1));                          \
> 	adm1030_write_value(client, reg((nr)-1), to_reg_##value(old_val,
> temp));	\
> 	return count;						                \
> }
> set(temp_min,   ADM1030_REG_TEMP_RNG, 1);
> set(temp_min,   ADM1030_REG_TEMP_RNG, 2);
> set(temp_range, ADM1030_REG_TEMP_RNG, 1);
> set(temp_range, ADM1030_REG_TEMP_RNG, 2);
> set(pwm,        ADM1030_REG_FSP,      1);
> set(fan_div,    ADM1030_REG_FAN_DIV,  1);

Now I get why you were defining multiple macros. You can just pass the
converter as an extra argument to set(). That said, some values will not
fit in that model anyway (set(fan_min...) for example, but strangely you
did not export it?)

> static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input_1, NULL);
> static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input_2, NULL);
> 
> static DEVICE_ATTR(pwm,         S_IWUSR | S_IRUGO, show_pwm_1,
> 		   set_pwm_1);

Incorrect name, should be pwm1.

> static DEVICE_ATTR(fan_div,     S_IRUGO | S_IWUSR, show_fan_div_1,
> 		   set_fan_div_1);

Incorrect name, should be fan1_div.

> static DEVICE_ATTR(fan_input,   S_IRUGO          , show_fan_input_1,   
> 		   NULL);

fan1_input.

Where is fan1_min?

> static DEVICE_ATTR(temp1_amin,   S_IRUGO | S_IWUSR, show_temp_min_1,    
> 		   set_temp_min_1);
> static DEVICE_ATTR(temp1_range, S_IRUGO | S_IWUSR, show_temp_range_1, 
> 		   set_temp_range_1);
> static DEVICE_ATTR(temp2_amin,   S_IRUGO | S_IWUSR, show_temp_min_2,    
> 		   set_temp_min_2);
> static DEVICE_ATTR(temp2_range, S_IRUGO | S_IWUSR, show_temp_range_2,  
> 		   set_temp_range_2);

BTW, what are these ones for? I don't understand why both thermal
sensors have these while there's a single fan to be controled anyway.

Also, I don't much like the names you chose, but you'd have to tell me a
bit more about what it is before I can come to the correct names.

> 	/* Now, we do the remaining detection. It is lousy. */

No, it is *NOT* lousy! (sorry to insist)

> static int adm1030_detach_client(struct i2c_client *client)
> {
> 	i2c_detach_client(client);
> 	kfree(client);
> 	return 0;
> }

You should check for error. The lm75 driver is a bad one and doesn't,
yet should. I think I already mentioned that. See any other chip driver
for an example of how to do it properly.

> 
> static int adm1030_read_value(struct i2c_client *client, u8 reg)
> {
>   return i2c_smbus_read_byte_data(client, reg);
> }
> 
> static int adm1030_write_value(struct i2c_client *client, u8 reg,
> unsigned int value)
> {
>   return i2c_smbus_write_byte_data(client, reg, value);
> }

You do not really need these, do you? At least inline them.

Two general comments/questions now:

1* How does bit 0 of configuration register 2 work? Maybe it could be
exported as pwm1_enable (if that's what it does).

2* I can clearly see that you are preparing the later adding of ADM1031
support. At this point, wouldn't it be easier to really support it? At
least we would protect ourselves against last time bad surprises. (And
if we extend the reasoning a little further, the driver should be named
adm1031, not adm1030, since the adm1030 is clearly a cut-down version of
the adm1031.)

Thanks.


-- 
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* ADM1030 Driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
                   ` (12 preceding siblings ...)
  2005-05-19  6:24 ` Alexandre d'Alton
@ 2005-05-19  6:24 ` Alexandre d'Alton
  2005-05-19  6:24 ` Jean Delvare
                   ` (6 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: Alexandre d'Alton @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

Quoting Jean Delvare <khali@linux-fr.org>:

> My comments inline.
> 
> > /* Many ADM1030 constants specified below */
> > #define ADM1030_CONF1_MEN               0x01 /* monitoring enable */
> > #define ADM1030_CONF1_AEN               0x80 /* Auto / SW Control */
> > 
> > #define ADM1030_REG_FAN_SPEED(nr)   	(0x08 + (nr))
> > 
> > #define ADM1030_REG_FAN_DIV(nr)		(0x20  + (nr))
> > #define ADM1030_REG_FTAC(nr)		(0x10 + (nr))
> 
> ADM1030_REG_FAN_LOW or _MIN would be clearer.
> 
> > #define ADM1030_REG_FSP(nr)		(0x22 + (nr))
> 
> This is not correct, register 0x23 is something different. The normal
> speed for fan 2 would obviously be stored in the upper nibble of the
> same register. Also, the name isn't very explicit. I'd suggest
> ADM1030_REG_FAN_NORMAL_SPEED.
> 
> > 
> > 
> > #define ADM1030_REG_TEMP_HLM(nr)		(0x14  + 4*(nr))
> > #define ADM1030_REG_TEMP_LLM(nr)		(0x15  + 4*(nr))
> > #define ADM1030_REG_TEMP_TLM(nr)		(0x16  + 4*(nr))
> 
> _HIGH, _LOW and (_THERM or _CRIT) prefered.
> 
> 
> > /* Temperature values (nr = 1 (local) / 2 (remote)) */
> > #define ADM1030_REG_TEMP(nr)		        (0xa + (nr))
> > #define ADM1030_REG_TEMP_RNG(nr)		(0x24 + (nr))
> 
> RNG is a common acronym for "random number generator". You should use
> "RANGE" instead, it's clearer IMHO.
> 
> > 
> > /* Temperature values (nr = 1 (local) / 2 (remote)) */
> > #define ADM1030_REG_CONF(nr)		        (nr)
> 
> Comment is bogus. And I don't see the point in using a parameter here,
> since the two configuration registers have nothing in common. Just use
> two defines.
> 
> I also note that the top macros use 0-1 for parameters, while the bottom
> ones use 1-2. That's a bit confusing. Isn't it possible to tick to one
> single convention?

I thought that I had changed all to take 0-1 as parameter.
The bottom macros uses 1-2 bacause of the file naming conventions, I think that
defining registers with not real values is also confusing.

> 
> You aren't taking advantage of the extended temperature resolution bits, are
> you?

No I don't for instance.

> 
> > static u16 adm1030_fan_div = 1;
> 
> No. You should not need that, and it wouldn't be safe anymore for the
> ADM1031 anyway (unless you declare a second variable). The correct way
> to do it is that your to_display_fan_input() macro below should have two
> parameters, not one. See asb100.c for a nice example.
> 

I'll check with asb100 to correct that.

> > /* Each client has this additional data */
> > struct adm1030_data {
> >   struct semaphore	update_lock;
> >   char			valid;		/* !=0 if following fields are valid */
> >   unsigned int		last_updated;	/* In jiffies */
> >   unsigned int		fan_input[1];
> >   unsigned int	       	fan_div[1];
> >   unsigned int	       	pwm[1];
> >   unsigned int          temp_input[2];
> >   unsigned int          temp_min[2];
> >   unsigned int          temp_range[2];
> >   unsigned int          temp_low_limit[2];
> >   unsigned int          temp_high_limit[2];
> >   unsigned int          temp_therm_limit[2];
> > };
> 
> Indentation is broken.
> 
> > /* These macros converts registers values to sysfs-sensors compliant
> >  * values
> >  */
> > #define to_display_temp_low_limit(reg) ((reg) * 1000)
> > #define to_display_temp_high_limit(reg) ((reg) * 1000)
> > #define to_display_temp_therm_limit(reg) ((reg) * 1000)
> > #define to_display_temp_input(reg) ((reg) * 1000)
> > #define to_display_temp_min(reg)   (1000 * ((((reg) >> 3) & 0x1f) << 2))
> > #define to_display_temp_range(reg) (5000 * (1<<((reg)&0x07)))
> > #define to_display_fan_input(reg)  ((reg) ? (11250 * 60) / ((reg)
> > *               \
> > 						    adm1030_fan_div) : 0)
> > #define to_display_fan_div(reg)    (1 << (((reg) & 0xc0)>>6))
> > #define to_display_pwm(reg)        ((reg)<<4)
> 
> Please use the usual names, see the other drivers for examples. Typical
> names are TEMP_FROM_REG, TEMP_TO_REG, etc...
> 
> Do not define more macros than you need. For example, you obviously need
> a single converter for temperature limits, don't define three.

OK, easy to change.

> 
> > /* These macros converts sysfs-sensors compliant values into
> >  * corresponding register value.
> >  */
> > #define to_reg_temp_min(reg, val) ((((val)/500) & 0xf8)|((reg) & 0x7))
> > #define to_reg_temp_range(reg, val) (((reg) & 0xf8) | \
> > (((val)<10000 ? 0 :                                   \
> >   (val)<20000 ? 1 :                                   \
> >   (val)<40000 ? 2 :                                   \
> >   (val)<80000 ? 3 : 4) & 7))
> 
> It just me, or is this macro plain broken?

Can I ask why without beeing an idiot ?

> 
> And usually, converters do not deal with the registers as you do. The
> converters should return the "raw" values, and the caller can then
> combine these with other values.

That means that we'll have different versions of set and show functions ?

> 
> > static inline int to_reg_fan_div(unsigned int reg, unsigned int val) 
> > {
> >   adm1030_fan_div = val;
> >   return ((reg & 0x3f) | 
> > 	  ((val = 8 ? 0xc0 :                                            
> > 	    val = 4 ? 0x80 :                                                
> > 	    val = 2 ? 0x40 :                                                
> > 	    val = 1 ? 0x00 : reg & 0xc0))); 
> > }
> 
> Can be simplified.
> 
> > #define to_reg_pwm(reg, val) ((val) >> 4)
> > #define to_reg_temp_low_limit(reg, val) ((val) / 1000)
> > #define to_reg_temp_high_limit(reg, val) ((val) / 1000)
> > #define to_reg_temp_therm_limit(reg, val) ((val) / 1000)
> 
> See my comments above (no register, no multiple macros doing the same
> thing).

Yep.

> 
> > #define show(value, nr)	\
> > static ssize_t show_##value##_##nr(struct device *dev, char *buf)		\
> > {									\
> > 	struct adm1030_data *data = adm1030_update_device(dev);		\
> > 	return sprintf(buf, "%u\n", to_display_##value(data->value[(nr)-1]));	\
> > }
> > show(pwm, 1);
> > show(temp_min, 1);
> > show(temp_min, 2);
> > show(temp_low_limit, 1);
> > show(temp_low_limit, 2);
> > show(temp_high_limit, 1);
> > show(temp_high_limit, 2);
> > show(temp_therm_limit, 1);
> > show(temp_therm_limit, 2);
> > show(temp_range, 1);
> > show(temp_range, 2);
> > show(temp_input, 1);
> > show(temp_input, 2);
> > show(fan_input, 1);
> > show(fan_div, 1);
> > 
> > #define set(value, reg, nr)	\
> > static ssize_t set_##value##_##nr(struct device *dev, const char *buf,
> > size_t count)	\
> > {								                \
> > 	struct i2c_client *client = to_i2c_client(dev);		                \
> > 	int temp = simple_strtoul(buf, NULL, 10);		                \
> >         int old_val = adm1030_read_value(client,
> > reg((nr)-1));                          \
> > 	adm1030_write_value(client, reg((nr)-1), to_reg_##value(old_val,
> > temp));	\
> > 	return count;						                \
> > }
> > set(temp_min,   ADM1030_REG_TEMP_RNG, 1);
> > set(temp_min,   ADM1030_REG_TEMP_RNG, 2);
> > set(temp_range, ADM1030_REG_TEMP_RNG, 1);
> > set(temp_range, ADM1030_REG_TEMP_RNG, 2);
> > set(pwm,        ADM1030_REG_FSP,      1);
> > set(fan_div,    ADM1030_REG_FAN_DIV,  1);
> 
> Now I get why you were defining multiple macros. You can just pass the
> converter as an extra argument to set(). That said, some values will not
> fit in that model anyway (set(fan_min...) for example, but strangely you
> did not export it?)
> 

In fact, fan_min and pwm are the same register. 
when in normal mode, pwm is the speed at which the fan will spin.
in automatic mode, it's the minimum speed at which the fan will spin.


> > static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input_1, NULL);
> > static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input_2, NULL);
> > 
> > static DEVICE_ATTR(pwm,         S_IWUSR | S_IRUGO, show_pwm_1,
> > 		   set_pwm_1);
> 
> Incorrect name, should be pwm1.
> 
> > static DEVICE_ATTR(fan_div,     S_IRUGO | S_IWUSR, show_fan_div_1,
> > 		   set_fan_div_1);
> 
> Incorrect name, should be fan1_div.
> 
> > static DEVICE_ATTR(fan_input,   S_IRUGO          , show_fan_input_1,   
> > 		   NULL);
> 
> fan1_input.
> 
> Where is fan1_min?

See above. Same as pwm1.

> 
> > static DEVICE_ATTR(temp1_amin,   S_IRUGO | S_IWUSR, show_temp_min_1,    
> > 		   set_temp_min_1);
> > static DEVICE_ATTR(temp1_range, S_IRUGO | S_IWUSR, show_temp_range_1, 
> > 		   set_temp_range_1);
> > static DEVICE_ATTR(temp2_amin,   S_IRUGO | S_IWUSR, show_temp_min_2,    
> > 		   set_temp_min_2);
> > static DEVICE_ATTR(temp2_range, S_IRUGO | S_IWUSR, show_temp_range_2,  
> > 		   set_temp_range_2);
> 
> BTW, what are these ones for? I don't understand why both thermal
> sensors have these while there's a single fan to be controled anyway.
> 
> Also, I don't much like the names you chose, but you'd have to tell me a
> bit more about what it is before I can come to the correct names.
> 
> > 	/* Now, we do the remaining detection. It is lousy. */
> 
> No, it is *NOT* lousy! (sorry to insist)

OK I'll remove it on next version I'll post. :)

> 
> > static int adm1030_detach_client(struct i2c_client *client)
> > {
> > 	i2c_detach_client(client);
> > 	kfree(client);
> > 	return 0;
> > }
> 
> You should check for error. The lm75 driver is a bad one and doesn't,
> yet should. I think I already mentioned that. See any other chip driver
> for an example of how to do it properly.

In fact it is already done in my local version...

> 
> > 
> > static int adm1030_read_value(struct i2c_client *client, u8 reg)
> > {
> >   return i2c_smbus_read_byte_data(client, reg);
> > }
> > 
> > static int adm1030_write_value(struct i2c_client *client, u8 reg,
> > unsigned int value)
> > {
> >   return i2c_smbus_write_byte_data(client, reg, value);
> > }
> 
> You do not really need these, do you? At least inline them.
> 

You're right, will remove theese.

> Two general comments/questions now:
> 
> 1* How does bit 0 of configuration register 2 work? Maybe it could be
> exported as pwm1_enable (if that's what it does).
> 
Bit 0 of conf register 2 enables the pwm output, I think that if the bit is
cleared, the fan will never rotate. Even if in automatic mode. But it is not
verified yet...

> 2* I can clearly see that you are preparing the later adding of ADM1031
> support. At this point, wouldn't it be easier to really support it? At
> least we would protect ourselves against last time bad surprises. (And
> if we extend the reasoning a little further, the driver should be named
> adm1031, not adm1030, since the adm1030 is clearly a cut-down version of
> the adm1031.)

I'll try to integrate adm1031 support in the next version i'll post.

In order to detect them I'll use the device ID register.

Given for example that the second external temp channel is temp3 prefixed,
The correct way to handle both is to do something like :

if( kind = adm1031 ) {
	device_create_file(&new_client->dev, &dev_attr_temp3_amin);
	device_create_file(&new_client->dev, &dev_attr_temp3_range);
	device_create_file(&new_client->dev, &dev_attr_temp3_min);
	device_create_file(&new_client->dev, &dev_attr_temp3_max);
	device_create_file(&new_client->dev, &dev_attr_temp3_crit);
        ...
}

isn't it ?

Thanks for your time.

Alex.
> 
> Thanks.
> 
> 
> -- 
> Jean Delvare
> http://www.ensicaen.ismra.fr/~delvare/
> 


-- 
-------------------------------------------------
Alexandre d'ALTON
                              alex@alexdalton.org

^ permalink raw reply	[flat|nested] 22+ messages in thread

* ADM1030 Driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
                   ` (14 preceding siblings ...)
  2005-05-19  6:24 ` Jean Delvare
@ 2005-05-19  6:24 ` Jean Delvare
  2005-05-19  6:24 ` Jean Delvare
                   ` (4 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: Jean Delvare @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

> I thought that I had changed all to take 0-1 as parameter.
> The bottom macros uses 1-2 bacause of the file naming conventions, I
> think that defining registers with not real values is also confusing.

I'm not sure I get what you mean exactly.

You can still call the macros with (nr-1) as the parameter, so it
shouldn't cause problems (and I now see that you already do that).

> > You aren't taking advantage of the extended temperature resolution
> > bits, are you?
> 
> No I don't for instance.

("for instance" = "par exemple")

> > > /* These macros converts sysfs-sensors compliant values into
> > >  * corresponding register value.
> > >  */
> > > #define to_reg_temp_min(reg, val) ((((val)/500) & 0xf8)|((reg) &
> 0x7))
> > > #define to_reg_temp_range(reg, val) (((reg) & 0xf8) | \
> > > (((val)<10000 ? 0 :                                   \
> > >   (val)<20000 ? 1 :                                   \
> > >   (val)<40000 ? 2 :                                   \
> > >   (val)<80000 ? 3 : 4) & 7))
> > 
> > It just me, or is this macro plain broken?
> 
> Can I ask why without beeing an idiot ?

Answer 1: it was just me. So I am the actual idiot ;)

> > And usually, converters do not deal with the registers as you do.
> > The converters should return the "raw" values, and the caller can
> > then combine these with other values.
> 
> That means that we'll have different versions of set and show
> functions ?

Now I see that most of your choices were guided by the choice to have a
single macro for all "set" functions. The other drivers also try to
refactor these functions where possible, but to a lesser extent,
obviously.

Remember that this is a macro, so you don't spare on the driver's size
anyway. And remember that a number of set functions will not fit in your
model anyway (set_fan_div among others).

I agree that your approach is interesting and makes code more compact,
but for the sake of consistency, it may be preferable to follow the
same policy as in the other drivers. But this is just my own opinion, I
don't know if it is shared by the other developers. So just do as you
like.

> > Now I get why you were defining multiple macros. You can just pass
> > the converter as an extra argument to set(). That said, some values
> > will not fit in that model anyway (set(fan_min...) for example, but
> > strangely you did not export it?)
> 
> In fact, fan_min and pwm are the same register. 
> when in normal mode, pwm is the speed at which the fan will spin.
> in automatic mode, it's the minimum speed at which the fan will
> spin.

Oh, OK. Thanks for the information, altough this isn't the "fan_min" I
was refering to. I was thinking of the fan speed (in RPM, not PWM)
below which an alarm is triggered. All fan monitoring chips have that.
Don't the ADM1030 do?

> > Where is fan1_min?
> 
> See above. Same as pwm1.

This cannot be, BTW, since fan1_min has to be a RPM value according to
Documentation/i2c/sysfs-interface.

> > 1* How does bit 0 of configuration register 2 work? Maybe it could
> > be exported as pwm1_enable (if that's what it does).
>
> Bit 0 of conf register 2 enables the pwm output, I think that if the
> bit iscleared, the fan will never rotate. Even if in automatic mode.
> But it is not verified yet...

Please try and tell me.

> I'll try to integrate adm1031 support in the next version i'll post.
> 
> In order to detect them I'll use the device ID register.
> 
> Given for example that the second external temp channel is temp3
> prefixed,
> The correct way to handle both is to do something like :
> 
> if( kind = adm1031 ) {
> 	device_create_file(&new_client->dev, &dev_attr_temp3_amin);
> 	device_create_file(&new_client->dev, &dev_attr_temp3_range);
> 	device_create_file(&new_client->dev, &dev_attr_temp3_min);
> 	device_create_file(&new_client->dev, &dev_attr_temp3_max);
> 	device_create_file(&new_client->dev, &dev_attr_temp3_crit);
>         ...
> }
> 
> isn't it ?

You got it.

More on automatic PWM after lunch ;)

-- 
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* ADM1030 Driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
                   ` (11 preceding siblings ...)
  2005-05-19  6:24 ` Alexandre d'Alton
@ 2005-05-19  6:24 ` Alexandre d'Alton
  2005-05-19  6:24 ` Alexandre d'Alton
                   ` (7 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: Alexandre d'Alton @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

Quoting Jean Delvare <khali@linux-fr.org>:

> > I thought that I had changed all to take 0-1 as parameter.
> > The bottom macros uses 1-2 bacause of the file naming conventions, I
> > think that defining registers with not real values is also confusing.
> 
> I'm not sure I get what you mean exactly.
> 
> You can still call the macros with (nr-1) as the parameter, so it
> shouldn't cause problems (and I now see that you already do that).

OK we agreed then.

> 
> > > You aren't taking advantage of the extended temperature resolution
> > > bits, are you?
> > 
> > No I don't for instance.
> 
> ("for instance" = "par exemple")

Have to improve my english :)
So I would say, I don't take advantage of extended resolution YET.

> 
> > > > /* These macros converts sysfs-sensors compliant values into
> > > >  * corresponding register value.
> > > >  */
> > > > #define to_reg_temp_min(reg, val) ((((val)/500) & 0xf8)|((reg) &
> > 0x7))
> > > > #define to_reg_temp_range(reg, val) (((reg) & 0xf8) | \
> > > > (((val)<10000 ? 0 :                                   \
> > > >   (val)<20000 ? 1 :                                   \
> > > >   (val)<40000 ? 2 :                                   \
> > > >   (val)<80000 ? 3 : 4) & 7))
> > > 
> > > It just me, or is this macro plain broken?
> > 
> > Can I ask why without beeing an idiot ?
> 
> Answer 1: it was just me. So I am the actual idiot ;)

Nobody is an idiot.

> 
> > > And usually, converters do not deal with the registers as you do.
> > > The converters should return the "raw" values, and the caller can
> > > then combine these with other values.
> > 
> > That means that we'll have different versions of set and show
> > functions ?
> 
> Now I see that most of your choices were guided by the choice to have a
> single macro for all "set" functions. The other drivers also try to
> refactor these functions where possible, but to a lesser extent,
> obviously.
> 
> Remember that this is a macro, so you don't spare on the driver's size
> anyway. And remember that a number of set functions will not fit in your
> model anyway (set_fan_div among others).
> 
> I agree that your approach is interesting and makes code more compact,
> but for the sake of consistency, it may be preferable to follow the
> same policy as in the other drivers. But this is just my own opinion, I
> don't know if it is shared by the other developers. So just do as you
> like.

So, if I get you, if it is easier to read to define more than one set macro or
function, I can do it ?

> 
> > > Now I get why you were defining multiple macros. You can just pass
> > > the converter as an extra argument to set(). That said, some values
> > > will not fit in that model anyway (set(fan_min...) for example, but
> > > strangely you did not export it?)
> > 
> > In fact, fan_min and pwm are the same register. 
> > when in normal mode, pwm is the speed at which the fan will spin.
> > in automatic mode, it's the minimum speed at which the fan will
> > spin.
> 
> Oh, OK. Thanks for the information, altough this isn't the "fan_min" I
> was refering to. I was thinking of the fan speed (in RPM, not PWM)
> below which an alarm is triggered. All fan monitoring chips have that.
> Don't the ADM1030 do?

Yes, it is the TACH register. I have to implement the access to that register.

> 
> > > Where is fan1_min?
> > 
> > See above. Same as pwm1.
> 
> This cannot be, BTW, since fan1_min has to be a RPM value according to
> Documentation/i2c/sysfs-interface.

I misunderstood, you're right, pwm1 will be the min value at which the fan will
spin, and fan1_min (to be added) will be the fan speed uner which the chip will
generate a fan_fault interrupt.

> 
> > > 1* How does bit 0 of configuration register 2 work? Maybe it could
> > > be exported as pwm1_enable (if that's what it does).
> >
> > Bit 0 of conf register 2 enables the pwm output, I think that if the
> > bit iscleared, the fan will never rotate. Even if in automatic mode.
> > But it is not verified yet...
> 
> Please try and tell me.

I'll test it as soon as I'll be in front of my computer.

> 
> > I'll try to integrate adm1031 support in the next version i'll post.
> > 
> > In order to detect them I'll use the device ID register.
> > 
> > Given for example that the second external temp channel is temp3
> > prefixed,
> > The correct way to handle both is to do something like :
> > 
> > if( kind = adm1031 ) {
> > 	device_create_file(&new_client->dev, &dev_attr_temp3_amin);
> > 	device_create_file(&new_client->dev, &dev_attr_temp3_range);
> > 	device_create_file(&new_client->dev, &dev_attr_temp3_min);
> > 	device_create_file(&new_client->dev, &dev_attr_temp3_max);
> > 	device_create_file(&new_client->dev, &dev_attr_temp3_crit);
> >         ...
> > }
> > 
> > isn't it ?
> 
> You got it.
> 
> More on automatic PWM after lunch ;)

Bon appetit !

> 
> -- 
> Jean Delvare
> http://www.ensicaen.ismra.fr/~delvare/
> 
> 


-- 
-------------------------------------------------
Alexandre d'ALTON
                              alex@alexdalton.org

^ permalink raw reply	[flat|nested] 22+ messages in thread

* ADM1030 Driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
                   ` (16 preceding siblings ...)
  2005-05-19  6:24 ` Jean Delvare
@ 2005-05-19  6:24 ` Alexandre d'Alton
  2005-05-19  6:24 ` Alexandre d'Alton
                   ` (2 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: Alexandre d'Alton @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

On Wed, 2004-04-14 at 12:07, Jean Delvare wrote:
> Now I see that most of your choices were guided by the choice to have a
> single macro for all "set" functions. The other drivers also try to
> refactor these functions where possible, but to a lesser extent,
> obviously.
> 
> Remember that this is a macro, so you don't spare on the driver's size
> anyway. And remember that a number of set functions will not fit in your
> model anyway (set_fan_div among others).
> 
> I agree that your approach is interesting and makes code more compact,
> but for the sake of consistency, it may be preferable to follow the
> same policy as in the other drivers. But this is just my own opinion, I
> don't know if it is shared by the other developers. So just do as you
> like.

So I'll try to be more compliant with lm85 driver. And for the tricky
registers, I'll define functions that handles them without using the
global show and set macros.

> 
> > > Now I get why you were defining multiple macros. You can just pass
> > > the converter as an extra argument to set(). That said, some values
> > > will not fit in that model anyway (set(fan_min...) for example, but
> > > strangely you did not export it?)
> > 
> > In fact, fan_min and pwm are the same register. 
> > when in normal mode, pwm is the speed at which the fan will spin.
> > in automatic mode, it's the minimum speed at which the fan will
> > spin.
> 
> Oh, OK. Thanks for the information, altough this isn't the "fan_min" I
> was refering to. I was thinking of the fan speed (in RPM, not PWM)
> below which an alarm is triggered. All fan monitoring chips have that.
> Don't the ADM1030 do?
> 
> > > Where is fan1_min?
> > 
> > See above. Same as pwm1.
> 
> This cannot be, BTW, since fan1_min has to be a RPM value according to
> Documentation/i2c/sysfs-interface.
> 
> > > 1* How does bit 0 of configuration register 2 work? Maybe it could
> > > be exported as pwm1_enable (if that's what it does).
> >
> > Bit 0 of conf register 2 enables the pwm output, I think that if the
> > bit iscleared, the fan will never rotate. Even if in automatic mode.
> > But it is not verified yet...
> 
> Please try and tell me.

Clearing that bit disables PWM output, so the fan doesn't run at all!

> 
> > I'll try to integrate adm1031 support in the next version i'll post.
> > 
> > In order to detect them I'll use the device ID register.
> > 
> > Given for example that the second external temp channel is temp3
> > prefixed,
> > The correct way to handle both is to do something like :
> > 
> > if( kind = adm1031 ) {
> > 	device_create_file(&new_client->dev, &dev_attr_temp3_amin);
> > 	device_create_file(&new_client->dev, &dev_attr_temp3_range);
> > 	device_create_file(&new_client->dev, &dev_attr_temp3_min);
> > 	device_create_file(&new_client->dev, &dev_attr_temp3_max);
> > 	device_create_file(&new_client->dev, &dev_attr_temp3_crit);
> >         ...
> > }
> > 
> > isn't it ?
> 
> You got it.
> 
> More on automatic PWM after lunch ;)

^ permalink raw reply	[flat|nested] 22+ messages in thread

* ADM1030 Driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
                   ` (15 preceding siblings ...)
  2005-05-19  6:24 ` Jean Delvare
@ 2005-05-19  6:24 ` Jean Delvare
  2005-05-19  6:24 ` Alexandre d'Alton
                   ` (3 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: Jean Delvare @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

> > > Bit 0 of conf register 2 enables the pwm output, I think that if
> > > the bit iscleared, the fan will never rotate. Even if in automatic
> > > mode. But it is not verified yet...
> > 
> > Please try and tell me.
> 
> Clearing that bit disables PWM output, so the fan doesn't run at all!

Then you don't want to allow the user to do it. At all.

-- 
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* ADM1030 Driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
                   ` (17 preceding siblings ...)
  2005-05-19  6:24 ` Alexandre d'Alton
@ 2005-05-19  6:24 ` Alexandre d'Alton
  2005-05-19  6:24 ` Jean Delvare
  2005-05-19  6:24 ` Alexandre d'Alton
  20 siblings, 0 replies; 22+ messages in thread
From: Alexandre d'Alton @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

On Mon, 2004-04-19 at 22:04, Jean Delvare wrote:
> > > > Bit 0 of conf register 2 enables the pwm output, I think that if
> > > > the bit iscleared, the fan will never rotate. Even if in automatic
> > > > mode. But it is not verified yet...
> > > 
> > > Please try and tell me.
> > 
> > Clearing that bit disables PWM output, so the fan doesn't run at all!
> 
> Then you don't want to allow the user to do it. At all.

Off course !

Concerning the auto fan mode, I think that the interface:

auto_temp_off
auto_temp_min
auto_temp_max

is clear, so I'm going to implement that.

Concerning the channel configuration, as

adm1030 can select:
	- ext temp for regulation 
	- max of the 2 channels

adm1031 can select:
	- ext temp1 for fan1, ext temp2 for fan2
	- ext temp1 for fan1 and fan2 
	- ext temp2 for fan1 and fan2 
	- max of 3 channels for fan1 and fan2

it is pretty hard to create a standard interface, so what I suggest, is
to create a file fan_auto_temp_select that would verbosely print the
values it can hold. Example for adm1030:

# cat fan_auto_temp_select
  * 0 => manual mode
    1 => temp2 controls fan1 temp3 controls fan2
    2 => temp2 controls fan1 and fan2
    3 => temp3 controls fan1 and fan2
    4 => max controls fan1 and fan2

The star shows the current config.

Do you like this approach ? Is it possible to write as many bytes in the
buffer given to the show() interface ? Or do you have another idea ?

Alex.


^ permalink raw reply	[flat|nested] 22+ messages in thread

* ADM1030 Driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
                   ` (18 preceding siblings ...)
  2005-05-19  6:24 ` Alexandre d'Alton
@ 2005-05-19  6:24 ` Jean Delvare
  2005-05-19  6:24 ` Alexandre d'Alton
  20 siblings, 0 replies; 22+ messages in thread
From: Jean Delvare @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

> Concerning the channel configuration, as
> 
> adm1030 can select:
> 	- ext temp for regulation 
> 	- max of the 2 channels
> 
> adm1031 can select:
> 	- ext temp1 for fan1, ext temp2 for fan2
> 	- ext temp1 for fan1 and fan2 
> 	- ext temp2 for fan1 and fan2 
> 	- max of 3 channels for fan1 and fan2
> 
> it is pretty hard to create a standard interface, so what I suggest,
> is to create a file fan_auto_temp_select that would verbosely print
> the values it can hold. Example for adm1030:
> 
> # cat fan_auto_temp_select
>   * 0 => manual mode
>     1 => temp2 controls fan1 temp3 controls fan2
>     2 => temp2 controls fan1 and fan2
>     3 => temp3 controls fan1 and fan2
>     4 => max controls fan1 and fan2
> 
> The star shows the current config.
> 
> Do you like this approach ?

Not at all.

> Is it possible to write as many bytes in the buffer given to the show() interface ?

Probably but with a little more research and work.

> Or do you have another idea ?

I proposed to handle it with a bitfield. A 1 means that the given
temperature channel is taken into account for the given fan speed
control. I agree that it's not immediate but with some extra thinking
and work we can have the ADM1030/1031 fit it this model.

For the adm1030:

- ext temp for regulation
  file 1 value is 00000010b

- max of the 2 channels
  file 1 value is 00000011b

For the adm101:
- ext temp1 for fan1, ext temp2 for fan2
  file 1 value is 00000010b
  file 2 value is 00000100b

- ext temp1 for fan1 and fan2 
  file 1 value is 00000010b
  file 2 value is 00000010b

- ext temp2 for fan1 and fan2 
  file 1 value is 00000100b
  file 2 value is 00000100b

- max of 3 channels for fan1 and fan2
  file 1 value is 00000111b
  file 2 value is 00000111b

So "decoding" it is easy. Now for encoding it's somewhat more tricky,
admittedly, because you write to a single file at a time while the
values are linked. That's not the first time we have to do that. There's
a first example in lm90.c.

If the value written is just not possible (say 00000110b to file 1) just
return an error (-EINVAL).

If it is possible and is compatible with the current value of the other
file, do the change.

If it is possible but not compatible with the current value of the other
file, change the other file, preferably to the "nearest" value.

The main advantage of this method compared with yours is that we can
hope to implement it in a similar and compatible way for the other
chipsets as well, thus preserving our ultimate goal: chip-independancy.

Does it sound feasable?

BTW remember that you can always propose a not full-features driver yet,
and add the extra features as further patches.

Also, how do you plan to add support to the user space (sensors and
libsensors?) Looks like you need a 2.4 driver as well to do it the same
way the other drivers do. So either you will have to backport it, or we
will have to find a trick.

Thanks.

-- 
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* ADM1030 Driver
  2005-05-19  6:24 Adm1030 driver Cedric Pradalier
                   ` (19 preceding siblings ...)
  2005-05-19  6:24 ` Jean Delvare
@ 2005-05-19  6:24 ` Alexandre d'Alton
  20 siblings, 0 replies; 22+ messages in thread
From: Alexandre d'Alton @ 2005-05-19  6:24 UTC (permalink / raw)
  To: lm-sensors

> I proposed to handle it with a bitfield. A 1 means that the given
> temperature channel is taken into account for the given fan speed
> control. I agree that it's not immediate but with some extra thinking
> and work we can have the ADM1030/1031 fit it this model.
> 
> For the adm1030:
> 
> - ext temp for regulation
>   file 1 value is 00000010b
> 
> - max of the 2 channels
>   file 1 value is 00000011b
> 
> For the adm101:
> - ext temp1 for fan1, ext temp2 for fan2
>   file 1 value is 00000010b
>   file 2 value is 00000100b
> 
> - ext temp1 for fan1 and fan2 
>   file 1 value is 00000010b
>   file 2 value is 00000010b
> 
> - ext temp2 for fan1 and fan2 
>   file 1 value is 00000100b
>   file 2 value is 00000100b
> 
> - max of 3 channels for fan1 and fan2
>   file 1 value is 00000111b
>   file 2 value is 00000111b
> 
> So "decoding" it is easy. Now for encoding it's somewhat more tricky,
> admittedly, because you write to a single file at a time while the
> values are linked. That's not the first time we have to do that. There's
> a first example in lm90.c.
> 
> If the value written is just not possible (say 00000110b to file 1) just
> return an error (-EINVAL).
> 
> If it is possible and is compatible with the current value of the other
> file, do the change.
> 
> If it is possible but not compatible with the current value of the other
> file, change the other file, preferably to the "nearest" value.
> 
> The main advantage of this method compared with yours is that we can
> hope to implement it in a similar and compatible way for the other
> chipsets as well, thus preserving our ultimate goal: chip-independancy.
> 
> Does it sound feasable?
I'm going to try such an implementation.

> 
> BTW remember that you can always propose a not full-features driver yet,
> and add the extra features as further patches.
> 
> Also, how do you plan to add support to the user space (sensors and
> libsensors?) Looks like you need a 2.4 driver as well to do it the same
> way the other drivers do. So either you will have to backport it, or we
> will have to find a trick.
When the 2.6 driver will be finnished, I'll backport it to the 2.4
kernel if you want me to.
But as I have only a adm1030 chip on my motherboard, the 1031 features
will not be tested...

> Thanks.

^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2005-05-19  6:24 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-05-19  6:24 Adm1030 driver Cedric Pradalier
2005-05-19  6:24 ` Jean Delvare
2005-05-19  6:24 ` Mark Studebaker
2005-05-19  6:24 ` Cedric Pradalier
2005-05-19  6:24 ` Jean Delvare
2005-05-19  6:24 ` Mark Studebaker
2005-05-19  6:24 ` Mark Studebaker
2005-05-19  6:24 ` Cedric Pradalier
2005-05-19  6:24 ` ADM1030 Driver Alexandre d'Alton
2005-05-19  6:24 ` Jean Delvare
2005-05-19  6:24 ` Alexandre d'Alton
2005-05-19  6:24 ` Alexandre d'Alton
2005-05-19  6:24 ` Alexandre d'Alton
2005-05-19  6:24 ` Alexandre d'Alton
2005-05-19  6:24 ` Alexandre d'Alton
2005-05-19  6:24 ` Jean Delvare
2005-05-19  6:24 ` Jean Delvare
2005-05-19  6:24 ` Jean Delvare
2005-05-19  6:24 ` Alexandre d'Alton
2005-05-19  6:24 ` Alexandre d'Alton
2005-05-19  6:24 ` Jean Delvare
2005-05-19  6:24 ` Alexandre d'Alton

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.