lm75.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * lm75.c - Part of lm_sensors, Linux kernel modules for hardware
  4. * monitoring
  5. * Copyright (c) 1998, 1999 Frodo Looijaard <[email protected]>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/jiffies.h>
  11. #include <linux/i2c.h>
  12. #include <linux/hwmon.h>
  13. #include <linux/hwmon-sysfs.h>
  14. #include <linux/err.h>
  15. #include <linux/of_device.h>
  16. #include <linux/of.h>
  17. #include <linux/regmap.h>
  18. #include <linux/util_macros.h>
  19. #include <linux/regulator/consumer.h>
  20. #include "lm75.h"
  21. /*
  22. * This driver handles the LM75 and compatible digital temperature sensors.
  23. */
  24. enum lm75_type { /* keep sorted in alphabetical order */
  25. adt75,
  26. at30ts74,
  27. ds1775,
  28. ds75,
  29. ds7505,
  30. g751,
  31. lm75,
  32. lm75a,
  33. lm75b,
  34. max6625,
  35. max6626,
  36. max31725,
  37. mcp980x,
  38. pct2075,
  39. stds75,
  40. stlm75,
  41. tcn75,
  42. tmp100,
  43. tmp101,
  44. tmp105,
  45. tmp112,
  46. tmp175,
  47. tmp275,
  48. tmp75,
  49. tmp75b,
  50. tmp75c,
  51. tmp1075,
  52. };
  53. /**
  54. * struct lm75_params - lm75 configuration parameters.
  55. * @set_mask: Bits to set in configuration register when configuring
  56. * the chip.
  57. * @clr_mask: Bits to clear in configuration register when configuring
  58. * the chip.
  59. * @default_resolution: Default number of bits to represent the temperature
  60. * value.
  61. * @resolution_limits: Limit register resolution. Optional. Should be set if
  62. * the resolution of limit registers does not match the
  63. * resolution of the temperature register.
  64. * @resolutions: List of resolutions associated with sample times.
  65. * Optional. Should be set if num_sample_times is larger
  66. * than 1, and if the resolution changes with sample times.
  67. * If set, number of entries must match num_sample_times.
  68. * @default_sample_time:Sample time to be set by default.
  69. * @num_sample_times: Number of possible sample times to be set. Optional.
  70. * Should be set if the number of sample times is larger
  71. * than one.
  72. * @sample_times: All the possible sample times to be set. Mandatory if
  73. * num_sample_times is larger than 1. If set, number of
  74. * entries must match num_sample_times.
  75. */
  76. struct lm75_params {
  77. u8 set_mask;
  78. u8 clr_mask;
  79. u8 default_resolution;
  80. u8 resolution_limits;
  81. const u8 *resolutions;
  82. unsigned int default_sample_time;
  83. u8 num_sample_times;
  84. const unsigned int *sample_times;
  85. };
  86. /* Addresses scanned */
  87. static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
  88. 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
  89. /* The LM75 registers */
  90. #define LM75_REG_TEMP 0x00
  91. #define LM75_REG_CONF 0x01
  92. #define LM75_REG_HYST 0x02
  93. #define LM75_REG_MAX 0x03
  94. #define PCT2075_REG_IDLE 0x04
  95. /* Each client has this additional data */
  96. struct lm75_data {
  97. struct i2c_client *client;
  98. struct regmap *regmap;
  99. struct regulator *vs;
  100. u8 orig_conf;
  101. u8 current_conf;
  102. u8 resolution; /* In bits, 9 to 16 */
  103. unsigned int sample_time; /* In ms */
  104. enum lm75_type kind;
  105. const struct lm75_params *params;
  106. };
  107. /*-----------------------------------------------------------------------*/
  108. static const u8 lm75_sample_set_masks[] = { 0 << 5, 1 << 5, 2 << 5, 3 << 5 };
  109. #define LM75_SAMPLE_CLEAR_MASK (3 << 5)
  110. /* The structure below stores the configuration values of the supported devices.
  111. * In case of being supported multiple configurations, the default one must
  112. * always be the first element of the array
  113. */
  114. static const struct lm75_params device_params[] = {
  115. [adt75] = {
  116. .clr_mask = 1 << 5, /* not one-shot mode */
  117. .default_resolution = 12,
  118. .default_sample_time = MSEC_PER_SEC / 10,
  119. },
  120. [at30ts74] = {
  121. .set_mask = 3 << 5, /* 12-bit mode*/
  122. .default_resolution = 12,
  123. .default_sample_time = 200,
  124. .num_sample_times = 4,
  125. .sample_times = (unsigned int []){ 25, 50, 100, 200 },
  126. .resolutions = (u8 []) {9, 10, 11, 12 },
  127. },
  128. [ds1775] = {
  129. .clr_mask = 3 << 5,
  130. .set_mask = 2 << 5, /* 11-bit mode */
  131. .default_resolution = 11,
  132. .default_sample_time = 500,
  133. .num_sample_times = 4,
  134. .sample_times = (unsigned int []){ 125, 250, 500, 1000 },
  135. .resolutions = (u8 []) {9, 10, 11, 12 },
  136. },
  137. [ds75] = {
  138. .clr_mask = 3 << 5,
  139. .set_mask = 2 << 5, /* 11-bit mode */
  140. .default_resolution = 11,
  141. .default_sample_time = 600,
  142. .num_sample_times = 4,
  143. .sample_times = (unsigned int []){ 150, 300, 600, 1200 },
  144. .resolutions = (u8 []) {9, 10, 11, 12 },
  145. },
  146. [stds75] = {
  147. .clr_mask = 3 << 5,
  148. .set_mask = 2 << 5, /* 11-bit mode */
  149. .default_resolution = 11,
  150. .default_sample_time = 600,
  151. .num_sample_times = 4,
  152. .sample_times = (unsigned int []){ 150, 300, 600, 1200 },
  153. .resolutions = (u8 []) {9, 10, 11, 12 },
  154. },
  155. [stlm75] = {
  156. .default_resolution = 9,
  157. .default_sample_time = MSEC_PER_SEC / 6,
  158. },
  159. [ds7505] = {
  160. .set_mask = 3 << 5, /* 12-bit mode*/
  161. .default_resolution = 12,
  162. .default_sample_time = 200,
  163. .num_sample_times = 4,
  164. .sample_times = (unsigned int []){ 25, 50, 100, 200 },
  165. .resolutions = (u8 []) {9, 10, 11, 12 },
  166. },
  167. [g751] = {
  168. .default_resolution = 9,
  169. .default_sample_time = MSEC_PER_SEC / 10,
  170. },
  171. [lm75] = {
  172. .default_resolution = 9,
  173. .default_sample_time = MSEC_PER_SEC / 10,
  174. },
  175. [lm75a] = {
  176. .default_resolution = 9,
  177. .default_sample_time = MSEC_PER_SEC / 10,
  178. },
  179. [lm75b] = {
  180. .default_resolution = 11,
  181. .default_sample_time = MSEC_PER_SEC / 10,
  182. },
  183. [max6625] = {
  184. .default_resolution = 9,
  185. .default_sample_time = MSEC_PER_SEC / 7,
  186. },
  187. [max6626] = {
  188. .default_resolution = 12,
  189. .default_sample_time = MSEC_PER_SEC / 7,
  190. .resolution_limits = 9,
  191. },
  192. [max31725] = {
  193. .default_resolution = 16,
  194. .default_sample_time = MSEC_PER_SEC / 20,
  195. },
  196. [tcn75] = {
  197. .default_resolution = 9,
  198. .default_sample_time = MSEC_PER_SEC / 18,
  199. },
  200. [pct2075] = {
  201. .default_resolution = 11,
  202. .default_sample_time = MSEC_PER_SEC / 10,
  203. .num_sample_times = 31,
  204. .sample_times = (unsigned int []){ 100, 200, 300, 400, 500, 600,
  205. 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700,
  206. 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700,
  207. 2800, 2900, 3000, 3100 },
  208. },
  209. [mcp980x] = {
  210. .set_mask = 3 << 5, /* 12-bit mode */
  211. .clr_mask = 1 << 7, /* not one-shot mode */
  212. .default_resolution = 12,
  213. .resolution_limits = 9,
  214. .default_sample_time = 240,
  215. .num_sample_times = 4,
  216. .sample_times = (unsigned int []){ 30, 60, 120, 240 },
  217. .resolutions = (u8 []) {9, 10, 11, 12 },
  218. },
  219. [tmp100] = {
  220. .set_mask = 3 << 5, /* 12-bit mode */
  221. .clr_mask = 1 << 7, /* not one-shot mode */
  222. .default_resolution = 12,
  223. .default_sample_time = 320,
  224. .num_sample_times = 4,
  225. .sample_times = (unsigned int []){ 40, 80, 160, 320 },
  226. .resolutions = (u8 []) {9, 10, 11, 12 },
  227. },
  228. [tmp101] = {
  229. .set_mask = 3 << 5, /* 12-bit mode */
  230. .clr_mask = 1 << 7, /* not one-shot mode */
  231. .default_resolution = 12,
  232. .default_sample_time = 320,
  233. .num_sample_times = 4,
  234. .sample_times = (unsigned int []){ 40, 80, 160, 320 },
  235. .resolutions = (u8 []) {9, 10, 11, 12 },
  236. },
  237. [tmp105] = {
  238. .set_mask = 3 << 5, /* 12-bit mode */
  239. .clr_mask = 1 << 7, /* not one-shot mode*/
  240. .default_resolution = 12,
  241. .default_sample_time = 220,
  242. .num_sample_times = 4,
  243. .sample_times = (unsigned int []){ 28, 55, 110, 220 },
  244. .resolutions = (u8 []) {9, 10, 11, 12 },
  245. },
  246. [tmp112] = {
  247. .set_mask = 3 << 5, /* 8 samples / second */
  248. .clr_mask = 1 << 7, /* no one-shot mode*/
  249. .default_resolution = 12,
  250. .default_sample_time = 125,
  251. .num_sample_times = 4,
  252. .sample_times = (unsigned int []){ 125, 250, 1000, 4000 },
  253. },
  254. [tmp175] = {
  255. .set_mask = 3 << 5, /* 12-bit mode */
  256. .clr_mask = 1 << 7, /* not one-shot mode*/
  257. .default_resolution = 12,
  258. .default_sample_time = 220,
  259. .num_sample_times = 4,
  260. .sample_times = (unsigned int []){ 28, 55, 110, 220 },
  261. .resolutions = (u8 []) {9, 10, 11, 12 },
  262. },
  263. [tmp275] = {
  264. .set_mask = 3 << 5, /* 12-bit mode */
  265. .clr_mask = 1 << 7, /* not one-shot mode*/
  266. .default_resolution = 12,
  267. .default_sample_time = 220,
  268. .num_sample_times = 4,
  269. .sample_times = (unsigned int []){ 28, 55, 110, 220 },
  270. .resolutions = (u8 []) {9, 10, 11, 12 },
  271. },
  272. [tmp75] = {
  273. .set_mask = 3 << 5, /* 12-bit mode */
  274. .clr_mask = 1 << 7, /* not one-shot mode*/
  275. .default_resolution = 12,
  276. .default_sample_time = 220,
  277. .num_sample_times = 4,
  278. .sample_times = (unsigned int []){ 28, 55, 110, 220 },
  279. .resolutions = (u8 []) {9, 10, 11, 12 },
  280. },
  281. [tmp75b] = { /* not one-shot mode, Conversion rate 37Hz */
  282. .clr_mask = 1 << 7 | 3 << 5,
  283. .default_resolution = 12,
  284. .default_sample_time = MSEC_PER_SEC / 37,
  285. .sample_times = (unsigned int []){ MSEC_PER_SEC / 37,
  286. MSEC_PER_SEC / 18,
  287. MSEC_PER_SEC / 9, MSEC_PER_SEC / 4 },
  288. .num_sample_times = 4,
  289. },
  290. [tmp75c] = {
  291. .clr_mask = 1 << 5, /*not one-shot mode*/
  292. .default_resolution = 12,
  293. .default_sample_time = MSEC_PER_SEC / 12,
  294. },
  295. [tmp1075] = { /* not one-shot mode, 27.5 ms sample rate */
  296. .clr_mask = 1 << 5 | 1 << 6 | 1 << 7,
  297. .default_resolution = 12,
  298. .default_sample_time = 28,
  299. .num_sample_times = 4,
  300. .sample_times = (unsigned int []){ 28, 55, 110, 220 },
  301. }
  302. };
  303. static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
  304. {
  305. return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
  306. }
  307. static int lm75_write_config(struct lm75_data *data, u8 set_mask,
  308. u8 clr_mask)
  309. {
  310. u8 value;
  311. clr_mask |= LM75_SHUTDOWN;
  312. value = data->current_conf & ~clr_mask;
  313. value |= set_mask;
  314. if (data->current_conf != value) {
  315. s32 err;
  316. err = i2c_smbus_write_byte_data(data->client, LM75_REG_CONF,
  317. value);
  318. if (err)
  319. return err;
  320. data->current_conf = value;
  321. }
  322. return 0;
  323. }
  324. static int lm75_read(struct device *dev, enum hwmon_sensor_types type,
  325. u32 attr, int channel, long *val)
  326. {
  327. struct lm75_data *data = dev_get_drvdata(dev);
  328. unsigned int regval;
  329. int err, reg;
  330. switch (type) {
  331. case hwmon_chip:
  332. switch (attr) {
  333. case hwmon_chip_update_interval:
  334. *val = data->sample_time;
  335. break;
  336. default:
  337. return -EINVAL;
  338. }
  339. break;
  340. case hwmon_temp:
  341. switch (attr) {
  342. case hwmon_temp_input:
  343. reg = LM75_REG_TEMP;
  344. break;
  345. case hwmon_temp_max:
  346. reg = LM75_REG_MAX;
  347. break;
  348. case hwmon_temp_max_hyst:
  349. reg = LM75_REG_HYST;
  350. break;
  351. default:
  352. return -EINVAL;
  353. }
  354. err = regmap_read(data->regmap, reg, &regval);
  355. if (err < 0)
  356. return err;
  357. *val = lm75_reg_to_mc(regval, data->resolution);
  358. break;
  359. default:
  360. return -EINVAL;
  361. }
  362. return 0;
  363. }
  364. static int lm75_write_temp(struct device *dev, u32 attr, long temp)
  365. {
  366. struct lm75_data *data = dev_get_drvdata(dev);
  367. u8 resolution;
  368. int reg;
  369. switch (attr) {
  370. case hwmon_temp_max:
  371. reg = LM75_REG_MAX;
  372. break;
  373. case hwmon_temp_max_hyst:
  374. reg = LM75_REG_HYST;
  375. break;
  376. default:
  377. return -EINVAL;
  378. }
  379. /*
  380. * Resolution of limit registers is assumed to be the same as the
  381. * temperature input register resolution unless given explicitly.
  382. */
  383. if (data->params->resolution_limits)
  384. resolution = data->params->resolution_limits;
  385. else
  386. resolution = data->resolution;
  387. temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
  388. temp = DIV_ROUND_CLOSEST(temp << (resolution - 8),
  389. 1000) << (16 - resolution);
  390. return regmap_write(data->regmap, reg, (u16)temp);
  391. }
  392. static int lm75_update_interval(struct device *dev, long val)
  393. {
  394. struct lm75_data *data = dev_get_drvdata(dev);
  395. unsigned int reg;
  396. u8 index;
  397. s32 err;
  398. index = find_closest(val, data->params->sample_times,
  399. (int)data->params->num_sample_times);
  400. switch (data->kind) {
  401. default:
  402. err = lm75_write_config(data, lm75_sample_set_masks[index],
  403. LM75_SAMPLE_CLEAR_MASK);
  404. if (err)
  405. return err;
  406. data->sample_time = data->params->sample_times[index];
  407. if (data->params->resolutions)
  408. data->resolution = data->params->resolutions[index];
  409. break;
  410. case tmp112:
  411. err = regmap_read(data->regmap, LM75_REG_CONF, &reg);
  412. if (err < 0)
  413. return err;
  414. reg &= ~0x00c0;
  415. reg |= (3 - index) << 6;
  416. err = regmap_write(data->regmap, LM75_REG_CONF, reg);
  417. if (err < 0)
  418. return err;
  419. data->sample_time = data->params->sample_times[index];
  420. break;
  421. case pct2075:
  422. err = i2c_smbus_write_byte_data(data->client, PCT2075_REG_IDLE,
  423. index + 1);
  424. if (err)
  425. return err;
  426. data->sample_time = data->params->sample_times[index];
  427. break;
  428. }
  429. return 0;
  430. }
  431. static int lm75_write_chip(struct device *dev, u32 attr, long val)
  432. {
  433. switch (attr) {
  434. case hwmon_chip_update_interval:
  435. return lm75_update_interval(dev, val);
  436. default:
  437. return -EINVAL;
  438. }
  439. return 0;
  440. }
  441. static int lm75_write(struct device *dev, enum hwmon_sensor_types type,
  442. u32 attr, int channel, long val)
  443. {
  444. switch (type) {
  445. case hwmon_chip:
  446. return lm75_write_chip(dev, attr, val);
  447. case hwmon_temp:
  448. return lm75_write_temp(dev, attr, val);
  449. default:
  450. return -EINVAL;
  451. }
  452. return 0;
  453. }
  454. static umode_t lm75_is_visible(const void *data, enum hwmon_sensor_types type,
  455. u32 attr, int channel)
  456. {
  457. const struct lm75_data *config_data = data;
  458. switch (type) {
  459. case hwmon_chip:
  460. switch (attr) {
  461. case hwmon_chip_update_interval:
  462. if (config_data->params->num_sample_times > 1)
  463. return 0644;
  464. return 0444;
  465. }
  466. break;
  467. case hwmon_temp:
  468. switch (attr) {
  469. case hwmon_temp_input:
  470. return 0444;
  471. case hwmon_temp_max:
  472. case hwmon_temp_max_hyst:
  473. return 0644;
  474. }
  475. break;
  476. default:
  477. break;
  478. }
  479. return 0;
  480. }
  481. static const struct hwmon_channel_info *lm75_info[] = {
  482. HWMON_CHANNEL_INFO(chip,
  483. HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
  484. HWMON_CHANNEL_INFO(temp,
  485. HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST),
  486. NULL
  487. };
  488. static const struct hwmon_ops lm75_hwmon_ops = {
  489. .is_visible = lm75_is_visible,
  490. .read = lm75_read,
  491. .write = lm75_write,
  492. };
  493. static const struct hwmon_chip_info lm75_chip_info = {
  494. .ops = &lm75_hwmon_ops,
  495. .info = lm75_info,
  496. };
  497. static bool lm75_is_writeable_reg(struct device *dev, unsigned int reg)
  498. {
  499. return reg != LM75_REG_TEMP;
  500. }
  501. static bool lm75_is_volatile_reg(struct device *dev, unsigned int reg)
  502. {
  503. return reg == LM75_REG_TEMP || reg == LM75_REG_CONF;
  504. }
  505. static const struct regmap_config lm75_regmap_config = {
  506. .reg_bits = 8,
  507. .val_bits = 16,
  508. .max_register = PCT2075_REG_IDLE,
  509. .writeable_reg = lm75_is_writeable_reg,
  510. .volatile_reg = lm75_is_volatile_reg,
  511. .val_format_endian = REGMAP_ENDIAN_BIG,
  512. .cache_type = REGCACHE_RBTREE,
  513. .use_single_read = true,
  514. .use_single_write = true,
  515. };
  516. static void lm75_disable_regulator(void *data)
  517. {
  518. struct lm75_data *lm75 = data;
  519. regulator_disable(lm75->vs);
  520. }
  521. static void lm75_remove(void *data)
  522. {
  523. struct lm75_data *lm75 = data;
  524. struct i2c_client *client = lm75->client;
  525. i2c_smbus_write_byte_data(client, LM75_REG_CONF, lm75->orig_conf);
  526. }
  527. static const struct i2c_device_id lm75_ids[];
  528. static int lm75_probe(struct i2c_client *client)
  529. {
  530. struct device *dev = &client->dev;
  531. struct device *hwmon_dev;
  532. struct lm75_data *data;
  533. int status, err;
  534. enum lm75_type kind;
  535. if (client->dev.of_node)
  536. kind = (enum lm75_type)of_device_get_match_data(&client->dev);
  537. else
  538. kind = i2c_match_id(lm75_ids, client)->driver_data;
  539. if (!i2c_check_functionality(client->adapter,
  540. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
  541. return -EIO;
  542. data = devm_kzalloc(dev, sizeof(struct lm75_data), GFP_KERNEL);
  543. if (!data)
  544. return -ENOMEM;
  545. data->client = client;
  546. data->kind = kind;
  547. data->vs = devm_regulator_get(dev, "vs");
  548. if (IS_ERR(data->vs))
  549. return PTR_ERR(data->vs);
  550. data->regmap = devm_regmap_init_i2c(client, &lm75_regmap_config);
  551. if (IS_ERR(data->regmap))
  552. return PTR_ERR(data->regmap);
  553. /* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
  554. * Then tweak to be more precise when appropriate.
  555. */
  556. data->params = &device_params[data->kind];
  557. /* Save default sample time and resolution*/
  558. data->sample_time = data->params->default_sample_time;
  559. data->resolution = data->params->default_resolution;
  560. /* Enable the power */
  561. err = regulator_enable(data->vs);
  562. if (err) {
  563. dev_err(dev, "failed to enable regulator: %d\n", err);
  564. return err;
  565. }
  566. err = devm_add_action_or_reset(dev, lm75_disable_regulator, data);
  567. if (err)
  568. return err;
  569. /* Cache original configuration */
  570. status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
  571. if (status < 0) {
  572. dev_dbg(dev, "Can't read config? %d\n", status);
  573. return status;
  574. }
  575. data->orig_conf = status;
  576. data->current_conf = status;
  577. err = lm75_write_config(data, data->params->set_mask,
  578. data->params->clr_mask);
  579. if (err)
  580. return err;
  581. err = devm_add_action_or_reset(dev, lm75_remove, data);
  582. if (err)
  583. return err;
  584. hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
  585. data, &lm75_chip_info,
  586. NULL);
  587. if (IS_ERR(hwmon_dev))
  588. return PTR_ERR(hwmon_dev);
  589. dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), client->name);
  590. return 0;
  591. }
  592. static const struct i2c_device_id lm75_ids[] = {
  593. { "adt75", adt75, },
  594. { "at30ts74", at30ts74, },
  595. { "ds1775", ds1775, },
  596. { "ds75", ds75, },
  597. { "ds7505", ds7505, },
  598. { "g751", g751, },
  599. { "lm75", lm75, },
  600. { "lm75a", lm75a, },
  601. { "lm75b", lm75b, },
  602. { "max6625", max6625, },
  603. { "max6626", max6626, },
  604. { "max31725", max31725, },
  605. { "max31726", max31725, },
  606. { "mcp980x", mcp980x, },
  607. { "pct2075", pct2075, },
  608. { "stds75", stds75, },
  609. { "stlm75", stlm75, },
  610. { "tcn75", tcn75, },
  611. { "tmp100", tmp100, },
  612. { "tmp101", tmp101, },
  613. { "tmp105", tmp105, },
  614. { "tmp112", tmp112, },
  615. { "tmp175", tmp175, },
  616. { "tmp275", tmp275, },
  617. { "tmp75", tmp75, },
  618. { "tmp75b", tmp75b, },
  619. { "tmp75c", tmp75c, },
  620. { "tmp1075", tmp1075, },
  621. { /* LIST END */ }
  622. };
  623. MODULE_DEVICE_TABLE(i2c, lm75_ids);
  624. static const struct of_device_id __maybe_unused lm75_of_match[] = {
  625. {
  626. .compatible = "adi,adt75",
  627. .data = (void *)adt75
  628. },
  629. {
  630. .compatible = "atmel,at30ts74",
  631. .data = (void *)at30ts74
  632. },
  633. {
  634. .compatible = "dallas,ds1775",
  635. .data = (void *)ds1775
  636. },
  637. {
  638. .compatible = "dallas,ds75",
  639. .data = (void *)ds75
  640. },
  641. {
  642. .compatible = "dallas,ds7505",
  643. .data = (void *)ds7505
  644. },
  645. {
  646. .compatible = "gmt,g751",
  647. .data = (void *)g751
  648. },
  649. {
  650. .compatible = "national,lm75",
  651. .data = (void *)lm75
  652. },
  653. {
  654. .compatible = "national,lm75a",
  655. .data = (void *)lm75a
  656. },
  657. {
  658. .compatible = "national,lm75b",
  659. .data = (void *)lm75b
  660. },
  661. {
  662. .compatible = "maxim,max6625",
  663. .data = (void *)max6625
  664. },
  665. {
  666. .compatible = "maxim,max6626",
  667. .data = (void *)max6626
  668. },
  669. {
  670. .compatible = "maxim,max31725",
  671. .data = (void *)max31725
  672. },
  673. {
  674. .compatible = "maxim,max31726",
  675. .data = (void *)max31725
  676. },
  677. {
  678. .compatible = "maxim,mcp980x",
  679. .data = (void *)mcp980x
  680. },
  681. {
  682. .compatible = "nxp,pct2075",
  683. .data = (void *)pct2075
  684. },
  685. {
  686. .compatible = "st,stds75",
  687. .data = (void *)stds75
  688. },
  689. {
  690. .compatible = "st,stlm75",
  691. .data = (void *)stlm75
  692. },
  693. {
  694. .compatible = "microchip,tcn75",
  695. .data = (void *)tcn75
  696. },
  697. {
  698. .compatible = "ti,tmp100",
  699. .data = (void *)tmp100
  700. },
  701. {
  702. .compatible = "ti,tmp101",
  703. .data = (void *)tmp101
  704. },
  705. {
  706. .compatible = "ti,tmp105",
  707. .data = (void *)tmp105
  708. },
  709. {
  710. .compatible = "ti,tmp112",
  711. .data = (void *)tmp112
  712. },
  713. {
  714. .compatible = "ti,tmp175",
  715. .data = (void *)tmp175
  716. },
  717. {
  718. .compatible = "ti,tmp275",
  719. .data = (void *)tmp275
  720. },
  721. {
  722. .compatible = "ti,tmp75",
  723. .data = (void *)tmp75
  724. },
  725. {
  726. .compatible = "ti,tmp75b",
  727. .data = (void *)tmp75b
  728. },
  729. {
  730. .compatible = "ti,tmp75c",
  731. .data = (void *)tmp75c
  732. },
  733. {
  734. .compatible = "ti,tmp1075",
  735. .data = (void *)tmp1075
  736. },
  737. { },
  738. };
  739. MODULE_DEVICE_TABLE(of, lm75_of_match);
  740. #define LM75A_ID 0xA1
  741. /* Return 0 if detection is successful, -ENODEV otherwise */
  742. static int lm75_detect(struct i2c_client *new_client,
  743. struct i2c_board_info *info)
  744. {
  745. struct i2c_adapter *adapter = new_client->adapter;
  746. int i;
  747. int conf, hyst, os;
  748. bool is_lm75a = 0;
  749. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  750. I2C_FUNC_SMBUS_WORD_DATA))
  751. return -ENODEV;
  752. /*
  753. * Now, we do the remaining detection. There is no identification-
  754. * dedicated register so we have to rely on several tricks:
  755. * unused bits, registers cycling over 8-address boundaries,
  756. * addresses 0x04-0x07 returning the last read value.
  757. * The cycling+unused addresses combination is not tested,
  758. * since it would significantly slow the detection down and would
  759. * hardly add any value.
  760. *
  761. * The National Semiconductor LM75A is different than earlier
  762. * LM75s. It has an ID byte of 0xaX (where X is the chip
  763. * revision, with 1 being the only revision in existence) in
  764. * register 7, and unused registers return 0xff rather than the
  765. * last read value.
  766. *
  767. * Note that this function only detects the original National
  768. * Semiconductor LM75 and the LM75A. Clones from other vendors
  769. * aren't detected, on purpose, because they are typically never
  770. * found on PC hardware. They are found on embedded designs where
  771. * they can be instantiated explicitly so detection is not needed.
  772. * The absence of identification registers on all these clones
  773. * would make their exhaustive detection very difficult and weak,
  774. * and odds are that the driver would bind to unsupported devices.
  775. */
  776. /* Unused bits */
  777. conf = i2c_smbus_read_byte_data(new_client, 1);
  778. if (conf & 0xe0)
  779. return -ENODEV;
  780. /* First check for LM75A */
  781. if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) {
  782. /*
  783. * LM75A returns 0xff on unused registers so
  784. * just to be sure we check for that too.
  785. */
  786. if (i2c_smbus_read_byte_data(new_client, 4) != 0xff
  787. || i2c_smbus_read_byte_data(new_client, 5) != 0xff
  788. || i2c_smbus_read_byte_data(new_client, 6) != 0xff)
  789. return -ENODEV;
  790. is_lm75a = 1;
  791. hyst = i2c_smbus_read_byte_data(new_client, 2);
  792. os = i2c_smbus_read_byte_data(new_client, 3);
  793. } else { /* Traditional style LM75 detection */
  794. /* Unused addresses */
  795. hyst = i2c_smbus_read_byte_data(new_client, 2);
  796. if (i2c_smbus_read_byte_data(new_client, 4) != hyst
  797. || i2c_smbus_read_byte_data(new_client, 5) != hyst
  798. || i2c_smbus_read_byte_data(new_client, 6) != hyst
  799. || i2c_smbus_read_byte_data(new_client, 7) != hyst)
  800. return -ENODEV;
  801. os = i2c_smbus_read_byte_data(new_client, 3);
  802. if (i2c_smbus_read_byte_data(new_client, 4) != os
  803. || i2c_smbus_read_byte_data(new_client, 5) != os
  804. || i2c_smbus_read_byte_data(new_client, 6) != os
  805. || i2c_smbus_read_byte_data(new_client, 7) != os)
  806. return -ENODEV;
  807. }
  808. /*
  809. * It is very unlikely that this is a LM75 if both
  810. * hysteresis and temperature limit registers are 0.
  811. */
  812. if (hyst == 0 && os == 0)
  813. return -ENODEV;
  814. /* Addresses cycling */
  815. for (i = 8; i <= 248; i += 40) {
  816. if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
  817. || i2c_smbus_read_byte_data(new_client, i + 2) != hyst
  818. || i2c_smbus_read_byte_data(new_client, i + 3) != os)
  819. return -ENODEV;
  820. if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7)
  821. != LM75A_ID)
  822. return -ENODEV;
  823. }
  824. strscpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE);
  825. return 0;
  826. }
  827. #ifdef CONFIG_PM
  828. static int lm75_suspend(struct device *dev)
  829. {
  830. int status;
  831. struct i2c_client *client = to_i2c_client(dev);
  832. status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
  833. if (status < 0) {
  834. dev_dbg(&client->dev, "Can't read config? %d\n", status);
  835. return status;
  836. }
  837. status = status | LM75_SHUTDOWN;
  838. i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
  839. return 0;
  840. }
  841. static int lm75_resume(struct device *dev)
  842. {
  843. int status;
  844. struct i2c_client *client = to_i2c_client(dev);
  845. status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
  846. if (status < 0) {
  847. dev_dbg(&client->dev, "Can't read config? %d\n", status);
  848. return status;
  849. }
  850. status = status & ~LM75_SHUTDOWN;
  851. i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
  852. return 0;
  853. }
  854. static const struct dev_pm_ops lm75_dev_pm_ops = {
  855. .suspend = lm75_suspend,
  856. .resume = lm75_resume,
  857. };
  858. #define LM75_DEV_PM_OPS (&lm75_dev_pm_ops)
  859. #else
  860. #define LM75_DEV_PM_OPS NULL
  861. #endif /* CONFIG_PM */
  862. static struct i2c_driver lm75_driver = {
  863. .class = I2C_CLASS_HWMON,
  864. .driver = {
  865. .name = "lm75",
  866. .of_match_table = of_match_ptr(lm75_of_match),
  867. .pm = LM75_DEV_PM_OPS,
  868. },
  869. .probe_new = lm75_probe,
  870. .id_table = lm75_ids,
  871. .detect = lm75_detect,
  872. .address_list = normal_i2c,
  873. };
  874. module_i2c_driver(lm75_driver);
  875. MODULE_AUTHOR("Frodo Looijaard <[email protected]>");
  876. MODULE_DESCRIPTION("LM75 driver");
  877. MODULE_LICENSE("GPL");