lm75.c 9.2 KB

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