Commit 98ab0b5d authored by Greg Kroah-Hartman's avatar Greg Kroah-Hartman

i2c: convert lm75 chip driver to use sysfs files.

parent 6fb80bd6
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ */
/* #define DEBUG 1 */
#include <linux/module.h> #include <linux/module.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/slab.h> #include <linux/slab.h>
...@@ -25,8 +27,6 @@ ...@@ -25,8 +27,6 @@
#include <linux/i2c-proc.h> #include <linux/i2c-proc.h>
#define LM75_SYSCTL_TEMP 1200 /* Degrees Celsius * 10 */
/* Addresses to scan */ /* Addresses to scan */
static unsigned short normal_i2c[] = { SENSORS_I2C_END }; static unsigned short normal_i2c[] = { SENSORS_I2C_END };
static unsigned short normal_i2c_range[] = { 0x48, 0x4f, SENSORS_I2C_END }; static unsigned short normal_i2c_range[] = { 0x48, 0x4f, SENSORS_I2C_END };
...@@ -57,79 +57,95 @@ SENSORS_INSMOD_1(lm75); ...@@ -57,79 +57,95 @@ SENSORS_INSMOD_1(lm75);
/* Each client has this additional data */ /* Each client has this additional data */
struct lm75_data { struct lm75_data {
int sysctl_id;
struct semaphore update_lock; struct semaphore update_lock;
char valid; /* !=0 if following fields are valid */ char valid; /* !=0 if following fields are valid */
unsigned long last_updated; /* In jiffies */ unsigned long last_updated; /* In jiffies */
u16 temp_input; /* Register values */
u16 temp, temp_os, temp_hyst; /* Register values */ u16 temp_max;
u16 temp_hyst;
}; };
static int lm75_attach_adapter(struct i2c_adapter *adapter); static int lm75_attach_adapter(struct i2c_adapter *adapter);
static int lm75_detect(struct i2c_adapter *adapter, int address, static int lm75_detect(struct i2c_adapter *adapter, int address, int kind);
unsigned short flags, int kind);
static void lm75_init_client(struct i2c_client *client); static void lm75_init_client(struct i2c_client *client);
static int lm75_detach_client(struct i2c_client *client); static int lm75_detach_client(struct i2c_client *client);
static u16 swap_bytes(u16 val);
static int lm75_read_value(struct i2c_client *client, u8 reg); static int lm75_read_value(struct i2c_client *client, u8 reg);
static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value); static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
static void lm75_temp(struct i2c_client *client, int operation,
int ctl_name, int *nrels_mag, long *results);
static void lm75_update_client(struct i2c_client *client); static void lm75_update_client(struct i2c_client *client);
/* This is the driver that will be inserted */ /* This is the driver that will be inserted */
static struct i2c_driver lm75_driver = { static struct i2c_driver lm75_driver = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.name = "LM75 sensor", .name = "lm75",
.id = I2C_DRIVERID_LM75, .id = I2C_DRIVERID_LM75,
.flags = I2C_DF_NOTIFY, .flags = I2C_DF_NOTIFY,
.attach_adapter = lm75_attach_adapter, .attach_adapter = lm75_attach_adapter,
.detach_client = lm75_detach_client, .detach_client = lm75_detach_client,
}; };
/* These files are created for each detected LM75. This is just a template;
though at first sight, you might think we could use a statically
allocated list, we need some way to get back to the parent - which
is done through one of the 'extra' fields which are initialized
when a new copy is allocated. */
static ctl_table lm75_dir_table_template[] = {
{LM75_SYSCTL_TEMP, "temp", NULL, 0, 0644, NULL, &i2c_proc_real,
&i2c_sysctl_real, NULL, &lm75_temp},
{0}
};
static int lm75_id = 0; static int lm75_id = 0;
#define show(value) \
static ssize_t show_##value(struct device *dev, char *buf) \
{ \
struct i2c_client *client = to_i2c_client(dev); \
struct lm75_data *data = i2c_get_clientdata(client); \
int temp; \
\
lm75_update_client(client); \
temp = TEMP_FROM_REG(data->value); \
return sprintf(buf, "%d\n", temp * 100); \
}
show(temp_max);
show(temp_hyst);
show(temp_input);
#define set(value, reg) \
static ssize_t set_##value(struct device *dev, const char *buf, size_t count) \
{ \
struct i2c_client *client = to_i2c_client(dev); \
struct lm75_data *data = i2c_get_clientdata(client); \
int temp = simple_strtoul(buf, NULL, 10) / 100; \
\
data->value = TEMP_TO_REG(temp); \
lm75_write_value(client, reg, data->value); \
return count; \
}
set(temp_max, LM75_REG_TEMP_OS);
set(temp_hyst, LM75_REG_TEMP_HYST);
static DEVICE_ATTR(temp_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
static DEVICE_ATTR(temp_min, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst);
static DEVICE_ATTR(temp_input, S_IRUGO, show_temp_input, NULL);
static int lm75_attach_adapter(struct i2c_adapter *adapter) static int lm75_attach_adapter(struct i2c_adapter *adapter)
{ {
return i2c_detect(adapter, &addr_data, lm75_detect); return i2c_detect(adapter, &addr_data, lm75_detect);
} }
/* This function is called by i2c_detect */ /* This function is called by i2c_detect */
static int lm75_detect(struct i2c_adapter *adapter, int address, static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
unsigned short flags, int kind)
{ {
int i, cur, conf, hyst, os; int i, cur, conf, hyst, os;
struct i2c_client *new_client; struct i2c_client *new_client;
struct lm75_data *data; struct lm75_data *data;
int err = 0; int err = 0;
const char *type_name, *client_name; const char *name;
/* Make sure we aren't probing the ISA bus!! This is just a safety check /* Make sure we aren't probing the ISA bus!! This is just a safety check
at this moment; i2c_detect really won't call us. */ at this moment; i2c_detect really won't call us. */
#ifdef DEBUG #ifdef DEBUG
if (i2c_is_isa_adapter(adapter)) { if (i2c_is_isa_adapter(adapter)) {
printk dev_dbg(&adapter->dev,
("lm75.o: lm75_detect called for an ISA bus adapter?!?\n"); "lm75_detect called for an ISA bus adapter?!?\n");
return 0; goto exit;
} }
#endif #endif
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
I2C_FUNC_SMBUS_WORD_DATA)) I2C_FUNC_SMBUS_WORD_DATA))
goto error0; goto exit;
/* OK. For now, we presume we have a valid client. We now create the /* OK. For now, we presume we have a valid client. We now create the
client structure, even though we cannot fill it completely yet. client structure, even though we cannot fill it completely yet.
...@@ -138,7 +154,7 @@ static int lm75_detect(struct i2c_adapter *adapter, int address, ...@@ -138,7 +154,7 @@ static int lm75_detect(struct i2c_adapter *adapter, int address,
sizeof(struct lm75_data), sizeof(struct lm75_data),
GFP_KERNEL))) { GFP_KERNEL))) {
err = -ENOMEM; err = -ENOMEM;
goto error0; goto exit;
} }
memset(new_client, 0x00, sizeof(struct i2c_client) + memset(new_client, 0x00, sizeof(struct i2c_client) +
sizeof(struct lm75_data)); sizeof(struct lm75_data));
...@@ -157,16 +173,10 @@ static int lm75_detect(struct i2c_adapter *adapter, int address, ...@@ -157,16 +173,10 @@ static int lm75_detect(struct i2c_adapter *adapter, int address,
hyst = i2c_smbus_read_word_data(new_client, 2); hyst = i2c_smbus_read_word_data(new_client, 2);
os = i2c_smbus_read_word_data(new_client, 3); os = i2c_smbus_read_word_data(new_client, 3);
for (i = 0; i <= 0x1f; i++) for (i = 0; i <= 0x1f; i++)
if ( if ((i2c_smbus_read_byte_data(new_client, i * 8 + 1) != conf) ||
(i2c_smbus_read_byte_data (i2c_smbus_read_word_data(new_client, i * 8 + 2) != hyst) ||
(new_client, i * 8 + 1) != conf) (i2c_smbus_read_word_data(new_client, i * 8 + 3) != os))
|| goto exit_free;
(i2c_smbus_read_word_data
(new_client, i * 8 + 2) != hyst)
||
(i2c_smbus_read_word_data
(new_client, i * 8 + 3) != os))
goto error1;
} }
/* Determine the chip type - only one kind supported! */ /* Determine the chip type - only one kind supported! */
...@@ -174,15 +184,15 @@ static int lm75_detect(struct i2c_adapter *adapter, int address, ...@@ -174,15 +184,15 @@ static int lm75_detect(struct i2c_adapter *adapter, int address,
kind = lm75; kind = lm75;
if (kind == lm75) { if (kind == lm75) {
type_name = "lm75"; name = "lm75";
client_name = "LM75 chip";
} else { } else {
pr_debug("lm75.o: Internal error: unknown kind (%d)?!?", kind); dev_dbg(&adapter->dev, "Internal error: unknown kind (%d)?!?",
goto error1; kind);
goto exit_free;
} }
/* Fill in the remaining client fields and put it into the global list */ /* Fill in the remaining client fields and put it into the global list */
strncpy(new_client->dev.name, client_name, DEVICE_NAME_SIZE); strncpy(new_client->dev.name, name, DEVICE_NAME_SIZE);
new_client->id = lm75_id++; new_client->id = lm75_id++;
data->valid = 0; data->valid = 0;
...@@ -190,36 +200,23 @@ static int lm75_detect(struct i2c_adapter *adapter, int address, ...@@ -190,36 +200,23 @@ static int lm75_detect(struct i2c_adapter *adapter, int address,
/* Tell the I2C layer a new client has arrived */ /* Tell the I2C layer a new client has arrived */
if ((err = i2c_attach_client(new_client))) if ((err = i2c_attach_client(new_client)))
goto error3; goto exit_free;
/* Register a new directory entry with module sensors */ device_create_file(&new_client->dev, &dev_attr_temp_max);
i = i2c_register_entry(new_client, type_name, lm75_dir_table_template); device_create_file(&new_client->dev, &dev_attr_temp_min);
if (i < 0) { device_create_file(&new_client->dev, &dev_attr_temp_input);
err = i;
goto error4;
}
data->sysctl_id = i;
lm75_init_client(new_client); lm75_init_client(new_client);
return 0; return 0;
/* OK, this is not exactly good programming practice, usually. But it is exit_free:
very code-efficient in this case. */
error4:
i2c_detach_client(new_client);
error3:
error1:
kfree(new_client); kfree(new_client);
error0: exit:
return err; return err;
} }
static int lm75_detach_client(struct i2c_client *client) static int lm75_detach_client(struct i2c_client *client)
{ {
struct lm75_data *data = i2c_get_clientdata(client);
i2c_deregister_entry(data->sysctl_id);
i2c_detach_client(client); i2c_detach_client(client);
kfree(client); kfree(client);
return 0; return 0;
...@@ -271,12 +268,11 @@ static void lm75_update_client(struct i2c_client *client) ...@@ -271,12 +268,11 @@ static void lm75_update_client(struct i2c_client *client)
if ((jiffies - data->last_updated > HZ + HZ / 2) || if ((jiffies - data->last_updated > HZ + HZ / 2) ||
(jiffies < data->last_updated) || !data->valid) { (jiffies < data->last_updated) || !data->valid) {
pr_debug("Starting lm75 update\n"); dev_dbg(&client->dev, "Starting lm75 update\n");
data->temp = lm75_read_value(client, LM75_REG_TEMP); data->temp_input = lm75_read_value(client, LM75_REG_TEMP);
data->temp_os = lm75_read_value(client, LM75_REG_TEMP_OS); data->temp_max = lm75_read_value(client, LM75_REG_TEMP_OS);
data->temp_hyst = data->temp_hyst = lm75_read_value(client, LM75_REG_TEMP_HYST);
lm75_read_value(client, LM75_REG_TEMP_HYST);
data->last_updated = jiffies; data->last_updated = jiffies;
data->valid = 1; data->valid = 1;
} }
...@@ -284,33 +280,6 @@ static void lm75_update_client(struct i2c_client *client) ...@@ -284,33 +280,6 @@ static void lm75_update_client(struct i2c_client *client)
up(&data->update_lock); up(&data->update_lock);
} }
static void lm75_temp(struct i2c_client *client, int operation, int ctl_name,
int *nrels_mag, long *results)
{
struct lm75_data *data = i2c_get_clientdata(client);
if (operation == SENSORS_PROC_REAL_INFO)
*nrels_mag = 1;
else if (operation == SENSORS_PROC_REAL_READ) {
lm75_update_client(client);
results[0] = TEMP_FROM_REG(data->temp_os);
results[1] = TEMP_FROM_REG(data->temp_hyst);
results[2] = TEMP_FROM_REG(data->temp);
*nrels_mag = 3;
} else if (operation == SENSORS_PROC_REAL_WRITE) {
if (*nrels_mag >= 1) {
data->temp_os = TEMP_TO_REG(results[0]);
lm75_write_value(client, LM75_REG_TEMP_OS,
data->temp_os);
}
if (*nrels_mag >= 2) {
data->temp_hyst = TEMP_TO_REG(results[1]);
lm75_write_value(client, LM75_REG_TEMP_HYST,
data->temp_hyst);
}
}
}
static int __init sensors_lm75_init(void) static int __init sensors_lm75_init(void)
{ {
return i2c_add_driver(&lm75_driver); return i2c_add_driver(&lm75_driver);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment