lm75.c 14 KB

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