lm75.c 14 KB

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