Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | lm75.c - Part of lm_sensors, Linux kernel modules for hardware |
| 3 | monitoring |
| 4 | Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/jiffies.h> |
| 25 | #include <linux/i2c.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 26 | #include <linux/hwmon.h> |
Jean Delvare | 9ca8e40c8 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 27 | #include <linux/hwmon-sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 28 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 29 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "lm75.h" |
| 31 | |
| 32 | |
| 33 | /* Addresses to scan */ |
| 34 | static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c, |
| 35 | 0x4d, 0x4e, 0x4f, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | /* Insmod parameters */ |
Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 38 | I2C_CLIENT_INSMOD_1(lm75); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | /* Many LM75 constants specified below */ |
| 41 | |
| 42 | /* The LM75 registers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #define LM75_REG_CONF 0x01 |
Jean Delvare | 9ca8e40c8 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 44 | static const u8 LM75_REG_TEMP[3] = { |
| 45 | 0x00, /* input */ |
| 46 | 0x03, /* max */ |
| 47 | 0x02, /* hyst */ |
| 48 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | /* Each client has this additional data */ |
| 51 | struct lm75_data { |
| 52 | struct i2c_client client; |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 53 | struct device *hwmon_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 54 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | char valid; /* !=0 if following fields are valid */ |
| 56 | unsigned long last_updated; /* In jiffies */ |
Jean Delvare | 9ca8e40c8 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 57 | u16 temp[3]; /* Register values, |
| 58 | 0 = input |
| 59 | 1 = max |
| 60 | 2 = hyst */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | static int lm75_attach_adapter(struct i2c_adapter *adapter); |
| 64 | static int lm75_detect(struct i2c_adapter *adapter, int address, int kind); |
| 65 | static void lm75_init_client(struct i2c_client *client); |
| 66 | static int lm75_detach_client(struct i2c_client *client); |
| 67 | static int lm75_read_value(struct i2c_client *client, u8 reg); |
| 68 | static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value); |
| 69 | static struct lm75_data *lm75_update_device(struct device *dev); |
| 70 | |
| 71 | |
| 72 | /* This is the driver that will be inserted */ |
| 73 | static struct i2c_driver lm75_driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 74 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 75 | .name = "lm75", |
| 76 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | .id = I2C_DRIVERID_LM75, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | .attach_adapter = lm75_attach_adapter, |
| 79 | .detach_client = lm75_detach_client, |
| 80 | }; |
| 81 | |
Jean Delvare | 9ca8e40c8 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 82 | static ssize_t show_temp(struct device *dev, struct device_attribute *da, |
| 83 | char *buf) |
| 84 | { |
| 85 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 86 | struct lm75_data *data = lm75_update_device(dev); |
| 87 | return sprintf(buf, "%d\n", |
| 88 | LM75_TEMP_FROM_REG(data->temp[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Jean Delvare | 9ca8e40c8 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 91 | static ssize_t set_temp(struct device *dev, struct device_attribute *da, |
| 92 | const char *buf, size_t count) |
| 93 | { |
| 94 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 95 | struct i2c_client *client = to_i2c_client(dev); |
| 96 | struct lm75_data *data = i2c_get_clientdata(client); |
| 97 | int nr = attr->index; |
Christian Hohnstaedt | 5bfedac | 2007-08-16 11:40:10 +0200 | [diff] [blame] | 98 | long temp = simple_strtol(buf, NULL, 10); |
Jean Delvare | 9ca8e40c8 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 99 | |
| 100 | mutex_lock(&data->update_lock); |
| 101 | data->temp[nr] = LM75_TEMP_TO_REG(temp); |
| 102 | lm75_write_value(client, LM75_REG_TEMP[nr], data->temp[nr]); |
| 103 | mutex_unlock(&data->update_lock); |
| 104 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Jean Delvare | 9ca8e40c8 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 107 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, |
| 108 | show_temp, set_temp, 1); |
| 109 | static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, |
| 110 | show_temp, set_temp, 2); |
| 111 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | static int lm75_attach_adapter(struct i2c_adapter *adapter) |
| 114 | { |
| 115 | if (!(adapter->class & I2C_CLASS_HWMON)) |
| 116 | return 0; |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 117 | return i2c_probe(adapter, &addr_data, lm75_detect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 120 | static struct attribute *lm75_attributes[] = { |
Jean Delvare | 9ca8e40c8 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 121 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 122 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 123 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 124 | |
| 125 | NULL |
| 126 | }; |
| 127 | |
| 128 | static const struct attribute_group lm75_group = { |
| 129 | .attrs = lm75_attributes, |
| 130 | }; |
| 131 | |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 132 | /* This function is called by i2c_probe */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | static int lm75_detect(struct i2c_adapter *adapter, int address, int kind) |
| 134 | { |
| 135 | int i; |
| 136 | struct i2c_client *new_client; |
| 137 | struct lm75_data *data; |
| 138 | int err = 0; |
| 139 | const char *name = ""; |
| 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
| 142 | I2C_FUNC_SMBUS_WORD_DATA)) |
| 143 | goto exit; |
| 144 | |
| 145 | /* OK. For now, we presume we have a valid client. We now create the |
| 146 | client structure, even though we cannot fill it completely yet. |
| 147 | But it allows us to access lm75_{read,write}_value. */ |
Deepak Saxena | ba9c2e8 | 2005-10-17 23:08:32 +0200 | [diff] [blame] | 148 | if (!(data = kzalloc(sizeof(struct lm75_data), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | err = -ENOMEM; |
| 150 | goto exit; |
| 151 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | new_client = &data->client; |
| 154 | i2c_set_clientdata(new_client, data); |
| 155 | new_client->addr = address; |
| 156 | new_client->adapter = adapter; |
| 157 | new_client->driver = &lm75_driver; |
| 158 | new_client->flags = 0; |
| 159 | |
| 160 | /* Now, we do the remaining detection. There is no identification- |
| 161 | dedicated register so we have to rely on several tricks: |
| 162 | unused bits, registers cycling over 8-address boundaries, |
| 163 | addresses 0x04-0x07 returning the last read value. |
| 164 | The cycling+unused addresses combination is not tested, |
| 165 | since it would significantly slow the detection down and would |
| 166 | hardly add any value. */ |
| 167 | if (kind < 0) { |
| 168 | int cur, conf, hyst, os; |
| 169 | |
| 170 | /* Unused addresses */ |
| 171 | cur = i2c_smbus_read_word_data(new_client, 0); |
| 172 | conf = i2c_smbus_read_byte_data(new_client, 1); |
| 173 | hyst = i2c_smbus_read_word_data(new_client, 2); |
| 174 | if (i2c_smbus_read_word_data(new_client, 4) != hyst |
| 175 | || i2c_smbus_read_word_data(new_client, 5) != hyst |
| 176 | || i2c_smbus_read_word_data(new_client, 6) != hyst |
| 177 | || i2c_smbus_read_word_data(new_client, 7) != hyst) |
| 178 | goto exit_free; |
| 179 | os = i2c_smbus_read_word_data(new_client, 3); |
| 180 | if (i2c_smbus_read_word_data(new_client, 4) != os |
| 181 | || i2c_smbus_read_word_data(new_client, 5) != os |
| 182 | || i2c_smbus_read_word_data(new_client, 6) != os |
| 183 | || i2c_smbus_read_word_data(new_client, 7) != os) |
| 184 | goto exit_free; |
| 185 | |
| 186 | /* Unused bits */ |
| 187 | if (conf & 0xe0) |
| 188 | goto exit_free; |
| 189 | |
| 190 | /* Addresses cycling */ |
| 191 | for (i = 8; i < 0xff; i += 8) |
| 192 | if (i2c_smbus_read_byte_data(new_client, i + 1) != conf |
| 193 | || i2c_smbus_read_word_data(new_client, i + 2) != hyst |
| 194 | || i2c_smbus_read_word_data(new_client, i + 3) != os) |
| 195 | goto exit_free; |
| 196 | } |
| 197 | |
| 198 | /* Determine the chip type - only one kind supported! */ |
| 199 | if (kind <= 0) |
| 200 | kind = lm75; |
| 201 | |
| 202 | if (kind == lm75) { |
| 203 | name = "lm75"; |
| 204 | } |
| 205 | |
| 206 | /* Fill in the remaining client fields and put it into the global list */ |
| 207 | strlcpy(new_client->name, name, I2C_NAME_SIZE); |
| 208 | data->valid = 0; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 209 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
| 211 | /* Tell the I2C layer a new client has arrived */ |
| 212 | if ((err = i2c_attach_client(new_client))) |
| 213 | goto exit_free; |
| 214 | |
| 215 | /* Initialize the LM75 chip */ |
| 216 | lm75_init_client(new_client); |
| 217 | |
| 218 | /* Register sysfs hooks */ |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 219 | if ((err = sysfs_create_group(&new_client->dev.kobj, &lm75_group))) |
| 220 | goto exit_detach; |
| 221 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 222 | data->hwmon_dev = hwmon_device_register(&new_client->dev); |
| 223 | if (IS_ERR(data->hwmon_dev)) { |
| 224 | err = PTR_ERR(data->hwmon_dev); |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 225 | goto exit_remove; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 226 | } |
| 227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | return 0; |
| 229 | |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 230 | exit_remove: |
| 231 | sysfs_remove_group(&new_client->dev.kobj, &lm75_group); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 232 | exit_detach: |
| 233 | i2c_detach_client(new_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | exit_free: |
| 235 | kfree(data); |
| 236 | exit: |
| 237 | return err; |
| 238 | } |
| 239 | |
| 240 | static int lm75_detach_client(struct i2c_client *client) |
| 241 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 242 | struct lm75_data *data = i2c_get_clientdata(client); |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 243 | hwmon_device_unregister(data->hwmon_dev); |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 244 | sysfs_remove_group(&client->dev.kobj, &lm75_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | i2c_detach_client(client); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 246 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | /* All registers are word-sized, except for the configuration register. |
| 251 | LM75 uses a high-byte first convention, which is exactly opposite to |
| 252 | the usual practice. */ |
| 253 | static int lm75_read_value(struct i2c_client *client, u8 reg) |
| 254 | { |
| 255 | if (reg == LM75_REG_CONF) |
| 256 | return i2c_smbus_read_byte_data(client, reg); |
| 257 | else |
| 258 | return swab16(i2c_smbus_read_word_data(client, reg)); |
| 259 | } |
| 260 | |
| 261 | /* All registers are word-sized, except for the configuration register. |
| 262 | LM75 uses a high-byte first convention, which is exactly opposite to |
| 263 | the usual practice. */ |
| 264 | static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value) |
| 265 | { |
| 266 | if (reg == LM75_REG_CONF) |
| 267 | return i2c_smbus_write_byte_data(client, reg, value); |
| 268 | else |
| 269 | return i2c_smbus_write_word_data(client, reg, swab16(value)); |
| 270 | } |
| 271 | |
| 272 | static void lm75_init_client(struct i2c_client *client) |
| 273 | { |
Jean Delvare | e647ecf | 2005-07-27 21:28:28 +0200 | [diff] [blame] | 274 | int reg; |
| 275 | |
| 276 | /* Enable if in shutdown mode */ |
| 277 | reg = lm75_read_value(client, LM75_REG_CONF); |
| 278 | if (reg >= 0 && (reg & 0x01)) |
| 279 | lm75_write_value(client, LM75_REG_CONF, reg & 0xfe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | static struct lm75_data *lm75_update_device(struct device *dev) |
| 283 | { |
| 284 | struct i2c_client *client = to_i2c_client(dev); |
| 285 | struct lm75_data *data = i2c_get_clientdata(client); |
| 286 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 287 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
| 289 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 290 | || !data->valid) { |
Jean Delvare | 9ca8e40c8 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 291 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | dev_dbg(&client->dev, "Starting lm75 update\n"); |
| 293 | |
Jean Delvare | 9ca8e40c8 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 294 | for (i = 0; i < ARRAY_SIZE(data->temp); i++) |
| 295 | data->temp[i] = lm75_read_value(client, |
| 296 | LM75_REG_TEMP[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | data->last_updated = jiffies; |
| 298 | data->valid = 1; |
| 299 | } |
| 300 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 301 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
| 303 | return data; |
| 304 | } |
| 305 | |
| 306 | static int __init sensors_lm75_init(void) |
| 307 | { |
| 308 | return i2c_add_driver(&lm75_driver); |
| 309 | } |
| 310 | |
| 311 | static void __exit sensors_lm75_exit(void) |
| 312 | { |
| 313 | i2c_del_driver(&lm75_driver); |
| 314 | } |
| 315 | |
| 316 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>"); |
| 317 | MODULE_DESCRIPTION("LM75 driver"); |
| 318 | MODULE_LICENSE("GPL"); |
| 319 | |
| 320 | module_init(sensors_lm75_init); |
| 321 | module_exit(sensors_lm75_exit); |