lm75.c 10 KB

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