lm75.c 16 KB

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