lm75.c 13 KB

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