lm75.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. /*
  28. * This driver handles the LM75 and compatible digital temperature sensors.
  29. */
  30. enum lm75_type { /* keep sorted in alphabetical order */
  31. ds1775,
  32. ds75,
  33. lm75,
  34. lm75a,
  35. max6625,
  36. max6626,
  37. mcp980x,
  38. stds75,
  39. tcn75,
  40. tmp100,
  41. tmp101,
  42. tmp175,
  43. tmp275,
  44. tmp75,
  45. };
  46. /* Addresses scanned */
  47. static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
  48. 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
  49. /* The LM75 registers */
  50. #define LM75_REG_CONF 0x01
  51. static const u8 LM75_REG_TEMP[3] = {
  52. 0x00, /* input */
  53. 0x03, /* max */
  54. 0x02, /* hyst */
  55. };
  56. /* Each client has this additional data */
  57. struct lm75_data {
  58. struct device *hwmon_dev;
  59. struct mutex update_lock;
  60. u8 orig_conf;
  61. char valid; /* !=0 if registers are valid */
  62. unsigned long last_updated; /* In jiffies */
  63. u16 temp[3]; /* Register values,
  64. 0 = input
  65. 1 = max
  66. 2 = hyst */
  67. };
  68. static int lm75_read_value(struct i2c_client *client, u8 reg);
  69. static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
  70. static struct lm75_data *lm75_update_device(struct device *dev);
  71. /*-----------------------------------------------------------------------*/
  72. /* sysfs attributes for hwmon */
  73. static ssize_t show_temp(struct device *dev, struct device_attribute *da,
  74. char *buf)
  75. {
  76. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  77. struct lm75_data *data = lm75_update_device(dev);
  78. return sprintf(buf, "%d\n",
  79. LM75_TEMP_FROM_REG(data->temp[attr->index]));
  80. }
  81. static ssize_t set_temp(struct device *dev, struct device_attribute *da,
  82. const char *buf, size_t count)
  83. {
  84. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  85. struct i2c_client *client = to_i2c_client(dev);
  86. struct lm75_data *data = i2c_get_clientdata(client);
  87. int nr = attr->index;
  88. long temp = simple_strtol(buf, NULL, 10);
  89. mutex_lock(&data->update_lock);
  90. data->temp[nr] = LM75_TEMP_TO_REG(temp);
  91. lm75_write_value(client, LM75_REG_TEMP[nr], data->temp[nr]);
  92. mutex_unlock(&data->update_lock);
  93. return count;
  94. }
  95. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  96. show_temp, set_temp, 1);
  97. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
  98. show_temp, set_temp, 2);
  99. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  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. /*-----------------------------------------------------------------------*/
  110. /* device probe and removal */
  111. static int
  112. lm75_probe(struct i2c_client *client, const struct i2c_device_id *id)
  113. {
  114. struct lm75_data *data;
  115. int status;
  116. u8 set_mask, clr_mask;
  117. int new;
  118. if (!i2c_check_functionality(client->adapter,
  119. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
  120. return -EIO;
  121. data = kzalloc(sizeof(struct lm75_data), GFP_KERNEL);
  122. if (!data)
  123. return -ENOMEM;
  124. i2c_set_clientdata(client, data);
  125. mutex_init(&data->update_lock);
  126. /* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
  127. * Then tweak to be more precise when appropriate.
  128. */
  129. set_mask = 0;
  130. clr_mask = (1 << 0) /* continuous conversions */
  131. | (1 << 6) | (1 << 5); /* 9-bit mode */
  132. /* configure as specified */
  133. status = lm75_read_value(client, LM75_REG_CONF);
  134. if (status < 0) {
  135. dev_dbg(&client->dev, "Can't read config? %d\n", status);
  136. goto exit_free;
  137. }
  138. data->orig_conf = status;
  139. new = status & ~clr_mask;
  140. new |= set_mask;
  141. if (status != new)
  142. lm75_write_value(client, LM75_REG_CONF, new);
  143. dev_dbg(&client->dev, "Config %02x\n", new);
  144. /* Register sysfs hooks */
  145. status = sysfs_create_group(&client->dev.kobj, &lm75_group);
  146. if (status)
  147. goto exit_free;
  148. data->hwmon_dev = hwmon_device_register(&client->dev);
  149. if (IS_ERR(data->hwmon_dev)) {
  150. status = PTR_ERR(data->hwmon_dev);
  151. goto exit_remove;
  152. }
  153. dev_info(&client->dev, "%s: sensor '%s'\n",
  154. dev_name(data->hwmon_dev), client->name);
  155. return 0;
  156. exit_remove:
  157. sysfs_remove_group(&client->dev.kobj, &lm75_group);
  158. exit_free:
  159. i2c_set_clientdata(client, NULL);
  160. kfree(data);
  161. return status;
  162. }
  163. static int lm75_remove(struct i2c_client *client)
  164. {
  165. struct lm75_data *data = i2c_get_clientdata(client);
  166. hwmon_device_unregister(data->hwmon_dev);
  167. sysfs_remove_group(&client->dev.kobj, &lm75_group);
  168. lm75_write_value(client, LM75_REG_CONF, data->orig_conf);
  169. i2c_set_clientdata(client, NULL);
  170. kfree(data);
  171. return 0;
  172. }
  173. static const struct i2c_device_id lm75_ids[] = {
  174. { "ds1775", ds1775, },
  175. { "ds75", ds75, },
  176. { "lm75", lm75, },
  177. { "lm75a", lm75a, },
  178. { "max6625", max6625, },
  179. { "max6626", max6626, },
  180. { "mcp980x", mcp980x, },
  181. { "stds75", stds75, },
  182. { "tcn75", tcn75, },
  183. { "tmp100", tmp100, },
  184. { "tmp101", tmp101, },
  185. { "tmp175", tmp175, },
  186. { "tmp275", tmp275, },
  187. { "tmp75", tmp75, },
  188. { /* LIST END */ }
  189. };
  190. MODULE_DEVICE_TABLE(i2c, lm75_ids);
  191. /* Return 0 if detection is successful, -ENODEV otherwise */
  192. static int lm75_detect(struct i2c_client *new_client,
  193. struct i2c_board_info *info)
  194. {
  195. struct i2c_adapter *adapter = new_client->adapter;
  196. int i;
  197. int cur, conf, hyst, os;
  198. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  199. I2C_FUNC_SMBUS_WORD_DATA))
  200. return -ENODEV;
  201. /* Now, we do the remaining detection. There is no identification-
  202. dedicated register so we have to rely on several tricks:
  203. unused bits, registers cycling over 8-address boundaries,
  204. addresses 0x04-0x07 returning the last read value.
  205. The cycling+unused addresses combination is not tested,
  206. since it would significantly slow the detection down and would
  207. hardly add any value. */
  208. /* Unused addresses */
  209. cur = i2c_smbus_read_word_data(new_client, 0);
  210. conf = i2c_smbus_read_byte_data(new_client, 1);
  211. hyst = i2c_smbus_read_word_data(new_client, 2);
  212. if (i2c_smbus_read_word_data(new_client, 4) != hyst
  213. || i2c_smbus_read_word_data(new_client, 5) != hyst
  214. || i2c_smbus_read_word_data(new_client, 6) != hyst
  215. || i2c_smbus_read_word_data(new_client, 7) != hyst)
  216. return -ENODEV;
  217. os = i2c_smbus_read_word_data(new_client, 3);
  218. if (i2c_smbus_read_word_data(new_client, 4) != os
  219. || i2c_smbus_read_word_data(new_client, 5) != os
  220. || i2c_smbus_read_word_data(new_client, 6) != os
  221. || i2c_smbus_read_word_data(new_client, 7) != os)
  222. return -ENODEV;
  223. /* Unused bits */
  224. if (conf & 0xe0)
  225. return -ENODEV;
  226. /* Addresses cycling */
  227. for (i = 8; i < 0xff; i += 8) {
  228. if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
  229. || i2c_smbus_read_word_data(new_client, i + 2) != hyst
  230. || i2c_smbus_read_word_data(new_client, i + 3) != os)
  231. return -ENODEV;
  232. }
  233. strlcpy(info->type, "lm75", I2C_NAME_SIZE);
  234. return 0;
  235. }
  236. static struct i2c_driver lm75_driver = {
  237. .class = I2C_CLASS_HWMON,
  238. .driver = {
  239. .name = "lm75",
  240. },
  241. .probe = lm75_probe,
  242. .remove = lm75_remove,
  243. .id_table = lm75_ids,
  244. .detect = lm75_detect,
  245. .address_list = normal_i2c,
  246. };
  247. /*-----------------------------------------------------------------------*/
  248. /* register access */
  249. /* All registers are word-sized, except for the configuration register.
  250. LM75 uses a high-byte first convention, which is exactly opposite to
  251. the SMBus standard. */
  252. static int lm75_read_value(struct i2c_client *client, u8 reg)
  253. {
  254. int value;
  255. if (reg == LM75_REG_CONF)
  256. return i2c_smbus_read_byte_data(client, reg);
  257. value = i2c_smbus_read_word_data(client, reg);
  258. return (value < 0) ? value : swab16(value);
  259. }
  260. static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
  261. {
  262. if (reg == LM75_REG_CONF)
  263. return i2c_smbus_write_byte_data(client, reg, value);
  264. else
  265. return i2c_smbus_write_word_data(client, reg, swab16(value));
  266. }
  267. static struct lm75_data *lm75_update_device(struct device *dev)
  268. {
  269. struct i2c_client *client = to_i2c_client(dev);
  270. struct lm75_data *data = i2c_get_clientdata(client);
  271. mutex_lock(&data->update_lock);
  272. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  273. || !data->valid) {
  274. int i;
  275. dev_dbg(&client->dev, "Starting lm75 update\n");
  276. for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
  277. int status;
  278. status = lm75_read_value(client, LM75_REG_TEMP[i]);
  279. if (status < 0)
  280. dev_dbg(&client->dev, "reg %d, err %d\n",
  281. LM75_REG_TEMP[i], status);
  282. else
  283. data->temp[i] = status;
  284. }
  285. data->last_updated = jiffies;
  286. data->valid = 1;
  287. }
  288. mutex_unlock(&data->update_lock);
  289. return data;
  290. }
  291. /*-----------------------------------------------------------------------*/
  292. /* module glue */
  293. static int __init sensors_lm75_init(void)
  294. {
  295. return i2c_add_driver(&lm75_driver);
  296. }
  297. static void __exit sensors_lm75_exit(void)
  298. {
  299. i2c_del_driver(&lm75_driver);
  300. }
  301. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
  302. MODULE_DESCRIPTION("LM75 driver");
  303. MODULE_LICENSE("GPL");
  304. module_init(sensors_lm75_init);
  305. module_exit(sensors_lm75_exit);