lm75.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. tmp112,
  52. tmp175,
  53. tmp275,
  54. tmp75,
  55. };
  56. /* Addresses scanned */
  57. static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
  58. 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
  59. /* The LM75 registers */
  60. #define LM75_REG_CONF 0x01
  61. static const u8 LM75_REG_TEMP[3] = {
  62. 0x00, /* input */
  63. 0x03, /* max */
  64. 0x02, /* hyst */
  65. };
  66. /* Each client has this additional data */
  67. struct lm75_data {
  68. struct i2c_client *client;
  69. struct device *hwmon_dev;
  70. struct thermal_zone_device *tz;
  71. struct mutex update_lock;
  72. u8 orig_conf;
  73. u8 resolution; /* In bits, between 9 and 12 */
  74. u8 resolution_limits;
  75. char valid; /* !=0 if registers are valid */
  76. unsigned long last_updated; /* In jiffies */
  77. unsigned long sample_time; /* In jiffies */
  78. s16 temp[3]; /* Register values,
  79. 0 = input
  80. 1 = max
  81. 2 = hyst */
  82. };
  83. static int lm75_read_value(struct i2c_client *client, u8 reg);
  84. static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
  85. static struct lm75_data *lm75_update_device(struct device *dev);
  86. /*-----------------------------------------------------------------------*/
  87. static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
  88. {
  89. return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
  90. }
  91. /* sysfs attributes for hwmon */
  92. static int lm75_read_temp(void *dev, long *temp)
  93. {
  94. struct lm75_data *data = lm75_update_device(dev);
  95. if (IS_ERR(data))
  96. return PTR_ERR(data);
  97. *temp = lm75_reg_to_mc(data->temp[0], data->resolution);
  98. return 0;
  99. }
  100. static ssize_t show_temp(struct device *dev, struct device_attribute *da,
  101. char *buf)
  102. {
  103. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  104. struct lm75_data *data = lm75_update_device(dev);
  105. if (IS_ERR(data))
  106. return PTR_ERR(data);
  107. return sprintf(buf, "%ld\n", lm75_reg_to_mc(data->temp[attr->index],
  108. data->resolution));
  109. }
  110. static ssize_t set_temp(struct device *dev, struct device_attribute *da,
  111. const char *buf, size_t count)
  112. {
  113. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  114. struct lm75_data *data = dev_get_drvdata(dev);
  115. struct i2c_client *client = data->client;
  116. int nr = attr->index;
  117. long temp;
  118. int error;
  119. u8 resolution;
  120. error = kstrtol(buf, 10, &temp);
  121. if (error)
  122. return error;
  123. /*
  124. * Resolution of limit registers is assumed to be the same as the
  125. * temperature input register resolution unless given explicitly.
  126. */
  127. if (attr->index && data->resolution_limits)
  128. resolution = data->resolution_limits;
  129. else
  130. resolution = data->resolution;
  131. mutex_lock(&data->update_lock);
  132. temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
  133. data->temp[nr] = DIV_ROUND_CLOSEST(temp << (resolution - 8),
  134. 1000) << (16 - resolution);
  135. lm75_write_value(client, LM75_REG_TEMP[nr], data->temp[nr]);
  136. mutex_unlock(&data->update_lock);
  137. return count;
  138. }
  139. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  140. show_temp, set_temp, 1);
  141. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
  142. show_temp, set_temp, 2);
  143. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  144. static struct attribute *lm75_attrs[] = {
  145. &sensor_dev_attr_temp1_input.dev_attr.attr,
  146. &sensor_dev_attr_temp1_max.dev_attr.attr,
  147. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  148. NULL
  149. };
  150. ATTRIBUTE_GROUPS(lm75);
  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 device *dev = &client->dev;
  157. struct lm75_data *data;
  158. int status;
  159. u8 set_mask, clr_mask;
  160. int new;
  161. enum lm75_type kind = id->driver_data;
  162. if (!i2c_check_functionality(client->adapter,
  163. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
  164. return -EIO;
  165. data = devm_kzalloc(dev, sizeof(struct lm75_data), GFP_KERNEL);
  166. if (!data)
  167. return -ENOMEM;
  168. data->client = client;
  169. i2c_set_clientdata(client, data);
  170. mutex_init(&data->update_lock);
  171. /* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
  172. * Then tweak to be more precise when appropriate.
  173. */
  174. set_mask = 0;
  175. clr_mask = LM75_SHUTDOWN; /* continuous conversions */
  176. switch (kind) {
  177. case adt75:
  178. clr_mask |= 1 << 5; /* not one-shot mode */
  179. data->resolution = 12;
  180. data->sample_time = HZ / 8;
  181. break;
  182. case ds1775:
  183. case ds75:
  184. case stds75:
  185. clr_mask |= 3 << 5;
  186. set_mask |= 2 << 5; /* 11-bit mode */
  187. data->resolution = 11;
  188. data->sample_time = HZ;
  189. break;
  190. case ds7505:
  191. set_mask |= 3 << 5; /* 12-bit mode */
  192. data->resolution = 12;
  193. data->sample_time = HZ / 4;
  194. break;
  195. case g751:
  196. case lm75:
  197. case lm75a:
  198. data->resolution = 9;
  199. data->sample_time = HZ / 2;
  200. break;
  201. case max6625:
  202. data->resolution = 9;
  203. data->sample_time = HZ / 4;
  204. break;
  205. case max6626:
  206. data->resolution = 12;
  207. data->resolution_limits = 9;
  208. data->sample_time = HZ / 4;
  209. break;
  210. case tcn75:
  211. data->resolution = 9;
  212. data->sample_time = HZ / 8;
  213. break;
  214. case mcp980x:
  215. data->resolution_limits = 9;
  216. /* fall through */
  217. case tmp100:
  218. case tmp101:
  219. set_mask |= 3 << 5; /* 12-bit mode */
  220. data->resolution = 12;
  221. data->sample_time = HZ;
  222. clr_mask |= 1 << 7; /* not one-shot mode */
  223. break;
  224. case tmp112:
  225. set_mask |= 3 << 5; /* 12-bit mode */
  226. clr_mask |= 1 << 7; /* not one-shot mode */
  227. data->resolution = 12;
  228. data->sample_time = HZ / 4;
  229. break;
  230. case tmp105:
  231. case tmp175:
  232. case tmp275:
  233. case tmp75:
  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 / 2;
  238. break;
  239. }
  240. /* configure as specified */
  241. status = lm75_read_value(client, LM75_REG_CONF);
  242. if (status < 0) {
  243. dev_dbg(dev, "Can't read config? %d\n", status);
  244. return status;
  245. }
  246. data->orig_conf = status;
  247. new = status & ~clr_mask;
  248. new |= set_mask;
  249. if (status != new)
  250. lm75_write_value(client, LM75_REG_CONF, new);
  251. dev_dbg(dev, "Config %02x\n", new);
  252. data->hwmon_dev = hwmon_device_register_with_groups(dev, client->name,
  253. data, lm75_groups);
  254. if (IS_ERR(data->hwmon_dev))
  255. return PTR_ERR(data->hwmon_dev);
  256. data->tz = thermal_zone_of_sensor_register(data->hwmon_dev,
  257. 0,
  258. data->hwmon_dev,
  259. lm75_read_temp, NULL);
  260. if (IS_ERR(data->tz))
  261. data->tz = NULL;
  262. dev_info(dev, "%s: sensor '%s'\n",
  263. dev_name(data->hwmon_dev), client->name);
  264. return 0;
  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(data->hwmon_dev, data->tz);
  270. hwmon_device_unregister(data->hwmon_dev);
  271. lm75_write_value(client, LM75_REG_CONF, data->orig_conf);
  272. return 0;
  273. }
  274. static const struct i2c_device_id lm75_ids[] = {
  275. { "adt75", adt75, },
  276. { "ds1775", ds1775, },
  277. { "ds75", ds75, },
  278. { "ds7505", ds7505, },
  279. { "g751", g751, },
  280. { "lm75", lm75, },
  281. { "lm75a", lm75a, },
  282. { "max6625", max6625, },
  283. { "max6626", max6626, },
  284. { "mcp980x", mcp980x, },
  285. { "stds75", stds75, },
  286. { "tcn75", tcn75, },
  287. { "tmp100", tmp100, },
  288. { "tmp101", tmp101, },
  289. { "tmp105", tmp105, },
  290. { "tmp112", tmp112, },
  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 lm75_data *data = dev_get_drvdata(dev);
  447. struct i2c_client *client = data->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");