lm75.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/i2c.h>
  25. #include <linux/hwmon.h>
  26. #include <linux/hwmon-sysfs.h>
  27. #include <linux/err.h>
  28. #include <linux/mutex.h>
  29. #include "lm75.h"
  30. /*
  31. * This driver handles the LM75 and compatible digital temperature sensors.
  32. */
  33. enum lm75_type { /* keep sorted in alphabetical order */
  34. ds1775,
  35. ds75,
  36. lm75,
  37. lm75a,
  38. max6625,
  39. max6626,
  40. mcp980x,
  41. stds75,
  42. tcn75,
  43. tmp100,
  44. tmp101,
  45. tmp105,
  46. tmp175,
  47. tmp275,
  48. tmp75,
  49. };
  50. /* Addresses scanned */
  51. static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
  52. 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
  53. /* The LM75 registers */
  54. #define LM75_REG_CONF 0x01
  55. static const u8 LM75_REG_TEMP[3] = {
  56. 0x00, /* input */
  57. 0x03, /* max */
  58. 0x02, /* hyst */
  59. };
  60. /* Each client has this additional data */
  61. struct lm75_data {
  62. struct device *hwmon_dev;
  63. struct mutex update_lock;
  64. u8 orig_conf;
  65. char valid; /* !=0 if registers are valid */
  66. unsigned long last_updated; /* In jiffies */
  67. u16 temp[3]; /* Register values,
  68. 0 = input
  69. 1 = max
  70. 2 = hyst */
  71. };
  72. static int lm75_read_value(struct i2c_client *client, u8 reg);
  73. static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
  74. static struct lm75_data *lm75_update_device(struct device *dev);
  75. /*-----------------------------------------------------------------------*/
  76. /* sysfs attributes for hwmon */
  77. static ssize_t show_temp(struct device *dev, struct device_attribute *da,
  78. char *buf)
  79. {
  80. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  81. struct lm75_data *data = lm75_update_device(dev);
  82. return sprintf(buf, "%d\n",
  83. LM75_TEMP_FROM_REG(data->temp[attr->index]));
  84. }
  85. static ssize_t set_temp(struct device *dev, struct device_attribute *da,
  86. const char *buf, size_t count)
  87. {
  88. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  89. struct i2c_client *client = to_i2c_client(dev);
  90. struct lm75_data *data = i2c_get_clientdata(client);
  91. int nr = attr->index;
  92. long temp;
  93. int error;
  94. error = strict_strtol(buf, 10, &temp);
  95. if (error)
  96. return error;
  97. mutex_lock(&data->update_lock);
  98. data->temp[nr] = LM75_TEMP_TO_REG(temp);
  99. lm75_write_value(client, LM75_REG_TEMP[nr], data->temp[nr]);
  100. mutex_unlock(&data->update_lock);
  101. return count;
  102. }
  103. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  104. show_temp, set_temp, 1);
  105. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
  106. show_temp, set_temp, 2);
  107. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  108. static struct attribute *lm75_attributes[] = {
  109. &sensor_dev_attr_temp1_input.dev_attr.attr,
  110. &sensor_dev_attr_temp1_max.dev_attr.attr,
  111. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  112. NULL
  113. };
  114. static const struct attribute_group lm75_group = {
  115. .attrs = lm75_attributes,
  116. };
  117. /*-----------------------------------------------------------------------*/
  118. /* device probe and removal */
  119. static int
  120. lm75_probe(struct i2c_client *client, const struct i2c_device_id *id)
  121. {
  122. struct lm75_data *data;
  123. int status;
  124. u8 set_mask, clr_mask;
  125. int new;
  126. if (!i2c_check_functionality(client->adapter,
  127. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
  128. return -EIO;
  129. data = kzalloc(sizeof(struct lm75_data), GFP_KERNEL);
  130. if (!data)
  131. return -ENOMEM;
  132. i2c_set_clientdata(client, data);
  133. mutex_init(&data->update_lock);
  134. /* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
  135. * Then tweak to be more precise when appropriate.
  136. */
  137. set_mask = 0;
  138. clr_mask = (1 << 0) /* continuous conversions */
  139. | (1 << 6) | (1 << 5); /* 9-bit mode */
  140. /* configure as specified */
  141. status = lm75_read_value(client, LM75_REG_CONF);
  142. if (status < 0) {
  143. dev_dbg(&client->dev, "Can't read config? %d\n", status);
  144. goto exit_free;
  145. }
  146. data->orig_conf = status;
  147. new = status & ~clr_mask;
  148. new |= set_mask;
  149. if (status != new)
  150. lm75_write_value(client, LM75_REG_CONF, new);
  151. dev_dbg(&client->dev, "Config %02x\n", new);
  152. /* Register sysfs hooks */
  153. status = sysfs_create_group(&client->dev.kobj, &lm75_group);
  154. if (status)
  155. goto exit_free;
  156. data->hwmon_dev = hwmon_device_register(&client->dev);
  157. if (IS_ERR(data->hwmon_dev)) {
  158. status = PTR_ERR(data->hwmon_dev);
  159. goto exit_remove;
  160. }
  161. dev_info(&client->dev, "%s: sensor '%s'\n",
  162. dev_name(data->hwmon_dev), client->name);
  163. return 0;
  164. exit_remove:
  165. sysfs_remove_group(&client->dev.kobj, &lm75_group);
  166. exit_free:
  167. kfree(data);
  168. return status;
  169. }
  170. static int lm75_remove(struct i2c_client *client)
  171. {
  172. struct lm75_data *data = i2c_get_clientdata(client);
  173. hwmon_device_unregister(data->hwmon_dev);
  174. sysfs_remove_group(&client->dev.kobj, &lm75_group);
  175. lm75_write_value(client, LM75_REG_CONF, data->orig_conf);
  176. kfree(data);
  177. return 0;
  178. }
  179. static const struct i2c_device_id lm75_ids[] = {
  180. { "ds1775", ds1775, },
  181. { "ds75", ds75, },
  182. { "lm75", lm75, },
  183. { "lm75a", lm75a, },
  184. { "max6625", max6625, },
  185. { "max6626", max6626, },
  186. { "mcp980x", mcp980x, },
  187. { "stds75", stds75, },
  188. { "tcn75", tcn75, },
  189. { "tmp100", tmp100, },
  190. { "tmp101", tmp101, },
  191. { "tmp105", tmp105, },
  192. { "tmp175", tmp175, },
  193. { "tmp275", tmp275, },
  194. { "tmp75", tmp75, },
  195. { /* LIST END */ }
  196. };
  197. MODULE_DEVICE_TABLE(i2c, lm75_ids);
  198. /* Return 0 if detection is successful, -ENODEV otherwise */
  199. static int lm75_detect(struct i2c_client *new_client,
  200. struct i2c_board_info *info)
  201. {
  202. struct i2c_adapter *adapter = new_client->adapter;
  203. int i;
  204. int cur, conf, hyst, os;
  205. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  206. I2C_FUNC_SMBUS_WORD_DATA))
  207. return -ENODEV;
  208. /* Now, we do the remaining detection. There is no identification-
  209. dedicated register so we have to rely on several tricks:
  210. unused bits, registers cycling over 8-address boundaries,
  211. addresses 0x04-0x07 returning the last read value.
  212. The cycling+unused addresses combination is not tested,
  213. since it would significantly slow the detection down and would
  214. hardly add any value. */
  215. /* Unused addresses */
  216. cur = i2c_smbus_read_word_data(new_client, 0);
  217. conf = i2c_smbus_read_byte_data(new_client, 1);
  218. hyst = i2c_smbus_read_word_data(new_client, 2);
  219. if (i2c_smbus_read_word_data(new_client, 4) != hyst
  220. || i2c_smbus_read_word_data(new_client, 5) != hyst
  221. || i2c_smbus_read_word_data(new_client, 6) != hyst
  222. || i2c_smbus_read_word_data(new_client, 7) != hyst)
  223. return -ENODEV;
  224. os = i2c_smbus_read_word_data(new_client, 3);
  225. if (i2c_smbus_read_word_data(new_client, 4) != os
  226. || i2c_smbus_read_word_data(new_client, 5) != os
  227. || i2c_smbus_read_word_data(new_client, 6) != os
  228. || i2c_smbus_read_word_data(new_client, 7) != os)
  229. return -ENODEV;
  230. /* Unused bits */
  231. if (conf & 0xe0)
  232. return -ENODEV;
  233. /* Addresses cycling */
  234. for (i = 8; i < 0xff; i += 8) {
  235. if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
  236. || i2c_smbus_read_word_data(new_client, i + 2) != hyst
  237. || i2c_smbus_read_word_data(new_client, i + 3) != os)
  238. return -ENODEV;
  239. }
  240. strlcpy(info->type, "lm75", I2C_NAME_SIZE);
  241. return 0;
  242. }
  243. #ifdef CONFIG_PM
  244. static int lm75_suspend(struct device *dev)
  245. {
  246. int status;
  247. struct i2c_client *client = to_i2c_client(dev);
  248. status = lm75_read_value(client, LM75_REG_CONF);
  249. if (status < 0) {
  250. dev_dbg(&client->dev, "Can't read config? %d\n", status);
  251. return status;
  252. }
  253. status = status | LM75_SHUTDOWN;
  254. lm75_write_value(client, LM75_REG_CONF, status);
  255. return 0;
  256. }
  257. static int lm75_resume(struct device *dev)
  258. {
  259. int status;
  260. struct i2c_client *client = to_i2c_client(dev);
  261. status = lm75_read_value(client, LM75_REG_CONF);
  262. if (status < 0) {
  263. dev_dbg(&client->dev, "Can't read config? %d\n", status);
  264. return status;
  265. }
  266. status = status & ~LM75_SHUTDOWN;
  267. lm75_write_value(client, LM75_REG_CONF, status);
  268. return 0;
  269. }
  270. static const struct dev_pm_ops lm75_dev_pm_ops = {
  271. .suspend = lm75_suspend,
  272. .resume = lm75_resume,
  273. };
  274. #define LM75_DEV_PM_OPS (&lm75_dev_pm_ops)
  275. #else
  276. #define LM75_DEV_PM_OPS NULL
  277. #endif /* CONFIG_PM */
  278. static struct i2c_driver lm75_driver = {
  279. .class = I2C_CLASS_HWMON,
  280. .driver = {
  281. .name = "lm75",
  282. .pm = LM75_DEV_PM_OPS,
  283. },
  284. .probe = lm75_probe,
  285. .remove = lm75_remove,
  286. .id_table = lm75_ids,
  287. .detect = lm75_detect,
  288. .address_list = normal_i2c,
  289. };
  290. /*-----------------------------------------------------------------------*/
  291. /* register access */
  292. /*
  293. * All registers are word-sized, except for the configuration register.
  294. * LM75 uses a high-byte first convention, which is exactly opposite to
  295. * the SMBus standard.
  296. */
  297. static int lm75_read_value(struct i2c_client *client, u8 reg)
  298. {
  299. int value;
  300. if (reg == LM75_REG_CONF)
  301. return i2c_smbus_read_byte_data(client, reg);
  302. value = i2c_smbus_read_word_data(client, reg);
  303. return (value < 0) ? value : swab16(value);
  304. }
  305. static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
  306. {
  307. if (reg == LM75_REG_CONF)
  308. return i2c_smbus_write_byte_data(client, reg, value);
  309. else
  310. return i2c_smbus_write_word_data(client, reg, swab16(value));
  311. }
  312. static struct lm75_data *lm75_update_device(struct device *dev)
  313. {
  314. struct i2c_client *client = to_i2c_client(dev);
  315. struct lm75_data *data = i2c_get_clientdata(client);
  316. mutex_lock(&data->update_lock);
  317. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  318. || !data->valid) {
  319. int i;
  320. dev_dbg(&client->dev, "Starting lm75 update\n");
  321. for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
  322. int status;
  323. status = lm75_read_value(client, LM75_REG_TEMP[i]);
  324. if (status < 0)
  325. dev_dbg(&client->dev, "reg %d, err %d\n",
  326. LM75_REG_TEMP[i], status);
  327. else
  328. data->temp[i] = status;
  329. }
  330. data->last_updated = jiffies;
  331. data->valid = 1;
  332. }
  333. mutex_unlock(&data->update_lock);
  334. return data;
  335. }
  336. /*-----------------------------------------------------------------------*/
  337. /* module glue */
  338. static int __init sensors_lm75_init(void)
  339. {
  340. return i2c_add_driver(&lm75_driver);
  341. }
  342. static void __exit sensors_lm75_exit(void)
  343. {
  344. i2c_del_driver(&lm75_driver);
  345. }
  346. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
  347. MODULE_DESCRIPTION("LM75 driver");
  348. MODULE_LICENSE("GPL");
  349. module_init(sensors_lm75_init);
  350. module_exit(sensors_lm75_exit);