lm75.c 15 KB

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