lm75.c 14 KB

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