lm75.c 9.2 KB

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