gl518sm chip driver for 2.6 kernel
Quoted 4 lines
I found that the gl518sm driver has not been ported to the 2.6 kernel in the latest 2.6.1 release. If no one has, I have attempted it, and it seems to work fine. I have attached the file with this email. I hope it can be included into the sensors package.
This is a great piece of news :) We are really looking for driver porters.
Quoted 1 line
What else do I need to do?
I will review your code this evening or tomorrow. Did you look at and follow the porting guidelines (Documentation/i2c/porting-clients) or not? If not, please take some time to read it, apply any change needed to you code and submit it again for review. Else, your work is probably over and all you have to do now is be patient until I can review your code and send it to Greg KH for inclusion into Linux 2.6.3.
Thanks a lot :)
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
Quoted 2 lines
Yes, I did follow the guidelines as well as the sysfs-interface, but since its my first kernel module attempt, it might need improvement.
Hopefully the code review will be quick then :) I'll let you know my impressions.
Quoted 2 lines
I can also help with other ports if there's a need, since am familiar with it.
This is very welcome, since only 25% of the chip drivers have been ported so far. Basically, all drivers that have not been ported yet are good candidates. Ports in progress are: adm1025, bt869, fscher and vt1211. So you can pick any other. You should focus on drivers you have the hardware to test, if possible, since it's much better if the port is tested by at least one person before being released. If you start another port, just let us know so that we can update our list.
Thanks a lot for your work.
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
Quoting Chew Hong Gunn <[email protected]>:
Quoted 5 lines
Do note that these comments are due to the original code that I was not involved in. I simply ported it over to 2.6 kernel accordingly. Since the chip specification is not available anymore, I can only comment based on what I think the code is doing and from doc/chips/gl518sm.
Same here. I'm only discovering the code and was quite surprised that is is so different from other chip drivers. Haven't read the docs nor data sheet yet, but this will probably be required.
Note that we *do* have a copy of the data sheet, here:
http://www2.lm-sensors.nu/~lm78/pdfs/gl518sm_r117.pdf
(This shows how important it is to centralize the data sheets as we get them, so that they survive manuacturer's website evolutions.)
Quoted 3 lines
As for the threaded method of reading the values, it would be up to team to decide whether to remove or keep it. I will update the code accordingly.
I think I understand why this was done that way. This makes the driver much more complex however. I have no strong opinion as to wether we should keep it that way or simplify it.
One possible replacement is to simply keep two different bufferization timings (as this is already the case) for fast registers and slow ones. Voltages don't change much as far as I can tell (as opposed to temperatures, and fans that can break silently). If the PSU breaks, it's likely that you'll notice that before sensors has a chance to tell you.
So I'd be perfectly happy with a driver simplification, especially since I don't know anything about threads. And a simplified driver is anyway better than no driver at all.
But if someone with enough knowledge of threads can handle the problem and ensure that the code is clean enough to enter the 2.6 kernel tree, it's OK too.
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
Quoting Chew Hong Gunn <[email protected]>:
Quoted 5 lines
Quoted 3 lines
* Your new driver defines two different names for the two different releases of the chip. This breaks compatibility with libsensors, and would better be fixed (I will do too).
Ok. It was there to differentiate the two revisions.
The intention was good, but libsensors won't like it, sorry.
Quoted 4 lines
I have tested rc2 and there are no problems with it. I did however make two minor changes to it:
* addedswap_bytesfunction declaration (unless you deleted it intentionally)
Yes, deletion was intentional. This is a utility function and doesn't belong to the interface, so to say.
Quoted 3 lines
* removed voltage_in[0-2] = 0 for rev00 chips
these are initialised to 0 on init, so there should be no need to set them at each update.
I couldn't find where it had been initialized so decided to play it safe, but you're right, there's a memset at the very beginning, so this part can get away.
Looks like we are not that far from a "final" version? Maybe some changes to bring comments, authorship and copyrights in line with the other drivers, and we should have something good for inclusion.
Greg, we're listening to your comments if you have any. And thanks a lot for your initial review which was obviously needed ;)
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
Quoting Chew Hong Gunn <[email protected]>:
Quoted 8 lines · Jean Delvare
Quoted 5 lines
Looks like we are not that far from a "final" version? Maybe some changes to bring comments, authorship and copyrights in line with the other drivers, and we should have something good for inclusion.
If you can point me to one to use as a template, I could work on it.
In fact I misexpressed myself. I don't think there is an "absolute" template to start from. It was more about random thoughts that came to me, such as:
1* I wouldn't use tabs in the heading comment.
2* I wouldn't mention "internal" revisions into the history.
3* I think we should mention the important changes we just made (removal
of the ugly iteration stuff).
4* I wonder who should be listed in MODULE_AUTHOR(). This is really a general question. I just don't know wether it should be seen as a kind of hall of fame, in which case all names should appear, or a pointer to one or more contacts, in which case only people still in activity should be listed. The names of the original authors are still in the sources, whatever.
Also, in the case of a driver being ported, I don't know wether the names and contacts of the porters are to be mentioned there. I used to think no, but when people port the driver for hardware they have and use, they are probably the right people to contact for help and feedback (together with the list, of course). So I don't really know what should be done.
Greg, any advice as of good practices with respect to this topic?
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
Comments on the code itself:
Review
Quoted 3 lines
(quoted diff) +3
-0+#ifdef __SMP__ +#include <linux/smp_lock.h> +#endif
No, just include it, don't #ifdef it, that's not needed at all.
Review
Quoted 6 lines
(quoted diff) +6
-0+/* Defining this will enable debug messages for the voltage iteration + code used with rev 0 ICs */ +#undef DEBUG_VIN +#ifdef DEBUG +#define DEBUG_VIN +#endif
As you always tie DEBUG_VIN to DEBUG, why even have it at all? Just get rid of it, and everywhere it's defined.
Review
Quoted 8 lines
(quoted diff) +8
-0+static int gl518_update_thread(void *c) +{ + struct i2c_client *client = c; + struct gl518_data *data = i2c_get_clientdata(client); + +#ifdef __SMP__ + lock_kernel(); +#endif
No. Again, no #ifdefs are needed. And what's with the lock_kernel() call? What are you trying to protect here?
It looks like you only kick off this kernel thread if a user opens a specific sysfs file. Why? Why not just work like all other sensor drivers, and update the chip data when asked for it from the user?
I really do not see the need for this thread here.
Review
Quoted 12 lines
(quoted diff) +12
-0+ exit_mm(current); + current->session = 1; + sigfillset(¤t->blocked); + current->fs->umask = 0; + strcpy(current->comm, "gl518sm"); + + init_waitqueue_head(&(data->wq)); + data->thread = current; + +#ifdef __SMP__ + unlock_kernel(); +#endif
I think there's easier, and more proper ways to create a kernel thread in 2.6 now. If you really want to do this (and you can convince me of it), please look into this (hint, search for daemonize()).
Quoted 4 lines
(quoted diff) +4
-0+ for (j = 0; j < 10 && loop_more; j++) { + for (i = 0; i < 3; i++) + gl518_write_value(client, VIN_REG(i), + max[i] << 8 | min[i]);
Review
Quoted 10 lines
(quoted diff) +10
-0+ + if ((data->thread) && + ((data->quit_thread) || signal_pending(current))) + goto finish; + + /* we wait now 1.5 seconds before comparing */ + current->state = TASK_INTERRUPTIBLE; + schedule_timeout(HZ + HZ / 2); + + alarm = gl518_read_value(client, GL518_REG_INT);
So you sleep for 1.5 seconds 10 times? Why? That seems very slow...
thanks,
greg k-h
Quoted 2 lines
Yes, I did follow the guidelines as well as the sysfs-interface, but since its my first kernel module attempt, it might need improvement.
OK, I reviewed your code and you have done a great job. I can hardly believe that this is your first contact with kernel code.
I have adjusted a few things in your original port. If we left apart uninteresting things such as white space changes, indentation and the such (which BTW weren't bad at all), here is a list of changes:
* Add standard debug stuff. This is something I need to add to the porting-clients doc.
* Reorder includes.
* No function declaration for swap_bytes();
* Switch from gl518_list to gl518_id. This makes the driver more in line with the others I've seen so far. The other method looks like something leftover from older times. I think that this change could be backported to the 2.4 driver as well, since new code is more simple and doesn't imply any arbitrary limit to the number of chips supported. However, I'd like you to confirm that the new code works.
* Looks like you forgot to create the sysfs file "beep_mask". I added the required line. Please correct me if I'm mistaking.
* Remove one use of DEBUG_VIN, which seems to be bogus. It makes non-00 revision chips use revision 00 code if DEBUG_VIN is set. This doesn't match the description of DEBUG_VIN, so I took the liberty to clear it.
Attached is a patch against Linux 2.6.2-rc2 which adds the new driver and integrates it cleanly into the kernel tree. I'd like you to make sure it works correctly for you before we add it to the official tree.
For reference, can you tell us wether your test system uses release 0x00 or 0x80 of the gl518sm? If 0x00, please try with DEBUG turned on too, to make sure it still works.
Greg, I've reviewed the code and it looks good, however I'm not familiar with threads, neither in Linux 2.4 nor in 2.6. Could you review that part of the code (bottom of the file) and confirm that it's correct for inclusion into Linux 2.6? The easier way to check is probably to read a diff between the driver as it exists in our CVS repository and the attached ported driver. One thing that looks strange to me is the suppression of this line:current->pgrp = 1;
This is in gl518_update_thread(). Might be correct however, I just don't know bit about threads so I am wondering if it's correct to remove the line. This is the first chip driver I see that deals with threads, so I really need assistance in reviewing it.
Also, Greg, can you tell me wether my "inclusion" of DEBUG_VIN into DEBUG is correct or not? (that's at the top of the new driver.)
After tests and code review are completed, we will have a new chip driver ported to Linux 2.6. Yeah! :)
Thanks.
Signature · 871 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/ -------------- next part -------------- diff -ruN linux-2.6.2-rc2/drivers/i2c/chips/Kconfig linux-2.6.2-rc2-k1/drivers/i2c/chips/Kconfig --- linux-2.6.2-rc2/drivers/i2c/chips/Kconfig Tue Jan 27 19:22:59 2004 +++ linux-2.6.2-rc2-k1/drivers/i2c/chips/Kconfig Wed Jan 28 21:48:20 2004 @@ -45,6 +45,17 @@ This driver can also be built as a module. If so, the module will be called eeprom. +config SENSORS_GL518SM + tristate "Genesys Logic GL518SM" + depends on I2C && EXPERIMENTAL + select I2C_SENSOR + help + If you say yes here you get support for Genesys Logic GL518SM + sensor chips (releases 0x00 and 0x80). + + This driver can also be built as a module. If so, the module + will be called gl518sm. + config SENSORS_IT87 tristate "ITE IT87xx and compatibles" depends on I2C && EXPERIMENTAL diff -ruN linux-2.6.2-rc2/drivers/i2c/chips/Makefile linux-2.6.2-rc2-k1/drivers/i2c/chips/Makefile --- linux-2.6.2-rc2/drivers/i2c/chips/Makefile Tue Jan 27 19:22:59 2004 +++ linux-2.6.2-rc2-k1/drivers/i2c/chips/Makefile Wed Jan 28 21:48:20 2004 @@ -8,6 +8,7 @@ obj-$(CONFIG_SENSORS_ADM1021) += adm1021.o obj-$(CONFIG_SENSORS_EEPROM) += eeprom.o +obj-$(CONFIG_SENSORS_GL518SM) += gl518sm.o obj-$(CONFIG_SENSORS_IT87) += it87.o obj-$(CONFIG_SENSORS_LM75) += lm75.o obj-$(CONFIG_SENSORS_LM78) += lm78.o diff -ruN linux-2.6.2-rc2/drivers/i2c/chips/gl518sm.c linux-2.6.2-rc2-k1/drivers/i2c/chips/gl518sm.c --- linux-2.6.2-rc2/drivers/i2c/chips/gl518sm.c Thu Jan 1 01:00:00 1970 +++ linux-2.6.2-rc2-k1/drivers/i2c/chips/gl518sm.c Wed Jan 28 22:38:26 2004 @@ -0,0 +1,840 @@ +/* + * gl518sm.c - Part of lm_sensors, Linux kernel modules for hardware + * monitoring + * Copyright (C) 1998, 1999 Frodo Looijaard <[email protected]>, + * Kyosti Malkki <[email protected]> + * port to 2.6 Kernel + * Copyright (C) 2004 Hong-Gunn Chew <[email protected]> + * + * 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. + */ + +/* + * v2.0 (2004-01-24, Hong-Gunn Chew) + * - ported to 2.6 kernel + * v2.1 (2004-01-28, Hong-Gunn Chew) + * - Updated sysfs output according to standard data format + * (temp, in) + */ + +#include <linux/config.h> +#ifdef CONFIG_I2C_DEBUG_CHIP +#define DEBUG 1 +#endif + +#include <linux/module.h> +#include <linux/init.h> +#include <linux/slab.h> +#include <linux/i2c.h> +#include <linux/i2c-sensor.h> +#ifdef __SMP__ +#include <linux/smp_lock.h> +#endif + +/* Addresses to scan */ +static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END }; +static unsigned short normal_i2c_range[] = { 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_2(gl518sm_r00, gl518sm_r80); + +/* Defining this will enable debug messages for the voltage iteration + code used with rev 0 ICs */ +#undef DEBUG_VIN +#ifdef DEBUG +#define DEBUG_VIN +#endif + +/* Many GL518 constants specified below */ + +/* The GL518 registers */ +#define GL518_REG_CHIP_ID 0x00 +#define GL518_REG_REVISION 0x01 +#define GL518_REG_VENDOR_ID 0x02 +#define GL518_REG_CONF 0x03 +#define GL518_REG_TEMP_IN 0x04 +#define GL518_REG_TEMP_MAX 0x05 +#define GL518_REG_TEMP_HYST 0x06 +#define GL518_REG_FAN_COUNT 0x07 +#define GL518_REG_FAN_LIMIT 0x08 +#define GL518_REG_VIN1_LIMIT 0x09 +#define GL518_REG_VIN2_LIMIT 0x0a +#define GL518_REG_VIN3_LIMIT 0x0b +#define GL518_REG_VDD_LIMIT 0x0c +#define GL518_REG_VIN3 0x0d +#define GL518_REG_MISC 0x0f +#define GL518_REG_ALARM 0x10 +#define GL518_REG_MASK 0x11 +#define GL518_REG_INT 0x12 +#define GL518_REG_VIN2 0x13 +#define GL518_REG_VIN1 0x14 +#define GL518_REG_VDD 0x15 + + +/* + * Conversions. Rounding and limit checking is only done on the TO_REG + * variants. Note that you should be a bit careful with which arguments + * these macros are called: arguments may be evaluated more than once. + * Fixing this is just not worth it. + */ + +#define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0? \ + (val)-500:(val)+500)/1000)+119),0,255)) +#define TEMP_FROM_REG(val) (((val) - 119) * 1000) + +static inline u8 FAN_TO_REG(long rpm, int div) +{ + if (rpm = 0) + return 255; + rpm = SENSORS_LIMIT(rpm, 1, 1000000); + return SENSORS_LIMIT((960000 + rpm * div / 2) / (rpm * div), 1, 254); +} + +#define FAN_FROM_REG(val,div) ((val)=0 ? 0 : (val)=255 ? 0 : \ + (960000/((val)*(div)))) + +#define IN_TO_REG(val) (SENSORS_LIMIT((((val)+8)/19),0,255)) +#define IN_FROM_REG(val) ((val)*19) + +#define VDD_TO_REG(val) (SENSORS_LIMIT((((val)+11)/23),0,255)) +#define VDD_FROM_REG(val) ((val)*23) + +#define DIV_TO_REG(val) ((val)=8?3:(val)=4?2:(val)=1?0:1) +#define DIV_FROM_REG(val) (1 << (val)) + +#define ALARMS_FROM_REG(val) val + +#define BEEP_ENABLE_TO_REG(val) ((val)?0:1) +#define BEEP_ENABLE_FROM_REG(val) ((val)?0:1) + +#define BEEP_MASK_TO_REG(val) ((val) & 0x7f & data->alarm_mask) +#define BEEP_MASK_FROM_REG(val) ((val) & 0x7f) + +/* Each client has this additional data */ +struct gl518_data { + enum chips type; + + struct semaphore update_lock; + + int iterate_lock; + int quit_thread; + struct task_struct *thread; + wait_queue_head_t wq; + char valid; /* !=0 if following fields are valid */ + unsigned long last_updated; /* In jiffies */ + unsigned long last_updated_v00; + /* In jiffies (used only by rev00 chips) */ + + u8 voltage_in[4]; /* Register values; [0] = VDD */ + u8 voltage_min[4]; /* Register values; [0] = VDD */ + u8 voltage_max[4]; /* Register values; [0] = VDD */ + u8 iter_voltage_in[4]; /* Register values; [0] = VDD */ + u8 fan_in[2]; + u8 fan_min[2]; + u8 fan_div[2]; /* Register encoding, shifted right */ + u8 temp_in; /* Register values */ + u8 temp_max; /* Register values */ + u8 temp_hyst; /* Register values */ + u8 alarms; /* Register value */ + u8 alarm_mask; /* Register value */ + u8 beep_mask; /* Register value */ + u8 beep_enable; /* Boolean */ + u8 iterate; /* Voltage iteration mode */ +}; + +static int gl518_attach_adapter(struct i2c_adapter *adapter); +static int gl518_detect(struct i2c_adapter *adapter, int address, int kind); +static void gl518_init_client(struct i2c_client *client); +static int gl518_detach_client(struct i2c_client *client); + +static int gl518_read_value(struct i2c_client *client, u8 reg); +static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value); + +static void gl518_update_client(struct i2c_client *client); +static void gl518_update_client_rev00(struct i2c_client *client); +static void gl518_update_iterate(struct i2c_client *client); +static int gl518_update_thread(void *c); + + +/* This is the driver that will be inserted */ +static struct i2c_driver gl518_driver = { + .owner = THIS_MODULE, + .name = "gl518sm", + .id = I2C_DRIVERID_GL518, + .flags = I2C_DF_NOTIFY, + .attach_adapter = gl518_attach_adapter, + .detach_client = gl518_detach_client, +}; + +/* + * Internal variables + */ + +static int gl518_id = 0; + + +/* + * Sysfs stuff + */ + +#define show(type, suffix, value) \ +static ssize_t show_##suffix(struct device *dev, char *buf) \ +{ \ + struct i2c_client *client = to_i2c_client(dev); \ + struct gl518_data *data = i2c_get_clientdata(client); \ + gl518_update_client(client); \ + return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \ +} + +#define show_fan(suffix, value, index) \ +static ssize_t show_##suffix(struct device *dev, char *buf) \ +{ \ + struct i2c_client *client = to_i2c_client(dev); \ + struct gl518_data *data = i2c_get_clientdata(client); \ + gl518_update_client(client); \ + return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[index], \ + DIV_FROM_REG(data->fan_div[index]))); \ +} + +show(TEMP, temp_input, temp_in); +show(TEMP, temp_max, temp_max); +show(TEMP, temp_hyst, temp_hyst); +show_fan(fan_input1, fan_in, 0); +show_fan(fan_input2, fan_in, 1); +show_fan(fan_min1, fan_min, 0); +show_fan(fan_min2, fan_min, 1); +show(DIV, fan_div1, fan_div[0]); +show(DIV, fan_div2, fan_div[1]); +show(VDD, in_input1, voltage_in[0]); +show(IN, in_input2, voltage_in[1]); +show(IN, in_input3, voltage_in[2]); +show(IN, in_input4, voltage_in[3]); +show(VDD, in_min1, voltage_min[0]); +show(IN, in_min2, voltage_min[1]); +show(IN, in_min3, voltage_min[2]); +show(IN, in_min4, voltage_min[3]); +show(VDD, in_max1, voltage_max[0]); +show(IN, in_max2, voltage_max[1]); +show(IN, in_max3, voltage_max[2]); +show(IN, in_max4, voltage_max[3]); +show(ALARMS, alarms, alarms); +show(BEEP_ENABLE, beep_enable, beep_enable); +show(BEEP_MASK, beep_mask, beep_mask); + +static ssize_t show_iterate(struct device *dev, char *buf) +{ + struct i2c_client *client = to_i2c_client(dev); + struct gl518_data *data = i2c_get_clientdata(client); + return sprintf(buf, "%d\n", data->iterate); +} + +#define set(type, suffix, value, reg) \ +static ssize_t set_##suffix(struct device *dev, const char *buf, \ + size_t count) \ +{ \ + struct i2c_client *client = to_i2c_client(dev); \ + struct gl518_data *data = i2c_get_clientdata(client); \ + data->value = type##_TO_REG(simple_strtoul(buf, NULL, 10)); \ + gl518_write_value(client, reg, data->value); \ + return count; \ +} + +#define set_bits(type, suffix, value, reg, mask, shift) \ +static ssize_t set_##suffix(struct device *dev, const char *buf, \ + size_t count) \ +{ \ + struct i2c_client *client = to_i2c_client(dev); \ + struct gl518_data *data = i2c_get_clientdata(client); \ + int regvalue = gl518_read_value(client, reg); \ + data->value = type##_TO_REG(simple_strtoul(buf, NULL, 10)); \ + regvalue = (regvalue & mask) | (data->value << shift); \ + gl518_write_value(client, reg, regvalue); \ + return count; \ +} +#define set_low(type, suffix, value, reg) \ + set_bits(type, suffix, value, reg, 0xff00, 0) +#define set_high(type, suffix, value, reg) \ + set_bits(type, suffix, value, reg, 0x00ff, 8) + +set(TEMP, temp_max, temp_max, GL518_REG_TEMP_MAX); +set(TEMP, temp_hyst, temp_hyst, GL518_REG_TEMP_HYST); +set_bits(DIV, fan_div1, fan_div[0], GL518_REG_MISC, 0x3f, 4); +set_bits(DIV, fan_div2, fan_div[1], GL518_REG_MISC, 0xcf, 6); +set_low(VDD, in_min1, voltage_min[0], GL518_REG_VDD_LIMIT); +set_low(IN, in_min2, voltage_min[1], GL518_REG_VIN1_LIMIT); +set_low(IN, in_min3, voltage_min[2], GL518_REG_VIN2_LIMIT); +set_low(IN, in_min4, voltage_min[3], GL518_REG_VIN3_LIMIT); +set_high(VDD, in_max1, voltage_max[0], GL518_REG_VDD_LIMIT); +set_high(IN, in_max2, voltage_max[1], GL518_REG_VIN1_LIMIT); +set_high(IN, in_max3, voltage_max[2], GL518_REG_VIN2_LIMIT); +set_high(IN, in_max4, voltage_max[3], GL518_REG_VIN3_LIMIT); +set_bits(BEEP_ENABLE, beep_enable, beep_enable, GL518_REG_CONF, 0xfb, 2); +set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM); + +static ssize_t set_fan_min1(struct device *dev, const char *buf, size_t count) +{ + struct i2c_client *client = to_i2c_client(dev); + struct gl518_data *data = i2c_get_clientdata(client); + int regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT); + + data->fan_min[0] = FAN_TO_REG(simple_strtoul(buf, NULL, 10), + DIV_FROM_REG(data->fan_div[0])); + regvalue = (regvalue & 0xff00) | data->fan_min[0]; + if (data->fan_min[0] = 0) + data->alarm_mask &= ~0x0020; + else + data->alarm_mask |= 0x0020; + gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue); + + return count; +} + +static ssize_t set_fan_min2(struct device *dev, const char *buf, size_t count) +{ + struct i2c_client *client = to_i2c_client(dev); + struct gl518_data *data = i2c_get_clientdata(client); + int regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT); + + data->fan_min[1] = FAN_TO_REG(simple_strtoul(buf, NULL, 10), + DIV_FROM_REG(data->fan_div[0])); + regvalue = (regvalue & 0x00ff) | (data->fan_min[1] << 8); + if (data->fan_min[1] = 0) + data->alarm_mask &= ~0x0040; + else + data->alarm_mask |= 0x0040; + gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue); + + return count; +} + +static ssize_t set_iterate(struct device *dev, const char *buf, size_t count) +{ + struct i2c_client *client = to_i2c_client(dev); + struct gl518_data *data = i2c_get_clientdata(client); + + if (data->type = gl518sm_r00) { + u8 newiterate = simple_strtoul(buf, NULL, 10); + if ((data->iterate != newiterate) && (newiterate <= 2)) { + int i; + + data->iterate = newiterate; + data->valid = 0; + for (i=0; i<4; i++) { + data->voltage_in[i] = 0; + data->iter_voltage_in[i] = 0; + } + if (newiterate != 2) { + if (data->thread) { + data->quit_thread = 1; + wake_up_interruptible(&data->wq); + } + } else if (!data->thread) { + init_waitqueue_head(&data->wq); + kernel_thread(gl518_update_thread, + (void *) client, 0); + } + } + } + + return count; +} + +static DEVICE_ATTR(temp_input, S_IRUGO, show_temp_input, NULL); +static DEVICE_ATTR(temp_max, S_IWUSR|S_IRUGO, show_temp_max, set_temp_max); +static DEVICE_ATTR(temp_hyst, S_IWUSR|S_IRUGO, show_temp_hyst, set_temp_hyst); +static DEVICE_ATTR(fan_input1, S_IRUGO, show_fan_input1, NULL); +static DEVICE_ATTR(fan_input2, S_IRUGO, show_fan_input2, NULL); +static DEVICE_ATTR(fan_min1, S_IWUSR|S_IRUGO, show_fan_min1, set_fan_min1); +static DEVICE_ATTR(fan_min2, S_IWUSR|S_IRUGO, show_fan_min2, set_fan_min2); +static DEVICE_ATTR(fan_div1, S_IWUSR|S_IRUGO, show_fan_div1, set_fan_div1); +static DEVICE_ATTR(fan_div2, S_IWUSR|S_IRUGO, show_fan_div2, set_fan_div2); +static DEVICE_ATTR(in_input1, S_IRUGO, show_in_input1, NULL); +static DEVICE_ATTR(in_input2, S_IRUGO, show_in_input2, NULL); +static DEVICE_ATTR(in_input3, S_IRUGO, show_in_input3, NULL); +static DEVICE_ATTR(in_input4, S_IRUGO, show_in_input4, NULL); +static DEVICE_ATTR(in_min1, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1); +static DEVICE_ATTR(in_min2, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2); +static DEVICE_ATTR(in_min3, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3); +static DEVICE_ATTR(in_min4, S_IWUSR|S_IRUGO, show_in_min4, set_in_min4); +static DEVICE_ATTR(in_max1, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1); +static DEVICE_ATTR(in_max2, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2); +static DEVICE_ATTR(in_max3, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3); +static DEVICE_ATTR(in_max4, S_IWUSR|S_IRUGO, show_in_max4, set_in_max4); +static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); +static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO, + show_beep_enable, set_beep_enable); +static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO, + show_beep_mask, set_beep_mask); +static DEVICE_ATTR(iterate, S_IWUSR|S_IRUGO, show_iterate, set_iterate); + +/* + * Real code + */ + +static int gl518_attach_adapter(struct i2c_adapter *adapter) +{ + if (!(adapter->class & I2C_ADAP_CLASS_SMBUS)) + return 0; + return i2c_detect(adapter, &addr_data, gl518_detect); +} + +static int gl518_detect(struct i2c_adapter *adapter, int address, int kind) +{ + int i; + struct i2c_client *new_client; + struct gl518_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; + + /* 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 gl518_{read,write}_value. */ + + if (!(new_client = kmalloc(sizeof(struct i2c_client) + + sizeof(struct gl518_data), + GFP_KERNEL))) { + err = -ENOMEM; + goto exit; + } + memset(new_client, 0x00, sizeof(struct i2c_client) + + sizeof(struct gl518_data)); + + data = (struct gl518_data *) (new_client + 1); + i2c_set_clientdata(new_client, data); + + new_client->addr = address; + new_client->adapter = adapter; + new_client->driver = &gl518_driver; + new_client->flags = 0; + + /* Now, we do the remaining detection. */ + + if (kind < 0) { + if ((gl518_read_value(new_client, GL518_REG_CHIP_ID) != 0x80) + || (gl518_read_value(new_client, GL518_REG_CONF) & 0x80)) + goto exit_free; + } + + /* Determine the chip type. */ + if (kind <= 0) { + i = gl518_read_value(new_client, GL518_REG_REVISION); + if (i = 0x00) { + kind = gl518sm_r00; + name = "gl518sm_r00"; + } else if (i = 0x80) { + kind = gl518sm_r80; + name = "gl518sm_r80"; + } else { + if (kind <= 0) + dev_info(&adapter->dev, + "Ignoring 'force' parameter for unknown " + "chip at adapter %d, address 0x%02x\n", + i2c_adapter_id(adapter), address); + goto exit_free; + } + } + + /* Fill in the remaining client fields */ + strlcpy(new_client->name, name, I2C_NAME_SIZE); + new_client->id = gl518_id++; + data->type = kind; + 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 GL518SM chip */ + if (kind = gl518sm_r00) + data->iterate = 0; + else + data->iterate = 3; + data->iterate_lock = 0; + data->quit_thread = 0; + data->thread = NULL; + data->alarm_mask = 0xff; + data->voltage_in[0]Úta->voltage_in[1]Úta->voltage_in[2]=0; + gl518_init_client((struct i2c_client *) new_client); + + /* Register sysfs hooks */ + device_create_file(&new_client->dev, &dev_attr_in_input1); + device_create_file(&new_client->dev, &dev_attr_in_input2); + device_create_file(&new_client->dev, &dev_attr_in_input3); + device_create_file(&new_client->dev, &dev_attr_in_input4); + device_create_file(&new_client->dev, &dev_attr_in_min1); + device_create_file(&new_client->dev, &dev_attr_in_min2); + device_create_file(&new_client->dev, &dev_attr_in_min3); + device_create_file(&new_client->dev, &dev_attr_in_min4); + device_create_file(&new_client->dev, &dev_attr_in_max1); + device_create_file(&new_client->dev, &dev_attr_in_max2); + device_create_file(&new_client->dev, &dev_attr_in_max3); + device_create_file(&new_client->dev, &dev_attr_in_max4); + device_create_file(&new_client->dev, &dev_attr_fan_input1); + device_create_file(&new_client->dev, &dev_attr_fan_input2); + device_create_file(&new_client->dev, &dev_attr_fan_min1); + device_create_file(&new_client->dev, &dev_attr_fan_min2); + device_create_file(&new_client->dev, &dev_attr_fan_div1); + device_create_file(&new_client->dev, &dev_attr_fan_div2); + device_create_file(&new_client->dev, &dev_attr_temp_input); + device_create_file(&new_client->dev, &dev_attr_temp_max); + device_create_file(&new_client->dev, &dev_attr_temp_hyst); + device_create_file(&new_client->dev, &dev_attr_alarms); + device_create_file(&new_client->dev, &dev_attr_beep_enable); + device_create_file(&new_client->dev, &dev_attr_beep_mask); + device_create_file(&new_client->dev, &dev_attr_iterate); + + return 0; + +/* OK, this is not exactly good programming practice, usually. But it is + very code-efficient in this case. */ + +exit_free: + kfree(new_client); +exit: + return err; +} + + +/* Called when we have found a new GL518SM. */ +static void gl518_init_client(struct i2c_client *client) +{ + /* Power-on defaults (bit 7=1) */ + gl518_write_value(client, GL518_REG_CONF, 0x80); + + /* No noisy output (bit 2=1), Comparator mode (bit 3=0), two fans (bit4=0), + standby mode (bit6=0) */ + gl518_write_value(client, GL518_REG_CONF, 0x04); + + /* Never interrupts */ + gl518_write_value(client, GL518_REG_MASK, 0x00); + + /* Clear status register (bit 5=1), start (bit6=1) */ + gl518_write_value(client, GL518_REG_CONF, 0x24); + gl518_write_value(client, GL518_REG_CONF, 0x44); +} + +static int gl518_detach_client(struct i2c_client *client) +{ + struct gl518_data *data = i2c_get_clientdata(client); + int err; + + if ((err = i2c_detach_client(client))) { + dev_err(&client->dev, "Client deregistration failed, " + "client not detached.\n"); + return err; + } + + if (data->thread) { + data->quit_thread = 1; + wake_up_interruptible(&data->wq); + } + + kfree(client); + + return 0; +} + +static inline u16 swap_bytes(u16 val) +{ + return (val >> 8) | (val << 8); +} + +/* Registers 0x07 to 0x0c are word-sized, others are byte-sized + GL518 uses a high-byte first convention, which is exactly opposite to + the usual practice. */ +static int gl518_read_value(struct i2c_client *client, u8 reg) +{ + if ((reg >= 0x07) && (reg <= 0x0c)) + return swap_bytes(i2c_smbus_read_word_data(client, reg)); + else + return i2c_smbus_read_byte_data(client, reg); +} + +/* Registers 0x07 to 0x0c are word-sized, others are byte-sized + GL518 uses a high-byte first convention, which is exactly opposite to + the usual practice. */ +static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value) +{ + if ((reg >= 0x07) && (reg <= 0x0c)) + return i2c_smbus_write_word_data(client, reg, + swap_bytes(value)); + else + return i2c_smbus_write_byte_data(client, reg, value); +} + +static void gl518_update_client(struct i2c_client *client) +{ + struct gl518_data *data = i2c_get_clientdata(client); + int val; + + down(&data->update_lock); + + if ((jiffies - data->last_updated > HZ + HZ / 2) || + (jiffies < data->last_updated) || !data->valid) { + dev_dbg(&client->dev, "Starting gl518 update\n"); + + data->alarms = gl518_read_value(client, GL518_REG_INT); + data->beep_mask = gl518_read_value(client, GL518_REG_ALARM); + + val = gl518_read_value(client, GL518_REG_VDD_LIMIT); + data->voltage_min[0] = val & 0xff; + data->voltage_max[0] = (val >> 8) & 0xff; + val = gl518_read_value(client, GL518_REG_VIN1_LIMIT); + data->voltage_min[1] = val & 0xff; + data->voltage_max[1] = (val >> 8) & 0xff; + val = gl518_read_value(client, GL518_REG_VIN2_LIMIT); + data->voltage_min[2] = val & 0xff; + data->voltage_max[2] = (val >> 8) & 0xff; + val = gl518_read_value(client, GL518_REG_VIN3_LIMIT); + data->voltage_min[3] = val & 0xff; + data->voltage_max[3] = (val >> 8) & 0xff; + + val = gl518_read_value(client, GL518_REG_FAN_COUNT); + data->fan_in[0] = (val >> 8) & 0xff; + data->fan_in[1] = val & 0xff; + + val = gl518_read_value(client, GL518_REG_FAN_LIMIT); + data->fan_min[0] = (val >> 8) & 0xff; + data->fan_min[1] = val & 0xff; + + data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN); + data->temp_max + gl518_read_value(client, GL518_REG_TEMP_MAX); + data->temp_hyst + gl518_read_value(client, GL518_REG_TEMP_HYST); + + val = gl518_read_value(client, GL518_REG_MISC); + data->fan_div[0] = (val >> 6) & 0x03; + data->fan_div[1] = (val >> 4) & 0x03; + + data->alarms &= data->alarm_mask; + + val = gl518_read_value(client, GL518_REG_CONF); + data->beep_enable = (val >> 2) & 1; + + if (data->type != gl518sm_r00) { + data->voltage_in[0] + gl518_read_value(client, GL518_REG_VDD); + data->voltage_in[1] + gl518_read_value(client, GL518_REG_VIN1); + data->voltage_in[2] + gl518_read_value(client, GL518_REG_VIN2); + data->voltage_in[3] + gl518_read_value(client, GL518_REG_VIN3); + } else + gl518_update_client_rev00(client); + + data->last_updated = jiffies; + data->valid = 1; + } + + up(&data->update_lock); +} + +/* Here we decide how to run the iteration code. + When called, we trigger the iteration and report the last + measured voltage. No delay for user apps */ +static void gl518_update_client_rev00(struct i2c_client *client) +{ + struct gl518_data *data = i2c_get_clientdata(client); + int i; + + if (data->iterate = 1) { /* 10 sec delay */ + /* as that update is slow, we consider the data valid for 30 seconds */ + if ( + ((jiffies - data->last_updated_v00 > 30 * HZ) + || (data->alarms & 7) + || (!data->valid)) && (!data->iterate_lock)) { + data->iterate_lock = 1; + gl518_update_iterate(client); + data->iterate_lock = 0; + } + for (i = 0; i < 4; i++) + data->voltage_in[i] = data->iter_voltage_in[i]; + } else if (data->iterate = 2) { /* show results of last iteration */ + for (i = 0; i < 4; i++) + data->voltage_in[i] = data->iter_voltage_in[i]; + wake_up_interruptible(&data->wq); + } else { /* no iteration */ + data->voltage_in[3] + gl518_read_value(client, GL518_REG_VIN3); + } +} + +static int gl518_update_thread(void *c) +{ + struct i2c_client *client = c; + struct gl518_data *data = i2c_get_clientdata(client); + +#ifdef __SMP__ + lock_kernel(); +#endif + exit_mm(current); + current->session = 1; + sigfillset(¤t->blocked); + current->fs->umask = 0; + strcpy(current->comm, "gl518sm"); + + init_waitqueue_head(&(data->wq)); + data->thread = current; + +#ifdef __SMP__ + unlock_kernel(); +#endif + + for (;;) { + if (!data->iterate_lock) { + data->iterate_lock = 1; + gl518_update_iterate(client); + data->iterate_lock = 0; + } + + if ((data->quit_thread) || signal_pending(current)) + break; + wait_event_interruptible(data->wq, data->quit_thread); + } + + data->thread = NULL; + data->quit_thread = 0; + return 0; +} + +/* This updates vdd, vin1, vin2 values by doing slow and multiple + comparisons for the GL518SM rev 00 that lacks support for direct + reading of these values. Values are kept in iter_voltage */ + +static void gl518_update_iterate(struct i2c_client *client) +{ + struct gl518_data *data = i2c_get_clientdata(client); + int i, j, loop_more = 1, min[3], max[3], delta[3]; + int alarm, beep_mask, irqs; + +#define VIN_REG(c) c=0?GL518_REG_VDD_LIMIT:\ + c=1?GL518_REG_VIN1_LIMIT:\ + GL518_REG_VIN2_LIMIT + + /* disable beeps & irqs for vin0-2 */ + beep_mask = gl518_read_value(client, GL518_REG_ALARM); + irqs = gl518_read_value(client, GL518_REG_MASK); + gl518_write_value(client, GL518_REG_ALARM, beep_mask & ~0x7); + gl518_write_value(client, GL518_REG_MASK, irqs & ~0x7); + + alarm = data->alarms; + + for (i = 0; i < 3; i++) { + if (alarm & (1 << i)) { + min[i] = 0; + max[i] = 127; + } else { + min[i] = data->voltage_min[i]; + max[i] + (data->voltage_max[i] + + data->voltage_min[i]) / 2; + } + delta[i] = (max[i] - min[i]) / 2; + } + + for (j = 0; j < 10 && loop_more; j++) { + for (i = 0; i < 3; i++) + gl518_write_value(client, VIN_REG(i), + max[i] << 8 | min[i]); + + if ((data->thread) && + ((data->quit_thread) || signal_pending(current))) + goto finish; + + /* we wait now 1.5 seconds before comparing */ + current->state = TASK_INTERRUPTIBLE; + schedule_timeout(HZ + HZ / 2); + + alarm = gl518_read_value(client, GL518_REG_INT); + +#ifdef DEBUG_VIN + dev_dbg(&client->dev, "iteration %2d: %4d%c %4d%c %4d%c\n", + j, + max[0], (alarm & 1) ? '!' : ' ', + max[1], (alarm & 2) ? '!' : ' ', + max[2], (alarm & 4) ? '!' : ' '); +#endif + + for (loop_more = 0, i = 0; i < 3; i++) { + if (alarm & (1 << i)) + max[i] += delta[i]; + else + max[i] -= delta[i]; + + if (delta[i]) + loop_more++; + delta[i] >>= 1; + } + + } + + for (i = 0; i < 3; i++) + if (alarm & (1 << i)) + max[i]++; + +#ifdef DEBUG_VIN + dev_dbg(&client->dev, " final :%5d %5d %5d\n", max[0], max[1], + max[2]); + dev_dbg(&client->dev, " meter :%5d %5d %5d\n", + data->voltage_in[0], data->voltage_in[1], + data->voltage_in[2]); +#endif + + /* update values, including vin3 */ + for (i = 0; i < 3; i++) { + data->iter_voltage_in[i] = max[i]; + } + data->iter_voltage_in[3] = gl518_read_value(client, GL518_REG_VIN3); + data->last_updated_v00 = jiffies; + +finish: + + /* reset values */ + for (i = 0; i < 3; i++) { + gl518_write_value(client, VIN_REG(i), + (data->voltage_max[i] << 8) | data->voltage_min[i]); + } + + gl518_write_value(client, GL518_REG_ALARM, beep_mask); + gl518_write_value(client, GL518_REG_MASK, irqs); +} + +static int __init sensors_gl518sm_init(void) +{ + return i2c_add_driver(&gl518_driver); +} + +static void __exit sensors_gl518sm_exit(void) +{ + i2c_del_driver(&gl518_driver); +} + +MODULE_AUTHOR("Frodo Looijaard <[email protected]>, " + "Kyosti Malkki <[email protected]> and " + "Hong-Gunn Chew <[email protected]>"); +MODULE_DESCRIPTION("GL518SM driver"); +MODULE_LICENSE("GPL"); + +module_init(sensors_gl518sm_init); +module_exit(sensors_gl518sm_exit);
Quoted 1 line
Comments on the code itself:
Thanks for the comments on the code.
Do note that these comments are due to the original code that I was not involved in. I simply ported it over to 2.6 kernel accordingly. Since the chip specification is not available anymore, I can only comment based on what I think the code is doing and from doc/chips/gl518sm.
Quoted 11 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+static int gl518_update_thread(void *c) +{ + struct i2c_client *client = c; + struct gl518_data *data = i2c_get_clientdata(client); + +#ifdef __SMP__ + lock_kernel(); +#endifNo. Again, no #ifdefs are needed. And what's with the
lock_kernel()call? What are you trying to protect here?
I have no experiment as to what is considered SMP unsafe. I assume the author wanted to create the thread safely.
Quoted 5 lines
It looks like you only kick off this kernel thread if a user opens a specific sysfs file. Why? Why not just work like all other sensor drivers, and update the chip data when asked for it from the user?
I really do not see the need for this thread here.
This code is only relevant for the rev0 chip and only for iterate=2
There are 3 operation modes for the rev0 chip:
iterate=0: in[1-2] are not read iterate=1: in[1-2] are read when sysfs is read iterate=2: in[1-2] are read every 1.5s
The reason for iterate=1 and 2 is that the read operation requires about 10s to complete for this rev0 chip hardware, so iterate=2 is used if it is preferable for sysfs not to block for 10s while the read is performed, and instead returns the value obtain in the previous iterative read.
Quoted 8 lines
Quoted 8 lines
(quoted diff) +8
-0+ exit_mm(current); + current->session = 1; + sigfillset(¤t->blocked); + current->fs->umask = 0; + strcpy(current->comm, "gl518sm"); + + init_waitqueue_head(&(data->wq)); + data->thread = current;
Quoted 8 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+ +#ifdef __SMP__ + unlock_kernel(); +#endifI think there's easier, and more proper ways to create a kernel thread in 2.6 now. If you really want to do this (and you can convince me of it), please look into this (hint, search for
daemonize()).
I will have a look into it and see if I change it. More hints would be helpful.
Quoted 8 lines
Quoted 8 lines
(quoted diff) +8
-0+ for (j = 0; j < 10 && loop_more; j++) { + for (i = 0; i < 3; i++) + gl518_write_value(client, VIN_REG(i), + max[i] << 8 | min[i]); + + if ((data->thread) && + ((data->quit_thread) || signal_pending(current))) + goto finish;
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ + /* we wait now 1.5 seconds before comparing */ + current->state = TASK_INTERRUPTIBLE; + schedule_timeout(HZ + HZ / 2); + + alarm = gl518_read_value(client, GL518_REG_INT);So you sleep for 1.5 seconds 10 times? Why? That seems very slow...
Rev0 chip does not allow for direct reading of in[1-2]. Somehow, the author has found a comparison method of deriving those values indirectly. As I stated above, it will take ~10s to complete.
As for the threaded method of reading the values, it would be up to team to decide whether to remove or keep it. I will update the code accordingly.
Hong-Gunn
Quoted 9 lines
Review
Quoted 6 lines
+/* Defining this will enable debug messages for the voltage
iteration+ code used with rev 0 ICs */(quoted diff) +4
-0+#undef DEBUG_VIN +#ifdef DEBUG +#define DEBUG_VIN +#endifAs you always tie
DEBUG_VINto DEBUG, why even have it at all? Just get rid of it, and everywhere it's defined.
I was wondering. Since it seemed that specific debug was wanted for this part of the code, I thought it would make sense to keep a separate define. Yes, it is set when DEBUG itself is, but could easily be disabled with DEBUG still set, or set when DEBUG isn't.
Anyway, I think that this code will go away, so there's no point discussing this anymore.
Here are the things as I understood them. Please correct me if I am mistaking.
Release 0x00 of the chipset doesn't support reading in0-in2 values. A workaround was implemented into the driver. The idea of it is to change the min and max limits of these channels, and watch wether an alarm is triggered. After a few iterations, you can get an idea of the approximative value of the reading.
Since the iteration process is slow, an option was introduced to use it or not, and possibly trigger it as a background job (thus the thread code). Note that when the iteration process is run in the background, the driver will return the previous value so that the caller doesn't have to wait for the iterations to finish.
I sure don't want to see this into the 2.6 kernel. The chip doesn't support reading these values? It doesn't support it. Period. So we don't report them. It's that simple. 160 lines of complex code (20% of the driver code) simply there to workaround a chip design issue is more than arguable. What's more, I think we all agree that what really needs to be monitored are temperatures and fans. If the voltages are bad, the alarm will trigger. You don't really need to know how bad they were. If people really want to know, well, they can do the limits trick from user space.
If anyone wants this "feature" to be kept, speak up. Else let's get rid of it. The way we will handle it in libsensors and sensors is a different problem, I'll take a look right now.
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
A few more comments and random thoughts about the gl518sm driver:
* Can you confirm that you do have a GL518SM chipset for testing and use? Please also tell us which release it is. This is an old chip and I am surprised that someone still uses it. I think we should only port to Linux 2.6 drivers that people need. We better don't work for nobody, and also don't clutter the kernel tree with useless code.
* Can you confirm that the 2.4 driver works OK for you? Just to make sure that the code we are starting from is working.
* Your new driver counts voltages starting at 1, instead of 0. This will break compatibility with existing code and needs to be fixed (I will do).
* Your new driver defines two different names for the two different releases of the chip. This breaks compatibility with libsensors, and would better be fixed (I will do too).
* The "iteration" code increases the driver code size by one third (not one fifth as I initially announced). One more reason to get rid of it (I already did it, to say the truth).
* The more simple way to handle the two different releases of the chip is probably to export 0 as the readings for in0-in2 in release 0x00. I had first thought of no file at all, or non-readable files, but this may be more confusing than helpful, and this will make things more complex in libsensors. MMH reminded me that sensors.conf is there to allow people to ignore unused inputs, so we can just do that. Minimal work, maximum compatibility. It's simply adding a "if these read 0, just ignore them" comment in sensors.conf and chip doc. If someone can think of a better and cleaner way of doing it, please speak up.
I should have something for you to test within a day.
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
Quoted 1 line
I should have something for you to test within a day.
Less than that actually ;)
All things previously "discussed" are inside. I also allowed writing negative temperature limits, which your original port would have forbidden if I'm not mistaking.
Please test as much as you can and report how it is going. Thanks.
Signature · 628 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/ -------------- next part -------------- diff -ruN linux-2.6.2-rc2/drivers/i2c/chips/Kconfig linux-2.6.2-rc2-k1/drivers/i2c/chips/Kconfig --- linux-2.6.2-rc2/drivers/i2c/chips/Kconfig Tue Jan 27 19:22:59 2004 +++ linux-2.6.2-rc2-k1/drivers/i2c/chips/Kconfig Thu Jan 29 20:52:15 2004 @@ -45,6 +45,17 @@ This driver can also be built as a module. If so, the module will be called eeprom. +config SENSORS_GL518SM + tristate "Genesys Logic GL518SM" + depends on I2C && EXPERIMENTAL + select I2C_SENSOR + help + If you say yes here you get support for Genesys Logic GL518SM + sensor chips. + + This driver can also be built as a module. If so, the module + will be called gl518sm. + config SENSORS_IT87 tristate "ITE IT87xx and compatibles" depends on I2C && EXPERIMENTAL diff -ruN linux-2.6.2-rc2/drivers/i2c/chips/Makefile linux-2.6.2-rc2-k1/drivers/i2c/chips/Makefile --- linux-2.6.2-rc2/drivers/i2c/chips/Makefile Tue Jan 27 19:22:59 2004 +++ linux-2.6.2-rc2-k1/drivers/i2c/chips/Makefile Wed Jan 28 21:48:20 2004 @@ -8,6 +8,7 @@ obj-$(CONFIG_SENSORS_ADM1021) += adm1021.o obj-$(CONFIG_SENSORS_EEPROM) += eeprom.o +obj-$(CONFIG_SENSORS_GL518SM) += gl518sm.o obj-$(CONFIG_SENSORS_IT87) += it87.o obj-$(CONFIG_SENSORS_LM75) += lm75.o obj-$(CONFIG_SENSORS_LM78) += lm78.o diff -ruN linux-2.6.2-rc2/drivers/i2c/chips/gl518sm.c linux-2.6.2-rc2-k1/drivers/i2c/chips/gl518sm.c --- linux-2.6.2-rc2/drivers/i2c/chips/gl518sm.c Thu Jan 1 01:00:00 1970 +++ linux-2.6.2-rc2-k1/drivers/i2c/chips/gl518sm.c Thu Jan 29 22:35:36 2004 @@ -0,0 +1,595 @@ +/* + * gl518sm.c - Part of lm_sensors, Linux kernel modules for hardware + * monitoring + * Copyright (C) 1998, 1999 Frodo Looijaard <[email protected]>, + * Kyosti Malkki <[email protected]> + * port to 2.6 Kernel + * Copyright (C) 2004 Hong-Gunn Chew <[email protected]> + * + * 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. + */ + +/* + * v2.0 (2004-01-24, Hong-Gunn Chew) + * - ported to 2.6 kernel + * v2.1 (2004-01-28, Hong-Gunn Chew) + * - Updated sysfs output according to standard data format + * (temp, in) + */ + +#include <linux/config.h> +#ifdef CONFIG_I2C_DEBUG_CHIP +#define DEBUG 1 +#endif + +#include <linux/module.h> +#include <linux/init.h> +#include <linux/slab.h> +#include <linux/i2c.h> +#include <linux/i2c-sensor.h> + +/* Addresses to scan */ +static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END }; +static unsigned short normal_i2c_range[] = { 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_2(gl518sm_r00, gl518sm_r80); + +/* Many GL518 constants specified below */ + +/* The GL518 registers */ +#define GL518_REG_CHIP_ID 0x00 +#define GL518_REG_REVISION 0x01 +#define GL518_REG_VENDOR_ID 0x02 +#define GL518_REG_CONF 0x03 +#define GL518_REG_TEMP_IN 0x04 +#define GL518_REG_TEMP_MAX 0x05 +#define GL518_REG_TEMP_HYST 0x06 +#define GL518_REG_FAN_COUNT 0x07 +#define GL518_REG_FAN_LIMIT 0x08 +#define GL518_REG_VIN1_LIMIT 0x09 +#define GL518_REG_VIN2_LIMIT 0x0a +#define GL518_REG_VIN3_LIMIT 0x0b +#define GL518_REG_VDD_LIMIT 0x0c +#define GL518_REG_VIN3 0x0d +#define GL518_REG_MISC 0x0f +#define GL518_REG_ALARM 0x10 +#define GL518_REG_MASK 0x11 +#define GL518_REG_INT 0x12 +#define GL518_REG_VIN2 0x13 +#define GL518_REG_VIN1 0x14 +#define GL518_REG_VDD 0x15 + + +/* + * Conversions. Rounding and limit checking is only done on the TO_REG + * variants. Note that you should be a bit careful with which arguments + * these macros are called: arguments may be evaluated more than once. + * Fixing this is just not worth it. + */ + +#define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0? \ + (val)-500:(val)+500)/1000)+119),0,255)) +#define TEMP_FROM_REG(val) (((val) - 119) * 1000) + +static inline u8 FAN_TO_REG(long rpm, int div) +{ + if (rpm = 0) + return 255; + rpm = SENSORS_LIMIT(rpm, 1, 1000000); + return SENSORS_LIMIT((960000 + rpm * div / 2) / (rpm * div), 1, 254); +} + +#define FAN_FROM_REG(val,div) ((val)=0 ? 0 : (val)=255 ? 0 : \ + (960000/((val)*(div)))) + +#define IN_TO_REG(val) (SENSORS_LIMIT((((val)+8)/19),0,255)) +#define IN_FROM_REG(val) ((val)*19) + +#define VDD_TO_REG(val) (SENSORS_LIMIT((((val)+11)/23),0,255)) +#define VDD_FROM_REG(val) ((val)*23) + +#define DIV_TO_REG(val) ((val)=8?3:(val)=4?2:(val)=1?0:1) +#define DIV_FROM_REG(val) (1 << (val)) + +#define ALARMS_FROM_REG(val) val + +#define BEEP_ENABLE_TO_REG(val) ((val)?0:1) +#define BEEP_ENABLE_FROM_REG(val) ((val)?0:1) + +#define BEEP_MASK_TO_REG(val) ((val) & 0x7f & data->alarm_mask) +#define BEEP_MASK_FROM_REG(val) ((val) & 0x7f) + +/* Each client has this additional data */ +struct gl518_data { + enum chips type; + + struct semaphore update_lock; + char valid; /* !=0 if following fields are valid */ + unsigned long last_updated; /* In jiffies */ + + u8 voltage_in[4]; /* Register values; [0] = VDD */ + u8 voltage_min[4]; /* Register values; [0] = VDD */ + u8 voltage_max[4]; /* Register values; [0] = VDD */ + u8 iter_voltage_in[4]; /* Register values; [0] = VDD */ + u8 fan_in[2]; + u8 fan_min[2]; + u8 fan_div[2]; /* Register encoding, shifted right */ + u8 temp_in; /* Register values */ + u8 temp_max; /* Register values */ + u8 temp_hyst; /* Register values */ + u8 alarms; /* Register value */ + u8 alarm_mask; /* Register value */ + u8 beep_mask; /* Register value */ + u8 beep_enable; /* Boolean */ +}; + +static int gl518_attach_adapter(struct i2c_adapter *adapter); +static int gl518_detect(struct i2c_adapter *adapter, int address, int kind); +static void gl518_init_client(struct i2c_client *client); +static int gl518_detach_client(struct i2c_client *client); +static int gl518_read_value(struct i2c_client *client, u8 reg); +static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value); +static void gl518_update_client(struct i2c_client *client); + +/* This is the driver that will be inserted */ +static struct i2c_driver gl518_driver = { + .owner = THIS_MODULE, + .name = "gl518sm", + .id = I2C_DRIVERID_GL518, + .flags = I2C_DF_NOTIFY, + .attach_adapter = gl518_attach_adapter, + .detach_client = gl518_detach_client, +}; + +/* + * Internal variables + */ + +static int gl518_id = 0; + +/* + * Sysfs stuff + */ + +#define show(type, suffix, value) \ +static ssize_t show_##suffix(struct device *dev, char *buf) \ +{ \ + struct i2c_client *client = to_i2c_client(dev); \ + struct gl518_data *data = i2c_get_clientdata(client); \ + gl518_update_client(client); \ + return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \ +} + +#define show_fan(suffix, value, index) \ +static ssize_t show_##suffix(struct device *dev, char *buf) \ +{ \ + struct i2c_client *client = to_i2c_client(dev); \ + struct gl518_data *data = i2c_get_clientdata(client); \ + gl518_update_client(client); \ + return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[index], \ + DIV_FROM_REG(data->fan_div[index]))); \ +} + +show(TEMP, temp_input, temp_in); +show(TEMP, temp_max, temp_max); +show(TEMP, temp_hyst, temp_hyst); +show_fan(fan_input1, fan_in, 0); +show_fan(fan_input2, fan_in, 1); +show_fan(fan_min1, fan_min, 0); +show_fan(fan_min2, fan_min, 1); +show(DIV, fan_div1, fan_div[0]); +show(DIV, fan_div2, fan_div[1]); +show(VDD, in_input0, voltage_in[0]); +show(IN, in_input1, voltage_in[1]); +show(IN, in_input2, voltage_in[2]); +show(IN, in_input3, voltage_in[3]); +show(VDD, in_min0, voltage_min[0]); +show(IN, in_min1, voltage_min[1]); +show(IN, in_min2, voltage_min[2]); +show(IN, in_min3, voltage_min[3]); +show(VDD, in_max0, voltage_max[0]); +show(IN, in_max1, voltage_max[1]); +show(IN, in_max2, voltage_max[2]); +show(IN, in_max3, voltage_max[3]); +show(ALARMS, alarms, alarms); +show(BEEP_ENABLE, beep_enable, beep_enable); +show(BEEP_MASK, beep_mask, beep_mask); + +#define set(type, suffix, value, reg) \ +static ssize_t set_##suffix(struct device *dev, const char *buf, \ + size_t count) \ +{ \ + struct i2c_client *client = to_i2c_client(dev); \ + struct gl518_data *data = i2c_get_clientdata(client); \ + data->value = type##_TO_REG(simple_strtol(buf, NULL, 10)); \ + gl518_write_value(client, reg, data->value); \ + return count; \ +} + +#define set_bits(type, suffix, value, reg, mask, shift) \ +static ssize_t set_##suffix(struct device *dev, const char *buf, \ + size_t count) \ +{ \ + struct i2c_client *client = to_i2c_client(dev); \ + struct gl518_data *data = i2c_get_clientdata(client); \ + int regvalue = gl518_read_value(client, reg); \ + data->value = type##_TO_REG(simple_strtoul(buf, NULL, 10)); \ + regvalue = (regvalue & mask) | (data->value << shift); \ + gl518_write_value(client, reg, regvalue); \ + return count; \ +} + +#define set_low(type, suffix, value, reg) \ + set_bits(type, suffix, value, reg, 0xff00, 0) +#define set_high(type, suffix, value, reg) \ + set_bits(type, suffix, value, reg, 0x00ff, 8) + +set(TEMP, temp_max, temp_max, GL518_REG_TEMP_MAX); +set(TEMP, temp_hyst, temp_hyst, GL518_REG_TEMP_HYST); +set_bits(DIV, fan_div1, fan_div[0], GL518_REG_MISC, 0x3f, 4); +set_bits(DIV, fan_div2, fan_div[1], GL518_REG_MISC, 0xcf, 6); +set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT); +set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT); +set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT); +set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT); +set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT); +set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT); +set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT); +set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT); +set_bits(BEEP_ENABLE, beep_enable, beep_enable, GL518_REG_CONF, 0xfb, 2); +set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM); + +static ssize_t set_fan_min1(struct device *dev, const char *buf, size_t count) +{ + struct i2c_client *client = to_i2c_client(dev); + struct gl518_data *data = i2c_get_clientdata(client); + int regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT); + + data->fan_min[0] = FAN_TO_REG(simple_strtoul(buf, NULL, 10), + DIV_FROM_REG(data->fan_div[0])); + regvalue = (regvalue & 0xff00) | data->fan_min[0]; + if (data->fan_min[0] = 0) + data->alarm_mask &= ~0x0020; + else + data->alarm_mask |= 0x0020; + gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue); + + return count; +} + +static ssize_t set_fan_min2(struct device *dev, const char *buf, size_t count) +{ + struct i2c_client *client = to_i2c_client(dev); + struct gl518_data *data = i2c_get_clientdata(client); + int regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT); + + data->fan_min[1] = FAN_TO_REG(simple_strtoul(buf, NULL, 10), + DIV_FROM_REG(data->fan_div[0])); + regvalue = (regvalue & 0x00ff) | (data->fan_min[1] << 8); + if (data->fan_min[1] = 0) + data->alarm_mask &= ~0x0040; + else + data->alarm_mask |= 0x0040; + gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue); + + return count; +} + +static DEVICE_ATTR(temp_input, S_IRUGO, show_temp_input, NULL); +static DEVICE_ATTR(temp_max, S_IWUSR|S_IRUGO, show_temp_max, set_temp_max); +static DEVICE_ATTR(temp_hyst, S_IWUSR|S_IRUGO, show_temp_hyst, set_temp_hyst); +static DEVICE_ATTR(fan_input1, S_IRUGO, show_fan_input1, NULL); +static DEVICE_ATTR(fan_input2, S_IRUGO, show_fan_input2, NULL); +static DEVICE_ATTR(fan_min1, S_IWUSR|S_IRUGO, show_fan_min1, set_fan_min1); +static DEVICE_ATTR(fan_min2, S_IWUSR|S_IRUGO, show_fan_min2, set_fan_min2); +static DEVICE_ATTR(fan_div1, S_IWUSR|S_IRUGO, show_fan_div1, set_fan_div1); +static DEVICE_ATTR(fan_div2, S_IWUSR|S_IRUGO, show_fan_div2, set_fan_div2); +static DEVICE_ATTR(in_input0, S_IRUGO, show_in_input0, NULL); +static DEVICE_ATTR(in_input1, S_IRUGO, show_in_input1, NULL); +static DEVICE_ATTR(in_input2, S_IRUGO, show_in_input2, NULL); +static DEVICE_ATTR(in_input3, S_IRUGO, show_in_input3, NULL); +static DEVICE_ATTR(in_min0, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0); +static DEVICE_ATTR(in_min1, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1); +static DEVICE_ATTR(in_min2, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2); +static DEVICE_ATTR(in_min3, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3); +static DEVICE_ATTR(in_max0, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0); +static DEVICE_ATTR(in_max1, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1); +static DEVICE_ATTR(in_max2, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2); +static DEVICE_ATTR(in_max3, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3); +static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); +static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO, + show_beep_enable, set_beep_enable); +static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO, + show_beep_mask, set_beep_mask); + +/* + * Real code + */ + +static int gl518_attach_adapter(struct i2c_adapter *adapter) +{ + if (!(adapter->class & I2C_ADAP_CLASS_SMBUS)) + return 0; + return i2c_detect(adapter, &addr_data, gl518_detect); +} + +static int gl518_detect(struct i2c_adapter *adapter, int address, int kind) +{ + int i; + struct i2c_client *new_client; + struct gl518_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; + + /* 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 gl518_{read,write}_value. */ + + if (!(new_client = kmalloc(sizeof(struct i2c_client) + + sizeof(struct gl518_data), + GFP_KERNEL))) { + err = -ENOMEM; + goto exit; + } + memset(new_client, 0x00, sizeof(struct i2c_client) + + sizeof(struct gl518_data)); + + data = (struct gl518_data *) (new_client + 1); + i2c_set_clientdata(new_client, data); + + new_client->addr = address; + new_client->adapter = adapter; + new_client->driver = &gl518_driver; + new_client->flags = 0; + + /* Now, we do the remaining detection. */ + + if (kind < 0) { + if ((gl518_read_value(new_client, GL518_REG_CHIP_ID) != 0x80) + || (gl518_read_value(new_client, GL518_REG_CONF) & 0x80)) + goto exit_free; + } + + /* Determine the chip type. */ + if (kind <= 0) { + i = gl518_read_value(new_client, GL518_REG_REVISION); + if (i = 0x00) { + kind = gl518sm_r00; + name = "gl518sm"; + } else if (i = 0x80) { + kind = gl518sm_r80; + name = "gl518sm"; + } else { + if (kind <= 0) + dev_info(&adapter->dev, + "Ignoring 'force' parameter for unknown " + "chip at adapter %d, address 0x%02x\n", + i2c_adapter_id(adapter), address); + goto exit_free; + } + } + + /* Fill in the remaining client fields */ + strlcpy(new_client->name, name, I2C_NAME_SIZE); + new_client->id = gl518_id++; + data->type = kind; + 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 GL518SM chip */ + data->alarm_mask = 0xff; + data->voltage_in[0]Úta->voltage_in[1]Úta->voltage_in[2]=0; + gl518_init_client((struct i2c_client *) new_client); + + /* Register sysfs hooks */ + device_create_file(&new_client->dev, &dev_attr_in_input0); + device_create_file(&new_client->dev, &dev_attr_in_input1); + device_create_file(&new_client->dev, &dev_attr_in_input2); + device_create_file(&new_client->dev, &dev_attr_in_input3); + device_create_file(&new_client->dev, &dev_attr_in_min0); + device_create_file(&new_client->dev, &dev_attr_in_min1); + device_create_file(&new_client->dev, &dev_attr_in_min2); + device_create_file(&new_client->dev, &dev_attr_in_min3); + device_create_file(&new_client->dev, &dev_attr_in_max0); + device_create_file(&new_client->dev, &dev_attr_in_max1); + device_create_file(&new_client->dev, &dev_attr_in_max2); + device_create_file(&new_client->dev, &dev_attr_in_max3); + device_create_file(&new_client->dev, &dev_attr_fan_input1); + device_create_file(&new_client->dev, &dev_attr_fan_input2); + device_create_file(&new_client->dev, &dev_attr_fan_min1); + device_create_file(&new_client->dev, &dev_attr_fan_min2); + device_create_file(&new_client->dev, &dev_attr_fan_div1); + device_create_file(&new_client->dev, &dev_attr_fan_div2); + device_create_file(&new_client->dev, &dev_attr_temp_input); + device_create_file(&new_client->dev, &dev_attr_temp_max); + device_create_file(&new_client->dev, &dev_attr_temp_hyst); + device_create_file(&new_client->dev, &dev_attr_alarms); + device_create_file(&new_client->dev, &dev_attr_beep_enable); + device_create_file(&new_client->dev, &dev_attr_beep_mask); + + return 0; + +/* OK, this is not exactly good programming practice, usually. But it is + very code-efficient in this case. */ + +exit_free: + kfree(new_client); +exit: + return err; +} + + +/* Called when we have found a new GL518SM. */ +static void gl518_init_client(struct i2c_client *client) +{ + /* Power-on defaults (bit 7=1) */ + gl518_write_value(client, GL518_REG_CONF, 0x80); + + /* No noisy output (bit 2=1), Comparator mode (bit 3=0), two fans (bit4=0), + standby mode (bit6=0) */ + gl518_write_value(client, GL518_REG_CONF, 0x04); + + /* Never interrupts */ + gl518_write_value(client, GL518_REG_MASK, 0x00); + + /* Clear status register (bit 5=1), start (bit6=1) */ + gl518_write_value(client, GL518_REG_CONF, 0x24); + gl518_write_value(client, GL518_REG_CONF, 0x44); +} + +static int gl518_detach_client(struct i2c_client *client) +{ + int err; + + if ((err = i2c_detach_client(client))) { + dev_err(&client->dev, "Client deregistration failed, " + "client not detached.\n"); + return err; + } + + kfree(client); + + return 0; +} + +static inline u16 swap_bytes(u16 val) +{ + return (val >> 8) | (val << 8); +} + +/* Registers 0x07 to 0x0c are word-sized, others are byte-sized + GL518 uses a high-byte first convention, which is exactly opposite to + the usual practice. */ +static int gl518_read_value(struct i2c_client *client, u8 reg) +{ + if ((reg >= 0x07) && (reg <= 0x0c)) + return swap_bytes(i2c_smbus_read_word_data(client, reg)); + else + return i2c_smbus_read_byte_data(client, reg); +} + +/* Registers 0x07 to 0x0c are word-sized, others are byte-sized + GL518 uses a high-byte first convention, which is exactly opposite to + the usual practice. */ +static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value) +{ + if ((reg >= 0x07) && (reg <= 0x0c)) + return i2c_smbus_write_word_data(client, reg, + swap_bytes(value)); + else + return i2c_smbus_write_byte_data(client, reg, value); +} + +static void gl518_update_client(struct i2c_client *client) +{ + struct gl518_data *data = i2c_get_clientdata(client); + int val; + + down(&data->update_lock); + + if ((jiffies - data->last_updated > HZ + HZ / 2) || + (jiffies < data->last_updated) || !data->valid) { + dev_dbg(&client->dev, "Starting gl518 update\n"); + + data->alarms = gl518_read_value(client, GL518_REG_INT); + data->beep_mask = gl518_read_value(client, GL518_REG_ALARM); + + val = gl518_read_value(client, GL518_REG_VDD_LIMIT); + data->voltage_min[0] = val & 0xff; + data->voltage_max[0] = (val >> 8) & 0xff; + val = gl518_read_value(client, GL518_REG_VIN1_LIMIT); + data->voltage_min[1] = val & 0xff; + data->voltage_max[1] = (val >> 8) & 0xff; + val = gl518_read_value(client, GL518_REG_VIN2_LIMIT); + data->voltage_min[2] = val & 0xff; + data->voltage_max[2] = (val >> 8) & 0xff; + val = gl518_read_value(client, GL518_REG_VIN3_LIMIT); + data->voltage_min[3] = val & 0xff; + data->voltage_max[3] = (val >> 8) & 0xff; + + val = gl518_read_value(client, GL518_REG_FAN_COUNT); + data->fan_in[0] = (val >> 8) & 0xff; + data->fan_in[1] = val & 0xff; + + val = gl518_read_value(client, GL518_REG_FAN_LIMIT); + data->fan_min[0] = (val >> 8) & 0xff; + data->fan_min[1] = val & 0xff; + + data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN); + data->temp_max + gl518_read_value(client, GL518_REG_TEMP_MAX); + data->temp_hyst + gl518_read_value(client, GL518_REG_TEMP_HYST); + + val = gl518_read_value(client, GL518_REG_MISC); + data->fan_div[0] = (val >> 6) & 0x03; + data->fan_div[1] = (val >> 4) & 0x03; + + data->alarms &= data->alarm_mask; + + val = gl518_read_value(client, GL518_REG_CONF); + data->beep_enable = (val >> 2) & 1; + + if (data->type = gl518sm_r00) { + data->voltage_in[0] = 0; + data->voltage_in[1] = 0; + data->voltage_in[2] = 0; + } else { + data->voltage_in[0] + gl518_read_value(client, GL518_REG_VDD); + data->voltage_in[1] + gl518_read_value(client, GL518_REG_VIN1); + data->voltage_in[2] + gl518_read_value(client, GL518_REG_VIN2); + } + data->voltage_in[3] + gl518_read_value(client, GL518_REG_VIN3); + + data->last_updated = jiffies; + data->valid = 1; + } + + up(&data->update_lock); +} + +static int __init sensors_gl518sm_init(void) +{ + return i2c_add_driver(&gl518_driver); +} + +static void __exit sensors_gl518sm_exit(void) +{ + i2c_del_driver(&gl518_driver); +} + +MODULE_AUTHOR("Frodo Looijaard <[email protected]>, " + "Kyosti Malkki <[email protected]> and " + "Hong-Gunn Chew <[email protected]>"); +MODULE_DESCRIPTION("GL518SM driver"); +MODULE_LICENSE("GPL"); + +module_init(sensors_gl518sm_init); +module_exit(sensors_gl518sm_exit);
Thanks for the changes and comments.
Quoted 5 lines
* Can you confirm that you do have a GL518SM chipset for testing and use? Please also tell us which release it is. This is an old chip and I am surprised that someone still uses it. I think we should only port to Linux 2.6 drivers that people need. We better don't work for nobody, and also don't clutter the kernel tree with useless code.
Yes, I have the gl518sm chip, rev 00. It is on a board with the Intel LX chipset for the PII, so there should still be people using it. It works fine as a file server/internet gateway.
Quoted 2 lines
* Can you confirm that the 2.4 driver works OK for you? Just to make sure that the code we are starting from is working.
Yes, the 2.4 driver works OK.
Quoted 3 lines
* Your new driver defines two different names for the two different releases of the chip. This breaks compatibility with libsensors, and would better be fixed (I will do too).
Ok. It was there to differentiate the two revisions.
Quoted 3 lines
* The "iteration" code increases the driver code size by one third (not one fifth as I initially announced). One more reason to get rid of it (I already did it, to say the truth).
I agree that it is not necessary and should be removed from the kernel even if it does work.
I have tested rc1 with and without DEBUG on the rev00 chip, and the iteration code works fine (if it were to remain).
I have tested rc2 and there are no problems with it. I did however make two minor changes to it:
* added swap_bytes function declaration (unless you deleted it intentionally)
* removed voltage_in[0-2] = 0 for rev00 chips
these are initialised to 0 on init, so there should be no need to set them at each update.
Regards,
Hong-Gunn
-------------- next part --------------
gl518sm.c renamed from gl518sm.c.rc2 +2 -5
@@ -145,6 +145,7 @@static int gl518_read_value (struct i2c_client *client,u8 reg);static int gl518_write_value (struct i2c_client *client,u8 reg,u16 value);static void gl518_update_client (struct i2c_client *client); +static inline u16 swap_bytes (u16 val);/* This is the driver that will be inserted */ static struct i2c_driver gl518_driver = {
@@ -553,11 +554,7 @@ val =gl518_read_value (client,GL518_REG_CONF ); data->beep_enable = (val >>2 ) &1 ; -if (data->type = gl518sm_r00) { - data->voltage_in[0 ] =0 ; - data->voltage_in[1 ] =0 ; - data->voltage_in[2 ] =0 ; - }else { +if (data->type != gl518sm_r00) { data->voltage_in[0] gl518_read_value(client, GL518_REG_VDD); data->voltage_in[1 ]
Hi Hong-Gunn,
I've updated libsensors and sensors to support the 2.6 gl518sm driver. Could you please checkout lm_sensors CVS and give it a try?
Also, I'd like you to make sure that the 2.4 driver still works with the new libsensors and sensors.
Thanks.
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
Hi Jean,
Quoted 2 lines
I've updated libsensors and sensors to support the 2.6 gl518sm driver. Could you please checkout
lm_sensorsCVS and give it a try?
I have tested it out, and there are 2 issues:
* libsensors is reading from 'in0' instead of 'in_input0'
- I have edited chips.c and attached the patch
* libsensors is reading from 'temp_input1' but gl518sm provides 'temp_input'
- I have edited the driver to use 'temp_*' instead.
- however, it might be confusing to read from 'temp_input1', but specifying it as 'temp_{max,hyst}' in the config files. It might be better to append '1' for all 'temp*'
Quoted 2 lines
Also, I'd like you to make sure that the 2.4 driver still works with the new libsensors and sensors.
I don't think I will be able to test the 2.4 drivers for a few weeks. I will be going overseas for 3 weeks and being a server, it takes a bit of effort to bring it down for a kernel change. I am happy to do it when I get back though. I am still able to work on the driver but using the current kernel.
The above was tested on fedora development's kernel-2.6.1-1.57, while the previous tests were on 2.6.3-rc2.
I have also updated gl518sm.c:
- I have found a bug due to me misreading the old code. It affects only fan_div*.
- I am thinking of replacing '(val >> 4) & 0x0f' with '(val & 0xf0) >> $)' as it allows easy checking with the chip specifications (the mask in particular). I have changed a few, and if there are no objections, I will be changing the rest.
- I have added fan_auto1 that switches fan1 between [true] always on and [false] auto on(30C)/off(25C). This was removed from the port as it did not a standard item, and I didn't know what to do with it.
- I have a question regarding the init of the driver. Is there a need to init the chip? The BIOS should have initialised the chip, and we only need to make sure it is enabled. Initialising the chip resets the config such as the min/max which the BIOS should have set, as well as other hardware dependent options such as 'fan2off' in the old driver. My understanding is that we should not be able to write 'fan2off' as it depends on the hardware implementation. So I am inclined not to reset the chip on init of the driver.
The patch is attached and based on linux-2.6.2-rc2-i2c-gl518sm-rc2.diff.
I hope to hear your comments.
Cheers,
Hong-Gunn
-------------- next part --------------
lib/chips.c +1 -2
@@ -419,7 +419,7 @@static sensors_chip_feature gl518_features[] { {SENSORS_GL518_VDD ,"vdd" ,NOMAP ,NOMAP , - R, GL518_SYSCTL_VDD, VALUE(3), 2, "in0", 3 }, + R, GL518_SYSCTL_VDD, VALUE(3), 2, "in_input0", 3 }, { SENSORS_GL518_VIN1, "vin1", NOMAP,NOMAP,R ,GL518_SYSCTL_VIN1 ,VALUE (3 ), 2 }, -------------- next part --------------
gl518sm.c renamed from gl518sm.c.rc2 +49 -48
202 lines not on this page · raw message
Quoted 2 lines
* libsensors is reading from 'in0' instead of '
in_input0'
- I have editedchips.cand attached the patch
Stupid me. Applied.
Quoted 3 lines
* libsensors is reading from '
temp_input1' but gl518sm provides 'temp_input'
- I have edited the driver to use 'temp_*' instead.
I guess you mean 'temp_*1'.
Quoted 3 lines
- however, it might be confusing to read from '
temp_input1', but specifying it as 'temp_{max,hyst}' in the config files. It might be better to append '1' for all 'temp*'
This would require changes to the library and default configuration file. Not worth it IMHO. I don't even find it confusing the way it is. People are not supposed to access sysfs files directly anyway.
Quoted 5 lines
I don't think I will be able to test the 2.4 drivers for a few weeks. I will be going overseas for 3 weeks and being a server, it takes a bit of effort to bring it down for a kernel change. I am happy to do it when I get back though. I am still able to work on the driver but using the current kernel.
No problem. The more important is that we get a good driver into Linux 2.6. The chances we break compatibility with the older driver are low, and if this happens and someone notices it, let him/her speak up.
Quoted 3 lines
I have also updated
gl518sm.c:
- I have found a bug due to me misreading the old code. It affects only fan_div*.
Don't you mean fan_min*?
Quoted 4 lines
- I am thinking of replacing '(val >> 4) &
0x0f' with '(val &0xf0) >> 4' as it allows easy checking with the chip specifications (the mask in particular). I have changed a few, and if there are no objections, I will be changing the rest.
Frankly, I don't see no benefit. I don't find it more nor less readable one way or one other - so I wouldn't bother changing, especially since it is likely to introduce errors.
Quoted 3 lines
- I have added
fan_auto1that switches fan1 between [true] always on and [false] auto on(30C)/off(25C). This was removed from the port as it did not a standard item, and I didn't know what to do with it.
This is fine. There's nothing wrong with not being standard. What's important is to respect what is defined in the standard. The rest is more or less free as long as it fits in the spirit. If new things would need to be standardized, we'll extend the standard.
I've made the necessary change to libsensors so that it knows about your fan_auto1. That way, "sensors -s" shouldn't complain about it anymore.
A few comments will need to be added to sensors.conf.eg to explain the differences between the 2.4 and 2.6 drivers, and how the auto fan thing works. I will do that right after the new driver is finished and submitted.
Quoted 8 lines
- I have a question regarding the init of the driver. Is there a need to init the chip? The BIOS should have initialised the chip, and we only need to make sure it is enabled. Initialising the chip resets the config such as the min/max which the BIOS should have set, as well as other hardware dependent options such as 'fan2off' in the old driver. My understanding is that we should not be able to write 'fan2off' as it depends on the hardware implementation. So I am inclined not to reset the chip on init of the driver.
I fully agree with you here. I am fighting against systematic chip resets since the day I wrote my first chip driver.
Now, a few more comments about your patch:
Review line 282
Quoted 4 lines
(quoted diff) +0
-0@@ -280,19 +280,21 @@ data->fan_min[1] = FAN_TO_REG(simple_strtoul(buf, NULL, 10), DIV_FROM_REG(data->fan_div[0]));
^
Isn't it a bug?
Review line 285
Quoted 4 lines
(quoted diff) +2
-1val = gl518_read_value(client, GL518_REG_CONF); - data->beep_enable = (val >> 2) & 1; + data->beep_enable = (val & 0x04) >> 2; + data->fan_auto1 |= (val & 0x10) >> 3;
What's the point of the last line? For one thing, it seems to be bogus (the mask doesn't match the shift). For another, I don't really see what it is supposed to do.
I applied everything from your patch but this line and the shift/mask permutations, and fixed what I suspected to be a bug. Full patch attached.
I leave it to you to rework the init function. Remove the chip reset and any other change that is done and should not. Especially the NoFan2 bit of the configuration register should be left untouched IMHO.
Signature · 638 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/ -------------- next part -------------- diff -ruN linux-2.6.2-rc2/drivers/i2c/chips/Kconfig linux-2.6.2-rc2-k1/drivers/i2c/chips/Kconfig --- linux-2.6.2-rc2/drivers/i2c/chips/Kconfig Tue Jan 27 19:22:59 2004 +++ linux-2.6.2-rc2-k1/drivers/i2c/chips/Kconfig Thu Jan 29 20:52:15 2004 @@ -45,6 +45,17 @@ This driver can also be built as a module. If so, the module will be called eeprom. +config SENSORS_GL518SM + tristate "Genesys Logic GL518SM" + depends on I2C && EXPERIMENTAL + select I2C_SENSOR + help + If you say yes here you get support for Genesys Logic GL518SM + sensor chips. + + This driver can also be built as a module. If so, the module + will be called gl518sm. + config SENSORS_IT87 tristate "ITE IT87xx and compatibles" depends on I2C && EXPERIMENTAL diff -ruN linux-2.6.2-rc2/drivers/i2c/chips/Makefile linux-2.6.2-rc2-k1/drivers/i2c/chips/Makefile --- linux-2.6.2-rc2/drivers/i2c/chips/Makefile Tue Jan 27 19:22:59 2004 +++ linux-2.6.2-rc2-k1/drivers/i2c/chips/Makefile Wed Jan 28 21:48:20 2004 @@ -8,6 +8,7 @@ obj-$(CONFIG_SENSORS_ADM1021) += adm1021.o obj-$(CONFIG_SENSORS_EEPROM) += eeprom.o +obj-$(CONFIG_SENSORS_GL518SM) += gl518sm.o obj-$(CONFIG_SENSORS_IT87) += it87.o obj-$(CONFIG_SENSORS_LM75) += lm75.o obj-$(CONFIG_SENSORS_LM78) += lm78.o diff -ruN linux-2.6.2-rc2/drivers/i2c/chips/gl518sm.c linux-2.6.2-rc2-k1/drivers/i2c/chips/gl518sm.c --- linux-2.6.2-rc2/drivers/i2c/chips/gl518sm.c Thu Jan 1 01:00:00 1970 +++ linux-2.6.2-rc2-k1/drivers/i2c/chips/gl518sm.c Sat Jan 31 17:32:04 2004 @@ -0,0 +1,605 @@ +/* + * gl518sm.c - Part of lm_sensors, Linux kernel modules for hardware + * monitoring + * Copyright (C) 1998, 1999 Frodo Looijaard <[email protected]> and + * Kyosti Malkki <[email protected]> + * Copyright (C) 2004 Hong-Gunn Chew <[email protected]> and + * Jean Delvare <[email protected]> + * + * 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. + * + * Ported to Linux 2.6 by Hong-Gunn Chew with the help of Jean Delvare + * and advice of Greg Kroah-Hartman. + * + * Notes about the port: + * Release 0x00 of the GL518SM chipset doesn't support reading of in0, + * in1 nor in2. The original driver had an ugly workaround to get them + * anyway (changing limits and watching alarms trigger and wear off). + * We did not keep that part of the original driver in the Linux 2.6 + * version, since it was making the driver significantly more complex + * with no real benefit. + * + * History: + * 2004-01-28 Original port. (Hong-Gunn Chew) + * 2004-01-31 Code review and approval. (Jean Delvare) + */ + +#include <linux/config.h> +#ifdef CONFIG_I2C_DEBUG_CHIP +#define DEBUG 1 +#endif + +#include <linux/module.h> +#include <linux/init.h> +#include <linux/slab.h> +#include <linux/i2c.h> +#include <linux/i2c-sensor.h> + +/* Addresses to scan */ +static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END }; +static unsigned short normal_i2c_range[] = { 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_2(gl518sm_r00, gl518sm_r80); + +/* Many GL518 constants specified below */ + +/* The GL518 registers */ +#define GL518_REG_CHIP_ID 0x00 +#define GL518_REG_REVISION 0x01 +#define GL518_REG_VENDOR_ID 0x02 +#define GL518_REG_CONF 0x03 +#define GL518_REG_TEMP_IN 0x04 +#define GL518_REG_TEMP_MAX 0x05 +#define GL518_REG_TEMP_HYST 0x06 +#define GL518_REG_FAN_COUNT 0x07 +#define GL518_REG_FAN_LIMIT 0x08 +#define GL518_REG_VIN1_LIMIT 0x09 +#define GL518_REG_VIN2_LIMIT 0x0a +#define GL518_REG_VIN3_LIMIT 0x0b +#define GL518_REG_VDD_LIMIT 0x0c +#define GL518_REG_VIN3 0x0d +#define GL518_REG_MISC 0x0f +#define GL518_REG_ALARM 0x10 +#define GL518_REG_MASK 0x11 +#define GL518_REG_INT 0x12 +#define GL518_REG_VIN2 0x13 +#define GL518_REG_VIN1 0x14 +#define GL518_REG_VDD 0x15 + + +/* + * Conversions. Rounding and limit checking is only done on the TO_REG + * variants. Note that you should be a bit careful with which arguments + * these macros are called: arguments may be evaluated more than once. + * Fixing this is just not worth it. + */ + +#define RAW_FROM_REG(val) val + +#define BOOL_FROM_REG(val) ((val)?0:1) +#define BOOL_TO_REG(val) ((val)?0:1) + +#define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0? \ + (val)-500:(val)+500)/1000)+119),0,255)) +#define TEMP_FROM_REG(val) (((val) - 119) * 1000) + +static inline u8 FAN_TO_REG(long rpm, int div) +{ + if (rpm = 0) + return 255; + rpm = SENSORS_LIMIT(rpm, 1, 1000000); + return SENSORS_LIMIT((960000 + rpm * div / 2) / (rpm * div), 1, 254); +} + +#define FAN_FROM_REG(val,div) ((val)=0 ? 0 : (val)=255 ? 0 : \ + (960000/((val)*(div)))) + +#define IN_TO_REG(val) (SENSORS_LIMIT((((val)+8)/19),0,255)) +#define IN_FROM_REG(val) ((val)*19) + +#define VDD_TO_REG(val) (SENSORS_LIMIT((((val)+11)/23),0,255)) +#define VDD_FROM_REG(val) ((val)*23) + +#define DIV_TO_REG(val) ((val)=8?3:(val)=4?2:(val)=1?0:1) +#define DIV_FROM_REG(val) (1 << (val)) + +#define BEEP_MASK_TO_REG(val) ((val) & 0x7f & data->alarm_mask) +#define BEEP_MASK_FROM_REG(val) ((val) & 0x7f) + +/* Each client has this additional data */ +struct gl518_data { + enum chips type; + + struct semaphore update_lock; + char valid; /* !=0 if following fields are valid */ + unsigned long last_updated; /* In jiffies */ + + u8 voltage_in[4]; /* Register values; [0] = VDD */ + u8 voltage_min[4]; /* Register values; [0] = VDD */ + u8 voltage_max[4]; /* Register values; [0] = VDD */ + u8 iter_voltage_in[4]; /* Register values; [0] = VDD */ + u8 fan_in[2]; + u8 fan_min[2]; + u8 fan_div[2]; /* Register encoding, shifted right */ + u8 fan_auto1; /* Boolean */ + u8 temp_in; /* Register values */ + u8 temp_max; /* Register values */ + u8 temp_hyst; /* Register values */ + u8 alarms; /* Register value */ + u8 alarm_mask; /* Register value */ + u8 beep_mask; /* Register value */ + u8 beep_enable; /* Boolean */ +}; + +static int gl518_attach_adapter(struct i2c_adapter *adapter); +static int gl518_detect(struct i2c_adapter *adapter, int address, int kind); +static void gl518_init_client(struct i2c_client *client); +static int gl518_detach_client(struct i2c_client *client); +static int gl518_read_value(struct i2c_client *client, u8 reg); +static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value); +static void gl518_update_client(struct i2c_client *client); + +/* This is the driver that will be inserted */ +static struct i2c_driver gl518_driver = { + .owner = THIS_MODULE, + .name = "gl518sm", + .id = I2C_DRIVERID_GL518, + .flags = I2C_DF_NOTIFY, + .attach_adapter = gl518_attach_adapter, + .detach_client = gl518_detach_client, +}; + +/* + * Internal variables + */ + +static int gl518_id = 0; + +/* + * Sysfs stuff + */ + +#define show(type, suffix, value) \ +static ssize_t show_##suffix(struct device *dev, char *buf) \ +{ \ + struct i2c_client *client = to_i2c_client(dev); \ + struct gl518_data *data = i2c_get_clientdata(client); \ + gl518_update_client(client); \ + return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \ +} + +#define show_fan(suffix, value, index) \ +static ssize_t show_##suffix(struct device *dev, char *buf) \ +{ \ + struct i2c_client *client = to_i2c_client(dev); \ + struct gl518_data *data = i2c_get_clientdata(client); \ + gl518_update_client(client); \ + return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[index], \ + DIV_FROM_REG(data->fan_div[index]))); \ +} + +show(TEMP, temp_input1, temp_in); +show(TEMP, temp_max1, temp_max); +show(TEMP, temp_hyst1, temp_hyst); +show(BOOL, fan_auto1, fan_auto1); +show_fan(fan_input1, fan_in, 0); +show_fan(fan_input2, fan_in, 1); +show_fan(fan_min1, fan_min, 0); +show_fan(fan_min2, fan_min, 1); +show(DIV, fan_div1, fan_div[0]); +show(DIV, fan_div2, fan_div[1]); +show(VDD, in_input0, voltage_in[0]); +show(IN, in_input1, voltage_in[1]); +show(IN, in_input2, voltage_in[2]); +show(IN, in_input3, voltage_in[3]); +show(VDD, in_min0, voltage_min[0]); +show(IN, in_min1, voltage_min[1]); +show(IN, in_min2, voltage_min[2]); +show(IN, in_min3, voltage_min[3]); +show(VDD, in_max0, voltage_max[0]); +show(IN, in_max1, voltage_max[1]); +show(IN, in_max2, voltage_max[2]); +show(IN, in_max3, voltage_max[3]); +show(RAW, alarms, alarms); +show(BOOL, beep_enable, beep_enable); +show(BEEP_MASK, beep_mask, beep_mask); + +#define set(type, suffix, value, reg) \ +static ssize_t set_##suffix(struct device *dev, const char *buf, \ + size_t count) \ +{ \ + struct i2c_client *client = to_i2c_client(dev); \ + struct gl518_data *data = i2c_get_clientdata(client); \ + data->value = type##_TO_REG(simple_strtol(buf, NULL, 10)); \ + gl518_write_value(client, reg, data->value); \ + return count; \ +} + +#define set_bits(type, suffix, value, reg, mask, shift) \ +static ssize_t set_##suffix(struct device *dev, const char *buf, \ + size_t count) \ +{ \ + struct i2c_client *client = to_i2c_client(dev); \ + struct gl518_data *data = i2c_get_clientdata(client); \ + int regvalue = gl518_read_value(client, reg); \ + data->value = type##_TO_REG(simple_strtoul(buf, NULL, 10)); \ + regvalue = (regvalue & ~mask) | (data->value << shift); \ + gl518_write_value(client, reg, regvalue); \ + return count; \ +} + +#define set_low(type, suffix, value, reg) \ + set_bits(type, suffix, value, reg, 0x00ff, 0) +#define set_high(type, suffix, value, reg) \ + set_bits(type, suffix, value, reg, 0xff00, 8) + +set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX); +set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST); +set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3); +set_bits(DIV, fan_div1, fan_div[0], GL518_REG_MISC, 0xc0, 6); +set_bits(DIV, fan_div2, fan_div[1], GL518_REG_MISC, 0x30, 4); +set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT); +set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT); +set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT); +set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT); +set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT); +set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT); +set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT); +set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT); +set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2); +set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM); + +static ssize_t set_fan_min1(struct device *dev, const char *buf, size_t count) +{ + struct i2c_client *client = to_i2c_client(dev); + struct gl518_data *data = i2c_get_clientdata(client); + int regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT); + + data->fan_min[0] = FAN_TO_REG(simple_strtoul(buf, NULL, 10), + DIV_FROM_REG(data->fan_div[0])); + regvalue = (regvalue & 0x00ff) | (data->fan_min[0] << 8); + if (data->fan_min[0] = 0) + data->alarm_mask &= ~0x20; + else + data->alarm_mask |= 0x20; + gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue); + + return count; +} + +static ssize_t set_fan_min2(struct device *dev, const char *buf, size_t count) +{ + struct i2c_client *client = to_i2c_client(dev); + struct gl518_data *data = i2c_get_clientdata(client); + int regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT); + + data->fan_min[1] = FAN_TO_REG(simple_strtoul(buf, NULL, 10), + DIV_FROM_REG(data->fan_div[1])); + regvalue = (regvalue & 0xff00) | data->fan_min[1]; + if (data->fan_min[1] = 0) + data->alarm_mask &= ~0x40; + else + data->alarm_mask |= 0x40; + gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue); + + return count; +} + +static DEVICE_ATTR(temp_input1, S_IRUGO, show_temp_input1, NULL); +static DEVICE_ATTR(temp_max1, S_IWUSR|S_IRUGO, show_temp_max1, set_temp_max1); +static DEVICE_ATTR(temp_hyst1, S_IWUSR|S_IRUGO, + show_temp_hyst1, set_temp_hyst1); +static DEVICE_ATTR(fan_auto1, S_IWUSR|S_IRUGO, show_fan_auto1, set_fan_auto1); +static DEVICE_ATTR(fan_input1, S_IRUGO, show_fan_input1, NULL); +static DEVICE_ATTR(fan_input2, S_IRUGO, show_fan_input2, NULL); +static DEVICE_ATTR(fan_min1, S_IWUSR|S_IRUGO, show_fan_min1, set_fan_min1); +static DEVICE_ATTR(fan_min2, S_IWUSR|S_IRUGO, show_fan_min2, set_fan_min2); +static DEVICE_ATTR(fan_div1, S_IWUSR|S_IRUGO, show_fan_div1, set_fan_div1); +static DEVICE_ATTR(fan_div2, S_IWUSR|S_IRUGO, show_fan_div2, set_fan_div2); +static DEVICE_ATTR(in_input0, S_IRUGO, show_in_input0, NULL); +static DEVICE_ATTR(in_input1, S_IRUGO, show_in_input1, NULL); +static DEVICE_ATTR(in_input2, S_IRUGO, show_in_input2, NULL); +static DEVICE_ATTR(in_input3, S_IRUGO, show_in_input3, NULL); +static DEVICE_ATTR(in_min0, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0); +static DEVICE_ATTR(in_min1, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1); +static DEVICE_ATTR(in_min2, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2); +static DEVICE_ATTR(in_min3, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3); +static DEVICE_ATTR(in_max0, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0); +static DEVICE_ATTR(in_max1, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1); +static DEVICE_ATTR(in_max2, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2); +static DEVICE_ATTR(in_max3, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3); +static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); +static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO, + show_beep_enable, set_beep_enable); +static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO, + show_beep_mask, set_beep_mask); + +/* + * Real code + */ + +static int gl518_attach_adapter(struct i2c_adapter *adapter) +{ + if (!(adapter->class & I2C_ADAP_CLASS_SMBUS)) + return 0; + return i2c_detect(adapter, &addr_data, gl518_detect); +} + +static int gl518_detect(struct i2c_adapter *adapter, int address, int kind) +{ + int i; + struct i2c_client *new_client; + struct gl518_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; + + /* 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 gl518_{read,write}_value. */ + + if (!(new_client = kmalloc(sizeof(struct i2c_client) + + sizeof(struct gl518_data), + GFP_KERNEL))) { + err = -ENOMEM; + goto exit; + } + memset(new_client, 0x00, sizeof(struct i2c_client) + + sizeof(struct gl518_data)); + + data = (struct gl518_data *) (new_client + 1); + i2c_set_clientdata(new_client, data); + + new_client->addr = address; + new_client->adapter = adapter; + new_client->driver = &gl518_driver; + new_client->flags = 0; + + /* Now, we do the remaining detection. */ + + if (kind < 0) { + if ((gl518_read_value(new_client, GL518_REG_CHIP_ID) != 0x80) + || (gl518_read_value(new_client, GL518_REG_CONF) & 0x80)) + goto exit_free; + } + + /* Determine the chip type. */ + if (kind <= 0) { + i = gl518_read_value(new_client, GL518_REG_REVISION); + if (i = 0x00) { + kind = gl518sm_r00; + name = "gl518sm"; + } else if (i = 0x80) { + kind = gl518sm_r80; + name = "gl518sm"; + } else { + if (kind <= 0) + dev_info(&adapter->dev, + "Ignoring 'force' parameter for unknown " + "chip at adapter %d, address 0x%02x\n", + i2c_adapter_id(adapter), address); + goto exit_free; + } + } + + /* Fill in the remaining client fields */ + strlcpy(new_client->name, name, I2C_NAME_SIZE); + new_client->id = gl518_id++; + data->type = kind; + 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 GL518SM chip */ + data->alarm_mask = 0xff; + data->voltage_in[0]Úta->voltage_in[1]Úta->voltage_in[2]=0; + gl518_init_client((struct i2c_client *) new_client); + + /* Register sysfs hooks */ + device_create_file(&new_client->dev, &dev_attr_in_input0); + device_create_file(&new_client->dev, &dev_attr_in_input1); + device_create_file(&new_client->dev, &dev_attr_in_input2); + device_create_file(&new_client->dev, &dev_attr_in_input3); + device_create_file(&new_client->dev, &dev_attr_in_min0); + device_create_file(&new_client->dev, &dev_attr_in_min1); + device_create_file(&new_client->dev, &dev_attr_in_min2); + device_create_file(&new_client->dev, &dev_attr_in_min3); + device_create_file(&new_client->dev, &dev_attr_in_max0); + device_create_file(&new_client->dev, &dev_attr_in_max1); + device_create_file(&new_client->dev, &dev_attr_in_max2); + device_create_file(&new_client->dev, &dev_attr_in_max3); + device_create_file(&new_client->dev, &dev_attr_fan_auto1); + device_create_file(&new_client->dev, &dev_attr_fan_input1); + device_create_file(&new_client->dev, &dev_attr_fan_input2); + device_create_file(&new_client->dev, &dev_attr_fan_min1); + device_create_file(&new_client->dev, &dev_attr_fan_min2); + device_create_file(&new_client->dev, &dev_attr_fan_div1); + device_create_file(&new_client->dev, &dev_attr_fan_div2); + device_create_file(&new_client->dev, &dev_attr_temp_input1); + device_create_file(&new_client->dev, &dev_attr_temp_max1); + device_create_file(&new_client->dev, &dev_attr_temp_hyst1); + device_create_file(&new_client->dev, &dev_attr_alarms); + device_create_file(&new_client->dev, &dev_attr_beep_enable); + device_create_file(&new_client->dev, &dev_attr_beep_mask); + + return 0; + +/* OK, this is not exactly good programming practice, usually. But it is + very code-efficient in this case. */ + +exit_free: + kfree(new_client); +exit: + return err; +} + + +/* Called when we have found a new GL518SM. */ +static void gl518_init_client(struct i2c_client *client) +{ + /* Power-on defaults (bit 7=1) */ + gl518_write_value(client, GL518_REG_CONF, 0x80); + + /* No noisy output (bit 2=1), Comparator mode (bit 3=0), two fans (bit4=0), + standby mode (bit6=0) */ + gl518_write_value(client, GL518_REG_CONF, 0x04); + + /* Never interrupts */ + gl518_write_value(client, GL518_REG_MASK, 0x00); + + /* Clear status register (bit 5=1), start (bit6=1) */ + gl518_write_value(client, GL518_REG_CONF, 0x24); + gl518_write_value(client, GL518_REG_CONF, 0x44); +} + +static int gl518_detach_client(struct i2c_client *client) +{ + int err; + + if ((err = i2c_detach_client(client))) { + dev_err(&client->dev, "Client deregistration failed, " + "client not detached.\n"); + return err; + } + + kfree(client); + + return 0; +} + +static inline u16 swap_bytes(u16 val) +{ + return (val >> 8) | (val << 8); +} + +/* Registers 0x07 to 0x0c are word-sized, others are byte-sized + GL518 uses a high-byte first convention, which is exactly opposite to + the usual practice. */ +static int gl518_read_value(struct i2c_client *client, u8 reg) +{ + if ((reg >= 0x07) && (reg <= 0x0c)) + return swap_bytes(i2c_smbus_read_word_data(client, reg)); + else + return i2c_smbus_read_byte_data(client, reg); +} + +/* Registers 0x07 to 0x0c are word-sized, others are byte-sized + GL518 uses a high-byte first convention, which is exactly opposite to + the usual practice. */ +static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value) +{ + if ((reg >= 0x07) && (reg <= 0x0c)) + return i2c_smbus_write_word_data(client, reg, + swap_bytes(value)); + else + return i2c_smbus_write_byte_data(client, reg, value); +} + +static void gl518_update_client(struct i2c_client *client) +{ + struct gl518_data *data = i2c_get_clientdata(client); + int val; + + down(&data->update_lock); + + if ((jiffies - data->last_updated > HZ + HZ / 2) || + (jiffies < data->last_updated) || !data->valid) { + dev_dbg(&client->dev, "Starting gl518 update\n"); + + data->alarms = gl518_read_value(client, GL518_REG_INT); + data->beep_mask = gl518_read_value(client, GL518_REG_ALARM); + + val = gl518_read_value(client, GL518_REG_VDD_LIMIT); + data->voltage_min[0] = val & 0xff; + data->voltage_max[0] = (val >> 8) & 0xff; + val = gl518_read_value(client, GL518_REG_VIN1_LIMIT); + data->voltage_min[1] = val & 0xff; + data->voltage_max[1] = (val >> 8) & 0xff; + val = gl518_read_value(client, GL518_REG_VIN2_LIMIT); + data->voltage_min[2] = val & 0xff; + data->voltage_max[2] = (val >> 8) & 0xff; + val = gl518_read_value(client, GL518_REG_VIN3_LIMIT); + data->voltage_min[3] = val & 0xff; + data->voltage_max[3] = (val >> 8) & 0xff; + + val = gl518_read_value(client, GL518_REG_FAN_COUNT); + data->fan_in[0] = (val >> 8) & 0xff; + data->fan_in[1] = val & 0xff; + + val = gl518_read_value(client, GL518_REG_FAN_LIMIT); + data->fan_min[0] = (val >> 8) & 0xff; + data->fan_min[1] = val & 0xff; + + data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN); + data->temp_max + gl518_read_value(client, GL518_REG_TEMP_MAX); + data->temp_hyst + gl518_read_value(client, GL518_REG_TEMP_HYST); + + val = gl518_read_value(client, GL518_REG_MISC); + data->fan_div[0] = (val >> 6) & 0x03; + data->fan_div[1] = (val >> 4) & 0x03; + data->fan_auto1 = (val >> 3) & 0x01; + + data->alarms &= data->alarm_mask; + + val = gl518_read_value(client, GL518_REG_CONF); + data->beep_enable = (val >> 2) & 1; + + if (data->type != gl518sm_r00) { + data->voltage_in[0] + gl518_read_value(client, GL518_REG_VDD); + data->voltage_in[1] + gl518_read_value(client, GL518_REG_VIN1); + data->voltage_in[2] + gl518_read_value(client, GL518_REG_VIN2); + } + data->voltage_in[3] + gl518_read_value(client, GL518_REG_VIN3); + + data->last_updated = jiffies; + data->valid = 1; + } + + up(&data->update_lock); +} + +static int __init sensors_gl518sm_init(void) +{ + return i2c_add_driver(&gl518_driver); +} + +static void __exit sensors_gl518sm_exit(void) +{ + i2c_del_driver(&gl518_driver); +} + +MODULE_AUTHOR("Frodo Looijaard <[email protected]>, " + "Kyosti Malkki <[email protected]> and " + "Hong-Gunn Chew <[email protected]>"); +MODULE_DESCRIPTION("GL518SM driver"); +MODULE_LICENSE("GPL"); + +module_init(sensors_gl518sm_init); +module_exit(sensors_gl518sm_exit);
[Please CC: the mailing-list on reply.]
Hi Gunn,
Quoted 9 lines
I have attached the latest patch against rc3. I have fixed up all that
we have discussed on sunday. In particular:
* BOOL and RAW macros
* voltage rounding
* fan speed calculation
*beep_maskfor fan_min=0
* driver init code
Quoted 3 lines
Hopefully the code is ready now. *phew*
Let me know what you think.
Looks very good overall. A few comments though:
Review
Quoted 2 lines
(quoted diff) +1
-1-#define IN_TO_REG(val) (SENSORS_LIMIT((((val)+8)/19),0,255)) +#define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
Nice catching, I had missed that one ;)
Quoted 1 line
+#define
VDD_FROM_REG(val)((val)*95/4)
(((val)*95+2)/4)?
Review
Quoted 2 lines
(quoted diff) +2
-0+ /* Comparator mode (D3=0), standby mode (D6=0) */ + gl518_write_value(client, GL518_REG_CONF, 0xb7 & regvalue);
I'd suggest 0x37 as the mask. D7 is something we don't want to set to 1. I agree it doesn't change anything practically. Just looks safer.
Review
Quoted 3 lines
(quoted diff) +3
-0+ /* Clear status register (D5=1), start (D6=1) */ + gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue); + gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
It works, but isn't correct. At this point, regvalue doesn't hold the current configuration register. I suggest we use (regvalue &= 0x37) in the first gl518_write_value() call above, so that it does.
The rest is OK, will apply and send the driver to Greg KH ASAP. No need for you to resend a patch, I'll do the changes myself, unless you or anyone have an objection.
Great job you did :) Thanks.
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
Quoted 1 line
There is already regvalue =
gl518_read_value()&0x14;
In fact there shouldn't be. Because I believe it shouldn't be, I forgot it was ;) More right below.
Quoted 1 line
above that, since regvalue is supposed to only hold values from
D6,D3,
Quoted 1 line
so there is no need to &=
0x37again.
For one thing, I didn't mean "&= 0x37 again" but "&= 0x37 *instead*". More precisely:
- gl518_write_value(client, GL518_REG_CONF, 0xb7 & regvalue);
+ gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
Hope it's clearer now.
More generally, the way things are now, you are more or less silently forcing D0, D1 and D5 to 0. You can argue that it doesn't matter much because D5 is set back to 1 (and 0 again) later, and D0 and D1 are "reserved" so likely 0 anyway. Still this isn't how things should be done IMHO. The right (and safe) way of doing things is not to touch anything unless we have a reason and say we do.
For this reason, I would only mask D7 out of the original register reading. It doesn't change anything practically as far as I can see, but, again, looks safer. If you don't see why this is better, thing of the case where the "reserved" bits are in fact used in later releases of the chip. Forcing them to 0 could have an unexpected effect.
Signature · 2 lines
Jean Delvare http://www.ensicaen.ismra.fr/~delvare/
Quoted 7 lines
Quoted 4 lines
I have also updated
gl518sm.c:
- I have found a bug due to me misreading the old code. It affects only fan_div*.Don't you mean fan_min*?
Actually, I meant fan_div* and fan_min*.
Quoted 9 lines
Quoted 4 lines
- I am thinking of replacing '(val >> 4) &
0x0f' with '(val &0xf0) >> 4' as it allows easy checking with the chip specifications (the mask in particular). I have changed a few, and if there are no objections, I will be changing the rest.Frankly, I don't see no benefit. I don't find it more nor less readable one way or one other - so I wouldn't bother changing, especially since it is likely to introduce errors.
I find it easier to check when the mask is the same as in the specs, but you are correct to see that changing them would likely introduce errors.
Quoted 3 lines
I've made the necessary change to libsensors so that it knows about your
fan_auto1. That way, "sensors -s" shouldn't complain about it anymore.
Ok. By the way, is there a label for beep_mask in libsensors?
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +0
-0data->fan_min[1] = FAN_TO_REG(simple_strtoul(buf, NULL, 10), DIV_FROM_REG(data->fan_div[0]));^Isn't it a bug?
Yes, thanks for finding it.
Quoted 9 lines
Review
Quoted 4 lines
(quoted diff) +2
-1val = gl518_read_value(client, GL518_REG_CONF); - data->beep_enable = (val >> 2) & 1; + data->beep_enable = (val & 0x04) >> 2; + data->fan_auto1 |= (val & 0x10) >> 3;What's the point of the last line? For one thing, it seems to be bogus (the mask doesn't match the shift). For another, I don't really see what it is supposed to do.
I originally intended NoFan2 to be bit 1 of fan_auto1, since they are sort of related. I missed it when I removed NoFan2.
The patch against rc3 is attached.
The changes in particular:
| -#define DIV_TO_REG(val) ((val)=8?3:(val)=4?2:(val)=1?0:1) | +#define DIV_TO_REG(val) ((val)=8?3:(val)=4?2:(val)=1?0:3)
If an invalid div is entered, it is set to the default of 2^3=8.
| -#define VDD_TO_REG(val) (SENSORS_LIMIT((((val)+11)/23),0,255)) | -#define VDD_FROM_REG(val) ((val)*23) | +#define VDD_TO_REG(val) (SENSORS_LIMIT((((val)+12)/24),0,255)) | +#define VDD_FROM_REG(val) ((val)*24)
Actual conversion is (val*19*5/4)=(val*23.75). The rounding up is negligible as it's (255*0.25mV) lower than the sensor accuracy of 75mV.
| static inline u8 FAN_TO_REG(long rpm, int div)
| {
| + long rpmdiv;
| if (rpm = 0)
| - return 255;
| - rpm = SENSORS_LIMIT(rpm, 1, 1000000);
| - return SENSORS_LIMIT((960000 + rpm * div / 2) / (rpm * div), 1, 254);
| + return 0;
| + rpmdiv = SENSORS_LIMIT(rpm, 1, 1920000) * div;
| + return SENSORS_LIMIT((960000 + rpmdiv / 2) / rpmdiv, 1, 255);
| }
| -
| -#define FAN_FROM_REG(val,div) ((val)=0 ? 0 : (val)=255 ? 0 : \
| - (960000/((val)*(div))))
| +#define FAN_FROM_REG(val,div) ((val)=0 ? 0 : (960000/((val)*(div)))) I changed rpm=0 from reg%5 to reg=0, as it breaks the fan_min=0 code below, and as reg=255 is a valid count, while reg=0 is not. rpm is limited to 1920000 as that's the largest possible with rounding, and to prevent overflow with '* div'.
-------------- next part --------------
gl518sm.c renamed from gl518sm.c.rc3 +28 -27
@@ -89,35 +89,37 @@* Fixing this is just not worth it. */ -#define RAW_FROM_REG (val) val - -#define BOOL_FROM_REG (val) ((val)?0:1) -#define BOOL_TO_REG (val) ((val)?0:1) -#define TEMP_TO_REG (val) (SENSORS_LIMIT(((((val)<0? \ (val)-500:(val)+500)/1000)+119),0,255))#define TEMP_FROM_REG (val) (((val) - 119) * 1000) +#define FAN_AUTO_FROM_REG (val) ((val)?0:1) +#define FAN_AUTO_TO_REG (val) ((val)?0:1) +static inline u8 FAN_TO_REG (long rpm,int div) { +long rpmdiv;if (rpm =0 ) -return 255 ; - rpm =SENSORS_LIMIT (rpm,1 ,1000000 ); -return SENSORS_LIMIT ((960000 + rpm * div /2 ) / (rpm * div),1 ,254 ); +return 0 ; + rpmdiv =SENSORS_LIMIT (rpm,1 ,1920000 ) * div; +return SENSORS_LIMIT ((960000 + rpmdiv /2 ) / rpmdiv,1 ,255 ); } - -#define FAN_FROM_REG (val,div) ((val)=0 ? 0 : (val)=255 ? 0 : \ - (960000/((val)*(div)))) +#define FAN_FROM_REG (val,div) ((val)=0 ? 0 : (960000/((val)*(div))))#define IN_TO_REG (val) (SENSORS_LIMIT((((val)+8)/19),0,255))#define IN_FROM_REG (val) ((val)*19) -#define VDD_TO_REG (val) (SENSORS_LIMIT((((val)+11)/23),0,255)) -#define VDD_FROM_REG (val) ((val)*23) +#define VDD_TO_REG (val) (SENSORS_LIMIT((((val)+12)/24),0,255)) +#define VDD_FROM_REG (val) ((val)*24) -#define DIV_TO_REG (val) ((val)=8?3:(val)=4?2:(val)=1?0:1) +#define DIV_TO_REG (val) ((val)=8?3:(val)=4?2:(val)=1?0:3)#define DIV_FROM_REG (val) (1 << (val)) +#define ALARMS_FROM_REG (val) val + +#define BEEP_ENABLE_FROM_REG (val) ((val)?0:1) +#define BEEP_ENABLE_TO_REG (val) ((val)?0:1) +#define BEEP_MASK_TO_REG (val) ((val) & 0x7f & data->alarm_mask)#define BEEP_MASK_FROM_REG (val) ((val) & 0x7f)
@@ -196,7 +198,7 @@show (TEMP , temp_input1, temp_in);show (TEMP , temp_max1, temp_max);show (TEMP , temp_hyst1, temp_hyst); -show (BOOL , fan_auto1, fan_auto1); +show (FAN_AUTO , fan_auto1, fan_auto1);show_fan (fan_input1, fan_in,0 );show_fan (fan_input2, fan_in,1 );show_fan (fan_min1, fan_min,0 );
@@ -215,8 +217,8 @@show (IN , in_max1, voltage_max[1 ]);show (IN , in_max2, voltage_max[2 ]);show (IN , in_max3, voltage_max[3 ]); -show (RAW , alarms, alarms); -show (BOOL , beep_enable, beep_enable); +show (ALARMS , alarms, alarms); +show (BEEP_ENABLE , beep_enable, beep_enable);show (BEEP_MASK , beep_mask, beep_mask);#define set (type, suffix, value, reg) \
@@ -250,7 +252,7 @@set (TEMP , temp_max1, temp_max,GL518_REG_TEMP_MAX );set (TEMP , temp_hyst1, temp_hyst,GL518_REG_TEMP_HYST ); -set_bits (BOOL , fan_auto1, fan_auto1,GL518_REG_MISC ,0x08 ,3 ); +set_bits (FAN_AUTO , fan_auto1, fan_auto1,GL518_REG_MISC ,0x08 ,3 );set_bits (DIV , fan_div1, fan_div[0 ],GL518_REG_MISC ,0xc0 ,6 );set_bits (DIV , fan_div2, fan_div[1 ],GL518_REG_MISC ,0x30 ,4 );set_low (VDD , in_min0, voltage_min[0 ],GL518_REG_VDD_LIMIT );
@@ -261,7 +263,7 @@set_high (IN , in_max1, voltage_max[1 ],GL518_REG_VIN1_LIMIT );set_high (IN , in_max2, voltage_max[2 ],GL518_REG_VIN2_LIMIT );set_high (IN , in_max3, voltage_max[3 ],GL518_REG_VIN3_LIMIT ); -set_bits (BOOL , beep_enable, beep_enable,GL518_REG_CONF ,0x04 ,2 ); +set_bits (BEEP_ENABLE , beep_enable, beep_enable,GL518_REG_CONF ,0x04 ,2 );set (BEEP_MASK , beep_mask, beep_mask,GL518_REG_ALARM );static ssize_t set_fan_min1 (struct device *dev,const char *buf,size_t count)
@@ -458,19 +460,18 @@/* Called when we have found a new GL518SM. */ static void gl518_init_client (struct i2c_client *client) { -/* Power-on defaults (bit 7=1) */ -gl518_write_value (client,GL518_REG_CONF ,0x80 ); +/* retain D4:NoFan2, D2:beep_enable */ +u8 regvalue =gl518_read_value (client,GL518_REG_CONF ) &0x14 ; -/* No noisy output (bit 2=1), Comparator mode (bit 3=0), two fans (bit4=0), -standby mode (bit6=0) */ -gl518_write_value (client,GL518_REG_CONF ,0x04 ); +/* Comparator mode (D3=0), two fans (D4=0), standby mode (D6=0) */ +gl518_write_value (client,GL518_REG_CONF ,0x00 | regvalue);/* Never interrupts */ gl518_write_value (client,GL518_REG_MASK ,0x00 ); -/* Clear status register (bit 5=1), start (bit6=1) */ -gl518_write_value (client,GL518_REG_CONF ,0x24 ); -gl518_write_value (client,GL518_REG_CONF ,0x44 ); +/* Clear status register (D5=1), start (D6=1) */ +gl518_write_value (client,GL518_REG_CONF ,0x20 | regvalue); +gl518_write_value (client,GL518_REG_CONF ,0x40 | regvalue); }static int gl518_detach_client (struct i2c_client *client)
Hi Jean,
I have attached the latest patch against rc3. I have fixed up all that we have discussed on sunday. In particular:
* BOOL and RAW macros
* voltage rounding
* fan speed calculation
* beep_mask for fan_min=0
* driver init code
Hopefully the code is ready now. *phew*
Let me know what you think.
Cheers,
Hong-Gunn
-------------- next part --------------
gl518sm.c renamed from gl518sm.c.rc3 +28 -22
@@ -100,22 +100,21 @@static inline u8 FAN_TO_REG (long rpm,int div) { +long rpmdiv;if (rpm =0 ) -return 255 ; - rpm =SENSORS_LIMIT (rpm,1 ,1000000 ); -return SENSORS_LIMIT ((960000 + rpm * div /2 ) / (rpm * div),1 ,254 ); +return 0 ; + rpmdiv =SENSORS_LIMIT (rpm,1 ,1920000 ) * div; +return SENSORS_LIMIT ((960000 + rpmdiv /2 ) / rpmdiv,1 ,255 ); } +#define FAN_FROM_REG (val,div) ((val)=0 ? 0 : (960000/((val)*(div)))) -#define FAN_FROM_REG (val,div) ((val)=0 ? 0 : (val)=255 ? 0 : \ - (960000/((val)*(div)))) - -#define IN_TO_REG (val) (SENSORS_LIMIT((((val)+8)/19),0,255)) +#define IN_TO_REG (val) (SENSORS_LIMIT((((val)+9)/19),0,255))#define IN_FROM_REG (val) ((val)*19) -#define VDD_TO_REG (val) (SENSORS_LIMIT((((val)+11)/23),0,255)) -#define VDD_FROM_REG (val) ((val)*23) +#define VDD_TO_REG (val) (SENSORS_LIMIT((((val)*4+47)/95),0,255)) +#define VDD_FROM_REG (val) ((val)*95/4) -#define DIV_TO_REG (val) ((val)=8?3:(val)=4?2:(val)=1?0:1) +#define DIV_TO_REG (val) ((val)=4?2:(val)=2?1:(val)=1?0:3)#define DIV_FROM_REG (val) (1 << (val))#define BEEP_MASK_TO_REG (val) ((val) & 0x7f & data->alarm_mask)
@@ -273,11 +272,15 @@ data->fan_min[0 ] =FAN_TO_REG (simple_strtoul (buf,NULL ,10 ),DIV_FROM_REG (data->fan_div[0 ])); regvalue = (regvalue &0x00ff ) | (data->fan_min[0 ] <<8 ); +gl518_write_value (client,GL518_REG_FAN_LIMIT , regvalue); + + data->beep_mask =gl518_read_value (client,GL518_REG_ALARM );if (data->fan_min[0 ] =0 ) data->alarm_mask &= ~0x20 ;else data->alarm_mask |=0x20 ; -gl518_write_value (client,GL518_REG_FAN_LIMIT , regvalue); + data->beep_mask &= data->alarm_mask; +gl518_write_value (client,GL518_REG_ALARM , data->beep_mask);return count; }
@@ -291,11 +294,15 @@ data->fan_min[1 ] =FAN_TO_REG (simple_strtoul (buf,NULL ,10 ),DIV_FROM_REG (data->fan_div[1 ])); regvalue = (regvalue &0xff00 ) | data->fan_min[1 ]; +gl518_write_value (client,GL518_REG_FAN_LIMIT , regvalue); + + data->beep_mask =gl518_read_value (client,GL518_REG_ALARM );if (data->fan_min[1 ] =0 ) data->alarm_mask &= ~0x40 ;else data->alarm_mask |=0x40 ; -gl518_write_value (client,GL518_REG_FAN_LIMIT , regvalue); + data->beep_mask &= data->alarm_mask; +gl518_write_value (client,GL518_REG_ALARM , data->beep_mask);return count; }
@@ -350,7 +357,7 @@if (!i2c_check_functionality (adapter,I2C_FUNC_SMBUS_BYTE_DATA |I2C_FUNC_SMBUS_WORD_DATA )) - goto exit; + 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.
@@ -378,7 +385,7 @@if (kind <0 ) {if ((gl518_read_value (new_client,GL518_REG_CHIP_ID ) !=0x80 ) || (gl518_read_value (new_client,GL518_REG_CONF ) &0x80 )) - goto exit_free; + goto exit_free; }/* Determine the chip type. */
@@ -458,19 +465,18 @@/* Called when we have found a new GL518SM. */ static void gl518_init_client (struct i2c_client *client) { -/* Power-on defaults (bit 7=1) */ -gl518_write_value (client,GL518_REG_CONF ,0x80 ); +/* retain D4:NoFan2, D2:beep_enable */ +u8 regvalue =gl518_read_value (client,GL518_REG_CONF ) &0x14 ; -/* No noisy output (bit 2=1), Comparator mode (bit 3=0), two fans (bit4=0), -standby mode (bit6=0) */ -gl518_write_value (client,GL518_REG_CONF ,0x04 ); +/* Comparator mode (D3=0), standby mode (D6=0) */ +gl518_write_value (client,GL518_REG_CONF ,0xb7 & regvalue);/* Never interrupts */ gl518_write_value (client,GL518_REG_MASK ,0x00 ); -/* Clear status register (bit 5=1), start (bit6=1) */ -gl518_write_value (client,GL518_REG_CONF ,0x24 ); -gl518_write_value (client,GL518_REG_CONF ,0x44 ); +/* Clear status register (D5=1), start (D6=1) */ +gl518_write_value (client,GL518_REG_CONF ,0x20 | regvalue); +gl518_write_value (client,GL518_REG_CONF ,0x40 | regvalue); }static int gl518_detach_client (struct i2c_client *client)
Jean,
Quoted 6 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ /* Comparator mode (D3=0), standby mode (D6=0) */ + gl518_write_value(client, GL518_REG_CONF, 0xb7 & regvalue);I'd suggest
0x37as the mask. D7 is something we don't want to set to 1. I agree it doesn't change anything practically. Just looks safer.
ok
Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ /* Clear status register (D5=1), start (D6=1) */ + gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue); + gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);It works, but isn't correct. At this point, regvalue doesn't hold the current configuration register. I suggest we use (regvalue &=
0x37) in the firstgl518_write_value()call above, so that it does.
There is already regvalue = gl518_read_value() & 0x14; above that, since regvalue is supposed to only hold values from D6,D3, so there is no need to &= 0x37 again.
I don't think we should change this. Just the 0xb7 to 0x37 above.
Hong-Gunn
Quoted 15 lines
Quoted 1 line
There is already regvalue =
gl518_read_value()&0x14;In fact there shouldn't be. Because I believe it shouldn't be, I forgot it was ;) More right below.
Quoted 1 line
above that, since regvalue is supposed to only hold values from
D6,D3,
Quoted 1 line
so there is no need to &=
0x37again.For one thing, I didn't mean "&=
0x37again" but "&=0x37*instead*". More precisely:-
gl518_write_value(client, GL518_REG_CONF, 0xb7 & regvalue);
+gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
Quoted 9 lines
Hope it's clearer now.
More generally, the way things are now, you are more or less silently forcing D0, D1 and D5 to 0. You can argue that it doesn't matter much because D5 is set back to 1 (and 0 again) later, and D0 and D1 are "reserved" so likely 0 anyway. Still this isn't how things should be done IMHO. The right (and safe) way of doing things is not to touch anything unless we have a reason and say we do.
This just changes the original intention of regvalue. It was only meant to store the NoFan2 (D4) and Intclr flags (D2). The other bits are to be changed within init. As for reserved bits, it is usual to assume setting them to 0. If their functions were changed, the it should be picked by by the chip revision, and the driver should not work as it is now.
However, there are many ways to skin a cat. Both methods work, and I find that what I had initially is pretty clear, though regvalue could be changed to D4D2value or something clearer, and that we are settings the rest of the bits within the function.
I will pick up your reply tomorrow.
Cheers,
Hong-Gunn