lm75.c 14 KB

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