lm75.c 8.9 KB

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