lm75.c 28 KB

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