]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | lm75.c - Part of lm_sensors, Linux kernel modules for hardware | |
3 | monitoring | |
4 | Copyright (c) 1998, 1999 Frodo Looijaard <[email protected]> | |
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 | ||
1da177e4 LT |
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> | |
943b0830 | 26 | #include <linux/hwmon.h> |
9ca8e40c | 27 | #include <linux/hwmon-sysfs.h> |
943b0830 | 28 | #include <linux/err.h> |
9a61bf63 | 29 | #include <linux/mutex.h> |
1da177e4 LT |
30 | #include "lm75.h" |
31 | ||
32 | ||
01a52397 DB |
33 | /* |
34 | * This driver handles the LM75 and compatible digital temperature sensors. | |
35 | * Compatibles include at least the DS75, DS1775, MCP980x, STDS75, TCN75, | |
36 | * TMP100, TMP101, TMP75, TMP175, and TMP275. | |
37 | */ | |
38 | ||
39 | /* Addresses scanned by legacy style driver binding */ | |
25e9c86d | 40 | static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c, |
1da177e4 | 41 | 0x4d, 0x4e, 0x4f, I2C_CLIENT_END }; |
1da177e4 | 42 | |
01a52397 | 43 | /* Insmod parameters (only for legacy style driver binding) */ |
f4b50261 | 44 | I2C_CLIENT_INSMOD_1(lm75); |
1da177e4 | 45 | |
1da177e4 LT |
46 | |
47 | /* The LM75 registers */ | |
1da177e4 | 48 | #define LM75_REG_CONF 0x01 |
9ca8e40c JD |
49 | static const u8 LM75_REG_TEMP[3] = { |
50 | 0x00, /* input */ | |
51 | 0x03, /* max */ | |
52 | 0x02, /* hyst */ | |
53 | }; | |
1da177e4 LT |
54 | |
55 | /* Each client has this additional data */ | |
56 | struct lm75_data { | |
57 | struct i2c_client client; | |
01a52397 | 58 | struct device *hwmon_dev; |
9a61bf63 | 59 | struct mutex update_lock; |
01a52397 | 60 | char valid; /* !=0 if registers are valid */ |
1da177e4 | 61 | unsigned long last_updated; /* In jiffies */ |
9ca8e40c JD |
62 | u16 temp[3]; /* Register values, |
63 | 0 = input | |
64 | 1 = max | |
65 | 2 = hyst */ | |
1da177e4 LT |
66 | }; |
67 | ||
1da177e4 | 68 | static void lm75_init_client(struct i2c_client *client); |
1da177e4 LT |
69 | static int lm75_read_value(struct i2c_client *client, u8 reg); |
70 | static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value); | |
71 | static struct lm75_data *lm75_update_device(struct device *dev); | |
72 | ||
73 | ||
01a52397 DB |
74 | /*-----------------------------------------------------------------------*/ |
75 | ||
76 | /* sysfs attributes for hwmon */ | |
1da177e4 | 77 | |
9ca8e40c JD |
78 | static ssize_t show_temp(struct device *dev, struct device_attribute *da, |
79 | char *buf) | |
80 | { | |
81 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | |
82 | struct lm75_data *data = lm75_update_device(dev); | |
83 | return sprintf(buf, "%d\n", | |
84 | LM75_TEMP_FROM_REG(data->temp[attr->index])); | |
1da177e4 | 85 | } |
9ca8e40c JD |
86 | |
87 | static ssize_t set_temp(struct device *dev, struct device_attribute *da, | |
88 | const char *buf, size_t count) | |
89 | { | |
90 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | |
91 | struct i2c_client *client = to_i2c_client(dev); | |
92 | struct lm75_data *data = i2c_get_clientdata(client); | |
93 | int nr = attr->index; | |
5bfedac0 | 94 | long temp = simple_strtol(buf, NULL, 10); |
9ca8e40c JD |
95 | |
96 | mutex_lock(&data->update_lock); | |
97 | data->temp[nr] = LM75_TEMP_TO_REG(temp); | |
98 | lm75_write_value(client, LM75_REG_TEMP[nr], data->temp[nr]); | |
99 | mutex_unlock(&data->update_lock); | |
100 | return count; | |
1da177e4 | 101 | } |
1da177e4 | 102 | |
9ca8e40c JD |
103 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, |
104 | show_temp, set_temp, 1); | |
105 | static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, | |
106 | show_temp, set_temp, 2); | |
107 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); | |
1da177e4 | 108 | |
c1685f61 | 109 | static struct attribute *lm75_attributes[] = { |
9ca8e40c JD |
110 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
111 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
112 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, | |
c1685f61 MH |
113 | |
114 | NULL | |
115 | }; | |
116 | ||
117 | static const struct attribute_group lm75_group = { | |
118 | .attrs = lm75_attributes, | |
119 | }; | |
120 | ||
01a52397 DB |
121 | /*-----------------------------------------------------------------------*/ |
122 | ||
123 | /* "Legacy" I2C driver binding */ | |
124 | ||
125 | static struct i2c_driver lm75_driver; | |
126 | ||
2ed2dc3c | 127 | /* This function is called by i2c_probe */ |
1da177e4 LT |
128 | static int lm75_detect(struct i2c_adapter *adapter, int address, int kind) |
129 | { | |
130 | int i; | |
131 | struct i2c_client *new_client; | |
132 | struct lm75_data *data; | |
133 | int err = 0; | |
1da177e4 | 134 | |
1da177e4 LT |
135 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
136 | I2C_FUNC_SMBUS_WORD_DATA)) | |
137 | goto exit; | |
138 | ||
01a52397 DB |
139 | /* OK. For now, we presume we have a valid address. We create the |
140 | client structure, even though there may be no sensor present. | |
141 | But it allows us to use i2c_smbus_read_*_data() calls. */ | |
ba9c2e8d | 142 | if (!(data = kzalloc(sizeof(struct lm75_data), GFP_KERNEL))) { |
1da177e4 LT |
143 | err = -ENOMEM; |
144 | goto exit; | |
145 | } | |
1da177e4 LT |
146 | |
147 | new_client = &data->client; | |
148 | i2c_set_clientdata(new_client, data); | |
149 | new_client->addr = address; | |
150 | new_client->adapter = adapter; | |
151 | new_client->driver = &lm75_driver; | |
152 | new_client->flags = 0; | |
153 | ||
154 | /* Now, we do the remaining detection. There is no identification- | |
155 | dedicated register so we have to rely on several tricks: | |
156 | unused bits, registers cycling over 8-address boundaries, | |
157 | addresses 0x04-0x07 returning the last read value. | |
158 | The cycling+unused addresses combination is not tested, | |
159 | since it would significantly slow the detection down and would | |
160 | hardly add any value. */ | |
161 | if (kind < 0) { | |
162 | int cur, conf, hyst, os; | |
163 | ||
164 | /* Unused addresses */ | |
165 | cur = i2c_smbus_read_word_data(new_client, 0); | |
166 | conf = i2c_smbus_read_byte_data(new_client, 1); | |
167 | hyst = i2c_smbus_read_word_data(new_client, 2); | |
168 | if (i2c_smbus_read_word_data(new_client, 4) != hyst | |
169 | || i2c_smbus_read_word_data(new_client, 5) != hyst | |
170 | || i2c_smbus_read_word_data(new_client, 6) != hyst | |
171 | || i2c_smbus_read_word_data(new_client, 7) != hyst) | |
01a52397 | 172 | goto exit_free; |
1da177e4 LT |
173 | os = i2c_smbus_read_word_data(new_client, 3); |
174 | if (i2c_smbus_read_word_data(new_client, 4) != os | |
175 | || i2c_smbus_read_word_data(new_client, 5) != os | |
176 | || i2c_smbus_read_word_data(new_client, 6) != os | |
177 | || i2c_smbus_read_word_data(new_client, 7) != os) | |
01a52397 | 178 | goto exit_free; |
1da177e4 LT |
179 | |
180 | /* Unused bits */ | |
181 | if (conf & 0xe0) | |
01a52397 | 182 | goto exit_free; |
1da177e4 LT |
183 | |
184 | /* Addresses cycling */ | |
185 | for (i = 8; i < 0xff; i += 8) | |
186 | if (i2c_smbus_read_byte_data(new_client, i + 1) != conf | |
187 | || i2c_smbus_read_word_data(new_client, i + 2) != hyst | |
188 | || i2c_smbus_read_word_data(new_client, i + 3) != os) | |
189 | goto exit_free; | |
190 | } | |
191 | ||
01a52397 DB |
192 | /* NOTE: we treat "force=..." and "force_lm75=..." the same. */ |
193 | strlcpy(new_client->name, "lm75", I2C_NAME_SIZE); | |
1da177e4 LT |
194 | |
195 | /* Fill in the remaining client fields and put it into the global list */ | |
1da177e4 | 196 | data->valid = 0; |
9a61bf63 | 197 | mutex_init(&data->update_lock); |
1da177e4 LT |
198 | |
199 | /* Tell the I2C layer a new client has arrived */ | |
200 | if ((err = i2c_attach_client(new_client))) | |
201 | goto exit_free; | |
202 | ||
203 | /* Initialize the LM75 chip */ | |
204 | lm75_init_client(new_client); | |
01a52397 | 205 | |
1da177e4 | 206 | /* Register sysfs hooks */ |
c1685f61 MH |
207 | if ((err = sysfs_create_group(&new_client->dev.kobj, &lm75_group))) |
208 | goto exit_detach; | |
209 | ||
1beeffe4 TJ |
210 | data->hwmon_dev = hwmon_device_register(&new_client->dev); |
211 | if (IS_ERR(data->hwmon_dev)) { | |
212 | err = PTR_ERR(data->hwmon_dev); | |
c1685f61 | 213 | goto exit_remove; |
943b0830 MH |
214 | } |
215 | ||
1da177e4 LT |
216 | return 0; |
217 | ||
c1685f61 MH |
218 | exit_remove: |
219 | sysfs_remove_group(&new_client->dev.kobj, &lm75_group); | |
943b0830 MH |
220 | exit_detach: |
221 | i2c_detach_client(new_client); | |
1da177e4 LT |
222 | exit_free: |
223 | kfree(data); | |
224 | exit: | |
225 | return err; | |
226 | } | |
227 | ||
01a52397 DB |
228 | static int lm75_attach_adapter(struct i2c_adapter *adapter) |
229 | { | |
230 | if (!(adapter->class & I2C_CLASS_HWMON)) | |
231 | return 0; | |
232 | return i2c_probe(adapter, &addr_data, lm75_detect); | |
233 | } | |
234 | ||
1da177e4 LT |
235 | static int lm75_detach_client(struct i2c_client *client) |
236 | { | |
943b0830 | 237 | struct lm75_data *data = i2c_get_clientdata(client); |
1beeffe4 | 238 | hwmon_device_unregister(data->hwmon_dev); |
c1685f61 | 239 | sysfs_remove_group(&client->dev.kobj, &lm75_group); |
1da177e4 | 240 | i2c_detach_client(client); |
943b0830 | 241 | kfree(data); |
1da177e4 LT |
242 | return 0; |
243 | } | |
244 | ||
01a52397 DB |
245 | static struct i2c_driver lm75_driver = { |
246 | .driver = { | |
247 | .name = "lm75", | |
248 | }, | |
249 | .attach_adapter = lm75_attach_adapter, | |
250 | .detach_client = lm75_detach_client, | |
251 | }; | |
252 | ||
253 | /*-----------------------------------------------------------------------*/ | |
254 | ||
255 | /* register access */ | |
256 | ||
1da177e4 LT |
257 | /* All registers are word-sized, except for the configuration register. |
258 | LM75 uses a high-byte first convention, which is exactly opposite to | |
ccd6befc | 259 | the SMBus standard. */ |
1da177e4 LT |
260 | static int lm75_read_value(struct i2c_client *client, u8 reg) |
261 | { | |
bcccc3a2 DB |
262 | int value; |
263 | ||
1da177e4 LT |
264 | if (reg == LM75_REG_CONF) |
265 | return i2c_smbus_read_byte_data(client, reg); | |
bcccc3a2 DB |
266 | |
267 | value = i2c_smbus_read_word_data(client, reg); | |
268 | return (value < 0) ? value : swab16(value); | |
1da177e4 LT |
269 | } |
270 | ||
1da177e4 LT |
271 | static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value) |
272 | { | |
273 | if (reg == LM75_REG_CONF) | |
274 | return i2c_smbus_write_byte_data(client, reg, value); | |
275 | else | |
276 | return i2c_smbus_write_word_data(client, reg, swab16(value)); | |
277 | } | |
278 | ||
279 | static void lm75_init_client(struct i2c_client *client) | |
280 | { | |
e647ecf1 JD |
281 | int reg; |
282 | ||
283 | /* Enable if in shutdown mode */ | |
284 | reg = lm75_read_value(client, LM75_REG_CONF); | |
285 | if (reg >= 0 && (reg & 0x01)) | |
286 | lm75_write_value(client, LM75_REG_CONF, reg & 0xfe); | |
1da177e4 LT |
287 | } |
288 | ||
289 | static struct lm75_data *lm75_update_device(struct device *dev) | |
290 | { | |
291 | struct i2c_client *client = to_i2c_client(dev); | |
292 | struct lm75_data *data = i2c_get_clientdata(client); | |
293 | ||
9a61bf63 | 294 | mutex_lock(&data->update_lock); |
1da177e4 LT |
295 | |
296 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) | |
297 | || !data->valid) { | |
9ca8e40c | 298 | int i; |
1da177e4 LT |
299 | dev_dbg(&client->dev, "Starting lm75 update\n"); |
300 | ||
bcccc3a2 DB |
301 | for (i = 0; i < ARRAY_SIZE(data->temp); i++) { |
302 | int status; | |
303 | ||
304 | status = lm75_read_value(client, LM75_REG_TEMP[i]); | |
305 | if (status < 0) | |
306 | dev_dbg(&client->dev, "reg %d, err %d\n", | |
307 | LM75_REG_TEMP[i], status); | |
308 | else | |
309 | data->temp[i] = status; | |
310 | } | |
1da177e4 LT |
311 | data->last_updated = jiffies; |
312 | data->valid = 1; | |
313 | } | |
314 | ||
9a61bf63 | 315 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
316 | |
317 | return data; | |
318 | } | |
319 | ||
01a52397 DB |
320 | /*-----------------------------------------------------------------------*/ |
321 | ||
322 | /* module glue */ | |
323 | ||
1da177e4 LT |
324 | static int __init sensors_lm75_init(void) |
325 | { | |
326 | return i2c_add_driver(&lm75_driver); | |
327 | } | |
328 | ||
329 | static void __exit sensors_lm75_exit(void) | |
330 | { | |
331 | i2c_del_driver(&lm75_driver); | |
332 | } | |
333 | ||
334 | MODULE_AUTHOR("Frodo Looijaard <[email protected]>"); | |
335 | MODULE_DESCRIPTION("LM75 driver"); | |
336 | MODULE_LICENSE("GPL"); | |
337 | ||
338 | module_init(sensors_lm75_init); | |
339 | module_exit(sensors_lm75_exit); |