lm75.c 15 KB

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