lm75.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. adt75,
  35. ds1775,
  36. ds75,
  37. lm75,
  38. lm75a,
  39. max6625,
  40. max6626,
  41. mcp980x,
  42. stds75,
  43. tcn75,
  44. tmp100,
  45. tmp101,
  46. tmp105,
  47. tmp175,
  48. tmp275,
  49. tmp75,
  50. };
  51. /* Addresses scanned */
  52. static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
  53. 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
  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. if (IS_ERR(data))
  84. return PTR_ERR(data);
  85. return sprintf(buf, "%d\n",
  86. LM75_TEMP_FROM_REG(data->temp[attr->index]));
  87. }
  88. static ssize_t set_temp(struct device *dev, struct device_attribute *da,
  89. const char *buf, size_t count)
  90. {
  91. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  92. struct i2c_client *client = to_i2c_client(dev);
  93. struct lm75_data *data = i2c_get_clientdata(client);
  94. int nr = attr->index;
  95. long temp;
  96. int error;
  97. error = kstrtol(buf, 10, &temp);
  98. if (error)
  99. return error;
  100. mutex_lock(&data->update_lock);
  101. data->temp[nr] = LM75_TEMP_TO_REG(temp);
  102. lm75_write_value(client, LM75_REG_TEMP[nr], data->temp[nr]);
  103. mutex_unlock(&data->update_lock);
  104. return count;
  105. }
  106. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  107. show_temp, set_temp, 1);
  108. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
  109. show_temp, set_temp, 2);
  110. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  111. static struct attribute *lm75_attributes[] = {
  112. &sensor_dev_attr_temp1_input.dev_attr.attr,
  113. &sensor_dev_attr_temp1_max.dev_attr.attr,
  114. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  115. NULL
  116. };
  117. static const struct attribute_group lm75_group = {
  118. .attrs = lm75_attributes,
  119. };
  120. /*-----------------------------------------------------------------------*/
  121. /* device probe and removal */
  122. static int
  123. lm75_probe(struct i2c_client *client, const struct i2c_device_id *id)
  124. {
  125. struct lm75_data *data;
  126. int status;
  127. u8 set_mask, clr_mask;
  128. int new;
  129. if (!i2c_check_functionality(client->adapter,
  130. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
  131. return -EIO;
  132. data = devm_kzalloc(&client->dev, sizeof(struct lm75_data), GFP_KERNEL);
  133. if (!data)
  134. return -ENOMEM;
  135. i2c_set_clientdata(client, data);
  136. mutex_init(&data->update_lock);
  137. /* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
  138. * Then tweak to be more precise when appropriate.
  139. */
  140. set_mask = 0;
  141. clr_mask = LM75_SHUTDOWN; /* continuous conversions */
  142. switch (id->driver_data) {
  143. case adt75:
  144. clr_mask |= 1 << 5; /* not one-shot mode */
  145. break;
  146. case ds1775:
  147. case ds75:
  148. case stds75:
  149. clr_mask |= 3 << 5; /* 9-bit mode */
  150. break;
  151. case mcp980x:
  152. case tmp100:
  153. case tmp101:
  154. case tmp105:
  155. case tmp175:
  156. case tmp275:
  157. case tmp75:
  158. clr_mask |= 3 << 5; /* 9-bit mode */
  159. clr_mask |= 1 << 7; /* not one-shot mode */
  160. break;
  161. }
  162. /* configure as specified */
  163. status = lm75_read_value(client, LM75_REG_CONF);
  164. if (status < 0) {
  165. dev_dbg(&client->dev, "Can't read config? %d\n", status);
  166. return status;
  167. }
  168. data->orig_conf = status;
  169. new = status & ~clr_mask;
  170. new |= set_mask;
  171. if (status != new)
  172. lm75_write_value(client, LM75_REG_CONF, new);
  173. dev_dbg(&client->dev, "Config %02x\n", new);
  174. /* Register sysfs hooks */
  175. status = sysfs_create_group(&client->dev.kobj, &lm75_group);
  176. if (status)
  177. return status;
  178. data->hwmon_dev = hwmon_device_register(&client->dev);
  179. if (IS_ERR(data->hwmon_dev)) {
  180. status = PTR_ERR(data->hwmon_dev);
  181. goto exit_remove;
  182. }
  183. dev_info(&client->dev, "%s: sensor '%s'\n",
  184. dev_name(data->hwmon_dev), client->name);
  185. return 0;
  186. exit_remove:
  187. sysfs_remove_group(&client->dev.kobj, &lm75_group);
  188. return status;
  189. }
  190. static int lm75_remove(struct i2c_client *client)
  191. {
  192. struct lm75_data *data = i2c_get_clientdata(client);
  193. hwmon_device_unregister(data->hwmon_dev);
  194. sysfs_remove_group(&client->dev.kobj, &lm75_group);
  195. lm75_write_value(client, LM75_REG_CONF, data->orig_conf);
  196. return 0;
  197. }
  198. static const struct i2c_device_id lm75_ids[] = {
  199. { "adt75", adt75, },
  200. { "ds1775", ds1775, },
  201. { "ds75", ds75, },
  202. { "lm75", lm75, },
  203. { "lm75a", lm75a, },
  204. { "max6625", max6625, },
  205. { "max6626", max6626, },
  206. { "mcp980x", mcp980x, },
  207. { "stds75", stds75, },
  208. { "tcn75", tcn75, },
  209. { "tmp100", tmp100, },
  210. { "tmp101", tmp101, },
  211. { "tmp105", tmp105, },
  212. { "tmp175", tmp175, },
  213. { "tmp275", tmp275, },
  214. { "tmp75", tmp75, },
  215. { /* LIST END */ }
  216. };
  217. MODULE_DEVICE_TABLE(i2c, lm75_ids);
  218. #define LM75A_ID 0xA1
  219. /* Return 0 if detection is successful, -ENODEV otherwise */
  220. static int lm75_detect(struct i2c_client *new_client,
  221. struct i2c_board_info *info)
  222. {
  223. struct i2c_adapter *adapter = new_client->adapter;
  224. int i;
  225. int conf, hyst, os;
  226. bool is_lm75a = 0;
  227. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  228. I2C_FUNC_SMBUS_WORD_DATA))
  229. return -ENODEV;
  230. /*
  231. * Now, we do the remaining detection. There is no identification-
  232. * dedicated register so we have to rely on several tricks:
  233. * unused bits, registers cycling over 8-address boundaries,
  234. * addresses 0x04-0x07 returning the last read value.
  235. * The cycling+unused addresses combination is not tested,
  236. * since it would significantly slow the detection down and would
  237. * hardly add any value.
  238. *
  239. * The National Semiconductor LM75A is different than earlier
  240. * LM75s. It has an ID byte of 0xaX (where X is the chip
  241. * revision, with 1 being the only revision in existence) in
  242. * register 7, and unused registers return 0xff rather than the
  243. * last read value.
  244. *
  245. * Note that this function only detects the original National
  246. * Semiconductor LM75 and the LM75A. Clones from other vendors
  247. * aren't detected, on purpose, because they are typically never
  248. * found on PC hardware. They are found on embedded designs where
  249. * they can be instantiated explicitly so detection is not needed.
  250. * The absence of identification registers on all these clones
  251. * would make their exhaustive detection very difficult and weak,
  252. * and odds are that the driver would bind to unsupported devices.
  253. */
  254. /* Unused bits */
  255. conf = i2c_smbus_read_byte_data(new_client, 1);
  256. if (conf & 0xe0)
  257. return -ENODEV;
  258. /* First check for LM75A */
  259. if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) {
  260. /* LM75A returns 0xff on unused registers so
  261. just to be sure we check for that too. */
  262. if (i2c_smbus_read_byte_data(new_client, 4) != 0xff
  263. || i2c_smbus_read_byte_data(new_client, 5) != 0xff
  264. || i2c_smbus_read_byte_data(new_client, 6) != 0xff)
  265. return -ENODEV;
  266. is_lm75a = 1;
  267. hyst = i2c_smbus_read_byte_data(new_client, 2);
  268. os = i2c_smbus_read_byte_data(new_client, 3);
  269. } else { /* Traditional style LM75 detection */
  270. /* Unused addresses */
  271. hyst = i2c_smbus_read_byte_data(new_client, 2);
  272. if (i2c_smbus_read_byte_data(new_client, 4) != hyst
  273. || i2c_smbus_read_byte_data(new_client, 5) != hyst
  274. || i2c_smbus_read_byte_data(new_client, 6) != hyst
  275. || i2c_smbus_read_byte_data(new_client, 7) != hyst)
  276. return -ENODEV;
  277. os = i2c_smbus_read_byte_data(new_client, 3);
  278. if (i2c_smbus_read_byte_data(new_client, 4) != os
  279. || i2c_smbus_read_byte_data(new_client, 5) != os
  280. || i2c_smbus_read_byte_data(new_client, 6) != os
  281. || i2c_smbus_read_byte_data(new_client, 7) != os)
  282. return -ENODEV;
  283. }
  284. /* Addresses cycling */
  285. for (i = 8; i <= 248; i += 40) {
  286. if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
  287. || i2c_smbus_read_byte_data(new_client, i + 2) != hyst
  288. || i2c_smbus_read_byte_data(new_client, i + 3) != os)
  289. return -ENODEV;
  290. if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7)
  291. != LM75A_ID)
  292. return -ENODEV;
  293. }
  294. strlcpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE);
  295. return 0;
  296. }
  297. #ifdef CONFIG_PM
  298. static int lm75_suspend(struct device *dev)
  299. {
  300. int status;
  301. struct i2c_client *client = to_i2c_client(dev);
  302. status = lm75_read_value(client, LM75_REG_CONF);
  303. if (status < 0) {
  304. dev_dbg(&client->dev, "Can't read config? %d\n", status);
  305. return status;
  306. }
  307. status = status | LM75_SHUTDOWN;
  308. lm75_write_value(client, LM75_REG_CONF, status);
  309. return 0;
  310. }
  311. static int lm75_resume(struct device *dev)
  312. {
  313. int status;
  314. struct i2c_client *client = to_i2c_client(dev);
  315. status = lm75_read_value(client, LM75_REG_CONF);
  316. if (status < 0) {
  317. dev_dbg(&client->dev, "Can't read config? %d\n", status);
  318. return status;
  319. }
  320. status = status & ~LM75_SHUTDOWN;
  321. lm75_write_value(client, LM75_REG_CONF, status);
  322. return 0;
  323. }
  324. static const struct dev_pm_ops lm75_dev_pm_ops = {
  325. .suspend = lm75_suspend,
  326. .resume = lm75_resume,
  327. };
  328. #define LM75_DEV_PM_OPS (&lm75_dev_pm_ops)
  329. #else
  330. #define LM75_DEV_PM_OPS NULL
  331. #endif /* CONFIG_PM */
  332. static struct i2c_driver lm75_driver = {
  333. .class = I2C_CLASS_HWMON,
  334. .driver = {
  335. .name = "lm75",
  336. .pm = LM75_DEV_PM_OPS,
  337. },
  338. .probe = lm75_probe,
  339. .remove = lm75_remove,
  340. .id_table = lm75_ids,
  341. .detect = lm75_detect,
  342. .address_list = normal_i2c,
  343. };
  344. /*-----------------------------------------------------------------------*/
  345. /* register access */
  346. /*
  347. * All registers are word-sized, except for the configuration register.
  348. * LM75 uses a high-byte first convention, which is exactly opposite to
  349. * the SMBus standard.
  350. */
  351. static int lm75_read_value(struct i2c_client *client, u8 reg)
  352. {
  353. if (reg == LM75_REG_CONF)
  354. return i2c_smbus_read_byte_data(client, reg);
  355. else
  356. return i2c_smbus_read_word_swapped(client, reg);
  357. }
  358. static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
  359. {
  360. if (reg == LM75_REG_CONF)
  361. return i2c_smbus_write_byte_data(client, reg, value);
  362. else
  363. return i2c_smbus_write_word_swapped(client, reg, value);
  364. }
  365. static struct lm75_data *lm75_update_device(struct device *dev)
  366. {
  367. struct i2c_client *client = to_i2c_client(dev);
  368. struct lm75_data *data = i2c_get_clientdata(client);
  369. struct lm75_data *ret = data;
  370. mutex_lock(&data->update_lock);
  371. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  372. || !data->valid) {
  373. int i;
  374. dev_dbg(&client->dev, "Starting lm75 update\n");
  375. for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
  376. int status;
  377. status = lm75_read_value(client, LM75_REG_TEMP[i]);
  378. if (unlikely(status < 0)) {
  379. dev_dbg(dev,
  380. "LM75: Failed to read value: reg %d, error %d\n",
  381. LM75_REG_TEMP[i], status);
  382. ret = ERR_PTR(status);
  383. data->valid = 0;
  384. goto abort;
  385. }
  386. data->temp[i] = status;
  387. }
  388. data->last_updated = jiffies;
  389. data->valid = 1;
  390. }
  391. abort:
  392. mutex_unlock(&data->update_lock);
  393. return ret;
  394. }
  395. module_i2c_driver(lm75_driver);
  396. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
  397. MODULE_DESCRIPTION("LM75 driver");
  398. MODULE_LICENSE("GPL");