lm75.c 14 KB

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