lm75.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 by legacy style driver binding */
  50. static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
  51. 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
  52. /* Insmod parameters (only for legacy style driver binding) */
  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 i2c_client *client;
  64. struct device *hwmon_dev;
  65. struct mutex update_lock;
  66. u8 orig_conf;
  67. char valid; /* !=0 if registers are valid */
  68. unsigned long last_updated; /* In jiffies */
  69. u16 temp[3]; /* Register values,
  70. 0 = input
  71. 1 = max
  72. 2 = hyst */
  73. };
  74. static int lm75_read_value(struct i2c_client *client, u8 reg);
  75. static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
  76. static struct lm75_data *lm75_update_device(struct device *dev);
  77. /*-----------------------------------------------------------------------*/
  78. /* sysfs attributes for hwmon */
  79. static ssize_t show_temp(struct device *dev, struct device_attribute *da,
  80. char *buf)
  81. {
  82. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  83. struct lm75_data *data = lm75_update_device(dev);
  84. return sprintf(buf, "%d\n",
  85. LM75_TEMP_FROM_REG(data->temp[attr->index]));
  86. }
  87. static ssize_t set_temp(struct device *dev, struct device_attribute *da,
  88. const char *buf, size_t count)
  89. {
  90. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  91. struct i2c_client *client = to_i2c_client(dev);
  92. struct lm75_data *data = i2c_get_clientdata(client);
  93. int nr = attr->index;
  94. long temp = simple_strtol(buf, NULL, 10);
  95. mutex_lock(&data->update_lock);
  96. data->temp[nr] = LM75_TEMP_TO_REG(temp);
  97. lm75_write_value(client, LM75_REG_TEMP[nr], data->temp[nr]);
  98. mutex_unlock(&data->update_lock);
  99. return count;
  100. }
  101. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  102. show_temp, set_temp, 1);
  103. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
  104. show_temp, set_temp, 2);
  105. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  106. static struct attribute *lm75_attributes[] = {
  107. &sensor_dev_attr_temp1_input.dev_attr.attr,
  108. &sensor_dev_attr_temp1_max.dev_attr.attr,
  109. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  110. NULL
  111. };
  112. static const struct attribute_group lm75_group = {
  113. .attrs = lm75_attributes,
  114. };
  115. /*-----------------------------------------------------------------------*/
  116. /* "New style" I2C driver binding -- following the driver model */
  117. static int
  118. lm75_probe(struct i2c_client *client, const struct i2c_device_id *id)
  119. {
  120. struct lm75_data *data;
  121. int status;
  122. u8 set_mask, clr_mask;
  123. int new;
  124. if (!i2c_check_functionality(client->adapter,
  125. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
  126. return -EIO;
  127. data = kzalloc(sizeof(struct lm75_data), GFP_KERNEL);
  128. if (!data)
  129. return -ENOMEM;
  130. i2c_set_clientdata(client, data);
  131. data->client = client;
  132. mutex_init(&data->update_lock);
  133. /* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
  134. * Then tweak to be more precise when appropriate.
  135. */
  136. set_mask = 0;
  137. clr_mask = (1 << 0) /* continuous conversions */
  138. | (1 << 6) | (1 << 5); /* 9-bit mode */
  139. /* configure as specified */
  140. status = lm75_read_value(client, LM75_REG_CONF);
  141. if (status < 0) {
  142. dev_dbg(&client->dev, "Can't read config? %d\n", status);
  143. goto exit_free;
  144. }
  145. data->orig_conf = status;
  146. new = status & ~clr_mask;
  147. new |= set_mask;
  148. if (status != new)
  149. lm75_write_value(client, LM75_REG_CONF, new);
  150. dev_dbg(&client->dev, "Config %02x\n", new);
  151. /* Register sysfs hooks */
  152. status = sysfs_create_group(&client->dev.kobj, &lm75_group);
  153. if (status)
  154. goto exit_free;
  155. data->hwmon_dev = hwmon_device_register(&client->dev);
  156. if (IS_ERR(data->hwmon_dev)) {
  157. status = PTR_ERR(data->hwmon_dev);
  158. goto exit_remove;
  159. }
  160. dev_info(&client->dev, "%s: sensor '%s'\n",
  161. data->hwmon_dev->bus_id, client->name);
  162. return 0;
  163. exit_remove:
  164. sysfs_remove_group(&client->dev.kobj, &lm75_group);
  165. exit_free:
  166. i2c_set_clientdata(client, NULL);
  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. i2c_set_clientdata(client, NULL);
  177. kfree(data);
  178. return 0;
  179. }
  180. static const struct i2c_device_id lm75_ids[] = {
  181. { "ds1775", ds1775, },
  182. { "ds75", ds75, },
  183. { "lm75", lm75, },
  184. { "lm75a", lm75a, },
  185. { "max6625", max6625, },
  186. { "max6626", max6626, },
  187. { "mcp980x", mcp980x, },
  188. { "stds75", stds75, },
  189. { "tcn75", tcn75, },
  190. { "tmp100", tmp100, },
  191. { "tmp101", tmp101, },
  192. { "tmp175", tmp175, },
  193. { "tmp275", tmp275, },
  194. { "tmp75", tmp75, },
  195. { /* LIST END */ }
  196. };
  197. MODULE_DEVICE_TABLE(i2c, lm75_ids);
  198. static struct i2c_driver lm75_driver = {
  199. .driver = {
  200. .name = "lm75",
  201. },
  202. .probe = lm75_probe,
  203. .remove = lm75_remove,
  204. .id_table = lm75_ids,
  205. };
  206. /*-----------------------------------------------------------------------*/
  207. /* "Legacy" I2C driver binding */
  208. static struct i2c_driver lm75_legacy_driver;
  209. /* This function is called by i2c_probe */
  210. static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
  211. {
  212. int i;
  213. struct i2c_client *new_client;
  214. int err = 0;
  215. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  216. I2C_FUNC_SMBUS_WORD_DATA))
  217. goto exit;
  218. /* OK. For now, we presume we have a valid address. We create the
  219. client structure, even though there may be no sensor present.
  220. But it allows us to use i2c_smbus_read_*_data() calls. */
  221. new_client = kzalloc(sizeof *new_client, GFP_KERNEL);
  222. if (!new_client) {
  223. err = -ENOMEM;
  224. goto exit;
  225. }
  226. new_client->addr = address;
  227. new_client->adapter = adapter;
  228. new_client->driver = &lm75_legacy_driver;
  229. new_client->flags = 0;
  230. /* Now, we do the remaining detection. There is no identification-
  231. dedicated register so we have to rely on several tricks:
  232. unused bits, registers cycling over 8-address boundaries,
  233. addresses 0x04-0x07 returning the last read value.
  234. The cycling+unused addresses combination is not tested,
  235. since it would significantly slow the detection down and would
  236. hardly add any value. */
  237. if (kind < 0) {
  238. int cur, conf, hyst, os;
  239. /* Unused addresses */
  240. cur = i2c_smbus_read_word_data(new_client, 0);
  241. conf = i2c_smbus_read_byte_data(new_client, 1);
  242. hyst = i2c_smbus_read_word_data(new_client, 2);
  243. if (i2c_smbus_read_word_data(new_client, 4) != hyst
  244. || i2c_smbus_read_word_data(new_client, 5) != hyst
  245. || i2c_smbus_read_word_data(new_client, 6) != hyst
  246. || i2c_smbus_read_word_data(new_client, 7) != hyst)
  247. goto exit_free;
  248. os = i2c_smbus_read_word_data(new_client, 3);
  249. if (i2c_smbus_read_word_data(new_client, 4) != os
  250. || i2c_smbus_read_word_data(new_client, 5) != os
  251. || i2c_smbus_read_word_data(new_client, 6) != os
  252. || i2c_smbus_read_word_data(new_client, 7) != os)
  253. goto exit_free;
  254. /* Unused bits */
  255. if (conf & 0xe0)
  256. goto exit_free;
  257. /* Addresses cycling */
  258. for (i = 8; i < 0xff; i += 8)
  259. if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
  260. || i2c_smbus_read_word_data(new_client, i + 2) != hyst
  261. || i2c_smbus_read_word_data(new_client, i + 3) != os)
  262. goto exit_free;
  263. }
  264. /* NOTE: we treat "force=..." and "force_lm75=..." the same.
  265. * Only new-style driver binding distinguishes chip types.
  266. */
  267. strlcpy(new_client->name, "lm75", I2C_NAME_SIZE);
  268. /* Tell the I2C layer a new client has arrived */
  269. err = i2c_attach_client(new_client);
  270. if (err)
  271. goto exit_free;
  272. err = lm75_probe(new_client, NULL);
  273. if (err < 0)
  274. goto exit_detach;
  275. return 0;
  276. exit_detach:
  277. i2c_detach_client(new_client);
  278. exit_free:
  279. kfree(new_client);
  280. exit:
  281. return err;
  282. }
  283. static int lm75_attach_adapter(struct i2c_adapter *adapter)
  284. {
  285. if (!(adapter->class & I2C_CLASS_HWMON))
  286. return 0;
  287. return i2c_probe(adapter, &addr_data, lm75_detect);
  288. }
  289. static int lm75_detach_client(struct i2c_client *client)
  290. {
  291. lm75_remove(client);
  292. i2c_detach_client(client);
  293. kfree(client);
  294. return 0;
  295. }
  296. static struct i2c_driver lm75_legacy_driver = {
  297. .driver = {
  298. .name = "lm75_legacy",
  299. },
  300. .attach_adapter = lm75_attach_adapter,
  301. .detach_client = lm75_detach_client,
  302. };
  303. /*-----------------------------------------------------------------------*/
  304. /* register access */
  305. /* All registers are word-sized, except for the configuration register.
  306. LM75 uses a high-byte first convention, which is exactly opposite to
  307. the SMBus standard. */
  308. static int lm75_read_value(struct i2c_client *client, u8 reg)
  309. {
  310. int value;
  311. if (reg == LM75_REG_CONF)
  312. return i2c_smbus_read_byte_data(client, reg);
  313. value = i2c_smbus_read_word_data(client, reg);
  314. return (value < 0) ? value : swab16(value);
  315. }
  316. static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
  317. {
  318. if (reg == LM75_REG_CONF)
  319. return i2c_smbus_write_byte_data(client, reg, value);
  320. else
  321. return i2c_smbus_write_word_data(client, reg, swab16(value));
  322. }
  323. static struct lm75_data *lm75_update_device(struct device *dev)
  324. {
  325. struct i2c_client *client = to_i2c_client(dev);
  326. struct lm75_data *data = i2c_get_clientdata(client);
  327. mutex_lock(&data->update_lock);
  328. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  329. || !data->valid) {
  330. int i;
  331. dev_dbg(&client->dev, "Starting lm75 update\n");
  332. for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
  333. int status;
  334. status = lm75_read_value(client, LM75_REG_TEMP[i]);
  335. if (status < 0)
  336. dev_dbg(&client->dev, "reg %d, err %d\n",
  337. LM75_REG_TEMP[i], status);
  338. else
  339. data->temp[i] = status;
  340. }
  341. data->last_updated = jiffies;
  342. data->valid = 1;
  343. }
  344. mutex_unlock(&data->update_lock);
  345. return data;
  346. }
  347. /*-----------------------------------------------------------------------*/
  348. /* module glue */
  349. static int __init sensors_lm75_init(void)
  350. {
  351. int status;
  352. status = i2c_add_driver(&lm75_driver);
  353. if (status < 0)
  354. return status;
  355. status = i2c_add_driver(&lm75_legacy_driver);
  356. if (status < 0)
  357. i2c_del_driver(&lm75_driver);
  358. return status;
  359. }
  360. static void __exit sensors_lm75_exit(void)
  361. {
  362. i2c_del_driver(&lm75_legacy_driver);
  363. i2c_del_driver(&lm75_driver);
  364. }
  365. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
  366. MODULE_DESCRIPTION("LM75 driver");
  367. MODULE_LICENSE("GPL");
  368. module_init(sensors_lm75_init);
  369. module_exit(sensors_lm75_exit);