spd5118.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for Jedec 5118 compliant temperature sensors
  4. *
  5. * Derived from https://github.com/Steve-Tech/SPD5118-DKMS
  6. * Originally from T/2 driver at https://t2sde.org/packages/linux
  7. * Copyright (c) 2023 René Rebe, ExactCODE GmbH; Germany.
  8. *
  9. * Copyright (c) 2024 Guenter Roeck
  10. *
  11. * Inspired by ee1004.c and jc42.c.
  12. *
  13. * SPD5118 compliant temperature sensors are typically used on DDR5
  14. * memory modules.
  15. */
  16. #include <linux/bitops.h>
  17. #include <linux/bits.h>
  18. #include <linux/err.h>
  19. #include <linux/i2c.h>
  20. #include <linux/hwmon.h>
  21. #include <linux/module.h>
  22. #include <linux/mutex.h>
  23. #include <linux/nvmem-provider.h>
  24. #include <linux/pm.h>
  25. #include <linux/regmap.h>
  26. #include <linux/units.h>
  27. /* Addresses to scan */
  28. static const unsigned short normal_i2c[] = {
  29. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, I2C_CLIENT_END };
  30. /* SPD5118 registers. */
  31. #define SPD5118_REG_TYPE 0x00 /* MR0:MR1 */
  32. #define SPD5118_REG_REVISION 0x02 /* MR2 */
  33. #define SPD5118_REG_VENDOR 0x03 /* MR3:MR4 */
  34. #define SPD5118_REG_CAPABILITY 0x05 /* MR5 */
  35. #define SPD5118_REG_I2C_LEGACY_MODE 0x0B /* MR11 */
  36. #define SPD5118_REG_TEMP_CLR 0x13 /* MR19 */
  37. #define SPD5118_REG_ERROR_CLR 0x14 /* MR20 */
  38. #define SPD5118_REG_TEMP_CONFIG 0x1A /* MR26 */
  39. #define SPD5118_REG_TEMP_MAX 0x1c /* MR28:MR29 */
  40. #define SPD5118_REG_TEMP_MIN 0x1e /* MR30:MR31 */
  41. #define SPD5118_REG_TEMP_CRIT 0x20 /* MR32:MR33 */
  42. #define SPD5118_REG_TEMP_LCRIT 0x22 /* MR34:MR35 */
  43. #define SPD5118_REG_TEMP 0x31 /* MR49:MR50 */
  44. #define SPD5118_REG_TEMP_STATUS 0x33 /* MR51 */
  45. #define SPD5118_TEMP_STATUS_HIGH BIT(0)
  46. #define SPD5118_TEMP_STATUS_LOW BIT(1)
  47. #define SPD5118_TEMP_STATUS_CRIT BIT(2)
  48. #define SPD5118_TEMP_STATUS_LCRIT BIT(3)
  49. #define SPD5118_CAP_TS_SUPPORT BIT(1) /* temperature sensor support */
  50. #define SPD5118_TS_DISABLE BIT(0) /* temperature sensor disable */
  51. #define SPD5118_LEGACY_MODE_ADDR BIT(3)
  52. #define SPD5118_LEGACY_PAGE_MASK GENMASK(2, 0)
  53. #define SPD5118_LEGACY_MODE_MASK (SPD5118_LEGACY_MODE_ADDR | SPD5118_LEGACY_PAGE_MASK)
  54. #define SPD5118_NUM_PAGES 8
  55. #define SPD5118_PAGE_SIZE 128
  56. #define SPD5118_PAGE_SHIFT 7
  57. #define SPD5118_PAGE_MASK GENMASK(6, 0)
  58. #define SPD5118_EEPROM_BASE 0x80
  59. #define SPD5118_EEPROM_SIZE (SPD5118_PAGE_SIZE * SPD5118_NUM_PAGES)
  60. #define PAGE_ADDR0(page) (((page) & BIT(0)) << 6)
  61. #define PAGE_ADDR1_4(page) (((page) & GENMASK(4, 1)) >> 1)
  62. /* Temperature unit in millicelsius */
  63. #define SPD5118_TEMP_UNIT (MILLIDEGREE_PER_DEGREE / 4)
  64. /* Representable temperature range in millicelsius */
  65. #define SPD5118_TEMP_RANGE_MIN -256000
  66. #define SPD5118_TEMP_RANGE_MAX 255750
  67. struct spd5118_data {
  68. struct regmap *regmap;
  69. struct mutex nvmem_lock;
  70. bool is_16bit;
  71. };
  72. /* hwmon */
  73. static int spd5118_temp_from_reg(u16 reg)
  74. {
  75. int temp = sign_extend32(reg >> 2, 10);
  76. return temp * SPD5118_TEMP_UNIT;
  77. }
  78. static u16 spd5118_temp_to_reg(long temp)
  79. {
  80. temp = clamp_val(temp, SPD5118_TEMP_RANGE_MIN, SPD5118_TEMP_RANGE_MAX);
  81. return (DIV_ROUND_CLOSEST(temp, SPD5118_TEMP_UNIT) & 0x7ff) << 2;
  82. }
  83. static int spd5118_read_temp(struct regmap *regmap, u32 attr, long *val)
  84. {
  85. int reg, err;
  86. u8 regval[2];
  87. u16 temp;
  88. switch (attr) {
  89. case hwmon_temp_input:
  90. reg = SPD5118_REG_TEMP;
  91. break;
  92. case hwmon_temp_max:
  93. reg = SPD5118_REG_TEMP_MAX;
  94. break;
  95. case hwmon_temp_min:
  96. reg = SPD5118_REG_TEMP_MIN;
  97. break;
  98. case hwmon_temp_crit:
  99. reg = SPD5118_REG_TEMP_CRIT;
  100. break;
  101. case hwmon_temp_lcrit:
  102. reg = SPD5118_REG_TEMP_LCRIT;
  103. break;
  104. default:
  105. return -EOPNOTSUPP;
  106. }
  107. err = regmap_bulk_read(regmap, reg, regval, 2);
  108. if (err)
  109. return err;
  110. temp = (regval[1] << 8) | regval[0];
  111. *val = spd5118_temp_from_reg(temp);
  112. return 0;
  113. }
  114. static int spd5118_read_alarm(struct regmap *regmap, u32 attr, long *val)
  115. {
  116. unsigned int mask, regval;
  117. int err;
  118. switch (attr) {
  119. case hwmon_temp_max_alarm:
  120. mask = SPD5118_TEMP_STATUS_HIGH;
  121. break;
  122. case hwmon_temp_min_alarm:
  123. mask = SPD5118_TEMP_STATUS_LOW;
  124. break;
  125. case hwmon_temp_crit_alarm:
  126. mask = SPD5118_TEMP_STATUS_CRIT;
  127. break;
  128. case hwmon_temp_lcrit_alarm:
  129. mask = SPD5118_TEMP_STATUS_LCRIT;
  130. break;
  131. default:
  132. return -EOPNOTSUPP;
  133. }
  134. err = regmap_read(regmap, SPD5118_REG_TEMP_STATUS, &regval);
  135. if (err < 0)
  136. return err;
  137. *val = !!(regval & mask);
  138. if (*val)
  139. return regmap_write(regmap, SPD5118_REG_TEMP_CLR, mask);
  140. return 0;
  141. }
  142. static int spd5118_read_enable(struct regmap *regmap, long *val)
  143. {
  144. u32 regval;
  145. int err;
  146. err = regmap_read(regmap, SPD5118_REG_TEMP_CONFIG, &regval);
  147. if (err < 0)
  148. return err;
  149. *val = !(regval & SPD5118_TS_DISABLE);
  150. return 0;
  151. }
  152. static int spd5118_read(struct device *dev, enum hwmon_sensor_types type,
  153. u32 attr, int channel, long *val)
  154. {
  155. struct regmap *regmap = dev_get_drvdata(dev);
  156. if (type != hwmon_temp)
  157. return -EOPNOTSUPP;
  158. switch (attr) {
  159. case hwmon_temp_input:
  160. case hwmon_temp_max:
  161. case hwmon_temp_min:
  162. case hwmon_temp_crit:
  163. case hwmon_temp_lcrit:
  164. return spd5118_read_temp(regmap, attr, val);
  165. case hwmon_temp_max_alarm:
  166. case hwmon_temp_min_alarm:
  167. case hwmon_temp_crit_alarm:
  168. case hwmon_temp_lcrit_alarm:
  169. return spd5118_read_alarm(regmap, attr, val);
  170. case hwmon_temp_enable:
  171. return spd5118_read_enable(regmap, val);
  172. default:
  173. return -EOPNOTSUPP;
  174. }
  175. }
  176. static int spd5118_write_temp(struct regmap *regmap, u32 attr, long val)
  177. {
  178. u8 regval[2];
  179. u16 temp;
  180. int reg;
  181. switch (attr) {
  182. case hwmon_temp_max:
  183. reg = SPD5118_REG_TEMP_MAX;
  184. break;
  185. case hwmon_temp_min:
  186. reg = SPD5118_REG_TEMP_MIN;
  187. break;
  188. case hwmon_temp_crit:
  189. reg = SPD5118_REG_TEMP_CRIT;
  190. break;
  191. case hwmon_temp_lcrit:
  192. reg = SPD5118_REG_TEMP_LCRIT;
  193. break;
  194. default:
  195. return -EOPNOTSUPP;
  196. }
  197. temp = spd5118_temp_to_reg(val);
  198. regval[0] = temp & 0xff;
  199. regval[1] = temp >> 8;
  200. return regmap_bulk_write(regmap, reg, regval, 2);
  201. }
  202. static int spd5118_write_enable(struct regmap *regmap, long val)
  203. {
  204. if (val && val != 1)
  205. return -EINVAL;
  206. return regmap_update_bits(regmap, SPD5118_REG_TEMP_CONFIG,
  207. SPD5118_TS_DISABLE,
  208. val ? 0 : SPD5118_TS_DISABLE);
  209. }
  210. static int spd5118_temp_write(struct regmap *regmap, u32 attr, long val)
  211. {
  212. switch (attr) {
  213. case hwmon_temp_max:
  214. case hwmon_temp_min:
  215. case hwmon_temp_crit:
  216. case hwmon_temp_lcrit:
  217. return spd5118_write_temp(regmap, attr, val);
  218. case hwmon_temp_enable:
  219. return spd5118_write_enable(regmap, val);
  220. default:
  221. return -EOPNOTSUPP;
  222. }
  223. }
  224. static int spd5118_write(struct device *dev, enum hwmon_sensor_types type,
  225. u32 attr, int channel, long val)
  226. {
  227. struct regmap *regmap = dev_get_drvdata(dev);
  228. switch (type) {
  229. case hwmon_temp:
  230. return spd5118_temp_write(regmap, attr, val);
  231. default:
  232. return -EOPNOTSUPP;
  233. }
  234. }
  235. static umode_t spd5118_is_visible(const void *_data, enum hwmon_sensor_types type,
  236. u32 attr, int channel)
  237. {
  238. if (type != hwmon_temp)
  239. return 0;
  240. switch (attr) {
  241. case hwmon_temp_input:
  242. return 0444;
  243. case hwmon_temp_min:
  244. case hwmon_temp_max:
  245. case hwmon_temp_lcrit:
  246. case hwmon_temp_crit:
  247. case hwmon_temp_enable:
  248. return 0644;
  249. case hwmon_temp_min_alarm:
  250. case hwmon_temp_max_alarm:
  251. case hwmon_temp_crit_alarm:
  252. case hwmon_temp_lcrit_alarm:
  253. return 0444;
  254. default:
  255. return 0;
  256. }
  257. }
  258. /*
  259. * Bank and vendor id are 8-bit fields with seven data bits and odd parity.
  260. * Vendor IDs 0 and 0x7f are invalid.
  261. * See Jedec standard JEP106BJ for details and a list of assigned vendor IDs.
  262. */
  263. static bool spd5118_vendor_valid(u8 bank, u8 id)
  264. {
  265. if (parity8(bank) == 0 || parity8(id) == 0)
  266. return false;
  267. id &= 0x7f;
  268. return id && id != 0x7f;
  269. }
  270. static const struct hwmon_channel_info *spd5118_info[] = {
  271. HWMON_CHANNEL_INFO(chip,
  272. HWMON_C_REGISTER_TZ),
  273. HWMON_CHANNEL_INFO(temp,
  274. HWMON_T_INPUT |
  275. HWMON_T_LCRIT | HWMON_T_LCRIT_ALARM |
  276. HWMON_T_MIN | HWMON_T_MIN_ALARM |
  277. HWMON_T_MAX | HWMON_T_MAX_ALARM |
  278. HWMON_T_CRIT | HWMON_T_CRIT_ALARM |
  279. HWMON_T_ENABLE),
  280. NULL
  281. };
  282. static const struct hwmon_ops spd5118_hwmon_ops = {
  283. .is_visible = spd5118_is_visible,
  284. .read = spd5118_read,
  285. .write = spd5118_write,
  286. };
  287. static const struct hwmon_chip_info spd5118_chip_info = {
  288. .ops = &spd5118_hwmon_ops,
  289. .info = spd5118_info,
  290. };
  291. /* nvmem */
  292. static ssize_t spd5118_nvmem_read_page(struct spd5118_data *data, char *buf,
  293. unsigned int offset, size_t count)
  294. {
  295. int page = offset >> SPD5118_PAGE_SHIFT;
  296. struct regmap *regmap = data->regmap;
  297. int err, addr;
  298. offset &= SPD5118_PAGE_MASK;
  299. /* Can't cross page boundaries */
  300. if (offset + count > SPD5118_PAGE_SIZE)
  301. count = SPD5118_PAGE_SIZE - offset;
  302. if (data->is_16bit) {
  303. addr = SPD5118_EEPROM_BASE | PAGE_ADDR0(page) |
  304. (PAGE_ADDR1_4(page) << 8);
  305. } else {
  306. addr = page * 0x100 + SPD5118_EEPROM_BASE;
  307. }
  308. err = regmap_bulk_read(regmap, addr + offset, buf, count);
  309. if (err)
  310. return err;
  311. return count;
  312. }
  313. static int spd5118_nvmem_read(void *priv, unsigned int off, void *val, size_t count)
  314. {
  315. struct spd5118_data *data = priv;
  316. char *buf = val;
  317. int ret;
  318. if (unlikely(!count))
  319. return count;
  320. if (off + count > SPD5118_EEPROM_SIZE)
  321. return -EINVAL;
  322. mutex_lock(&data->nvmem_lock);
  323. while (count) {
  324. ret = spd5118_nvmem_read_page(data, buf, off, count);
  325. if (ret < 0) {
  326. mutex_unlock(&data->nvmem_lock);
  327. return ret;
  328. }
  329. buf += ret;
  330. off += ret;
  331. count -= ret;
  332. }
  333. mutex_unlock(&data->nvmem_lock);
  334. return 0;
  335. }
  336. static int spd5118_nvmem_init(struct device *dev, struct spd5118_data *data)
  337. {
  338. struct nvmem_config nvmem_config = {
  339. .type = NVMEM_TYPE_EEPROM,
  340. .name = dev_name(dev),
  341. .id = NVMEM_DEVID_NONE,
  342. .dev = dev,
  343. .base_dev = dev,
  344. .read_only = true,
  345. .root_only = false,
  346. .owner = THIS_MODULE,
  347. .compat = true,
  348. .reg_read = spd5118_nvmem_read,
  349. .priv = data,
  350. .stride = 1,
  351. .word_size = 1,
  352. .size = SPD5118_EEPROM_SIZE,
  353. };
  354. struct nvmem_device *nvmem;
  355. nvmem = devm_nvmem_register(dev, &nvmem_config);
  356. return PTR_ERR_OR_ZERO(nvmem);
  357. }
  358. /* regmap */
  359. static bool spd5118_writeable_reg(struct device *dev, unsigned int reg)
  360. {
  361. switch (reg) {
  362. case SPD5118_REG_I2C_LEGACY_MODE:
  363. case SPD5118_REG_TEMP_CLR:
  364. case SPD5118_REG_TEMP_CONFIG:
  365. case SPD5118_REG_TEMP_MAX:
  366. case SPD5118_REG_TEMP_MAX + 1:
  367. case SPD5118_REG_TEMP_MIN:
  368. case SPD5118_REG_TEMP_MIN + 1:
  369. case SPD5118_REG_TEMP_CRIT:
  370. case SPD5118_REG_TEMP_CRIT + 1:
  371. case SPD5118_REG_TEMP_LCRIT:
  372. case SPD5118_REG_TEMP_LCRIT + 1:
  373. return true;
  374. default:
  375. return false;
  376. }
  377. }
  378. static bool spd5118_volatile_reg(struct device *dev, unsigned int reg)
  379. {
  380. switch (reg) {
  381. case SPD5118_REG_TEMP_CLR:
  382. case SPD5118_REG_ERROR_CLR:
  383. case SPD5118_REG_TEMP:
  384. case SPD5118_REG_TEMP + 1:
  385. case SPD5118_REG_TEMP_STATUS:
  386. return true;
  387. default:
  388. return false;
  389. }
  390. }
  391. static const struct regmap_range_cfg spd5118_i2c_regmap_range_cfg[] = {
  392. {
  393. .selector_reg = SPD5118_REG_I2C_LEGACY_MODE,
  394. .selector_mask = SPD5118_LEGACY_PAGE_MASK,
  395. .selector_shift = 0,
  396. .window_start = 0,
  397. .window_len = 0x100,
  398. .range_min = 0,
  399. .range_max = 0x7ff,
  400. },
  401. };
  402. static const struct regmap_config spd5118_regmap8_config = {
  403. .reg_bits = 8,
  404. .val_bits = 8,
  405. .max_register = 0x7ff,
  406. .writeable_reg = spd5118_writeable_reg,
  407. .volatile_reg = spd5118_volatile_reg,
  408. .cache_type = REGCACHE_MAPLE,
  409. .ranges = spd5118_i2c_regmap_range_cfg,
  410. .num_ranges = ARRAY_SIZE(spd5118_i2c_regmap_range_cfg),
  411. };
  412. static const struct regmap_config spd5118_regmap16_config = {
  413. .reg_bits = 16,
  414. .val_bits = 8,
  415. .max_register = 0x7ff,
  416. .writeable_reg = spd5118_writeable_reg,
  417. .volatile_reg = spd5118_volatile_reg,
  418. .cache_type = REGCACHE_MAPLE,
  419. };
  420. static int spd5118_suspend(struct device *dev)
  421. {
  422. struct spd5118_data *data = dev_get_drvdata(dev);
  423. struct regmap *regmap = data->regmap;
  424. u32 regval;
  425. int err;
  426. /*
  427. * Make sure the configuration register in the regmap cache is current
  428. * before bypassing it.
  429. */
  430. err = regmap_read(regmap, SPD5118_REG_TEMP_CONFIG, &regval);
  431. if (err < 0)
  432. return err;
  433. regcache_cache_bypass(regmap, true);
  434. regmap_update_bits(regmap, SPD5118_REG_TEMP_CONFIG, SPD5118_TS_DISABLE,
  435. SPD5118_TS_DISABLE);
  436. regcache_cache_bypass(regmap, false);
  437. regcache_cache_only(regmap, true);
  438. regcache_mark_dirty(regmap);
  439. return 0;
  440. }
  441. static int spd5118_resume(struct device *dev)
  442. {
  443. struct spd5118_data *data = dev_get_drvdata(dev);
  444. struct regmap *regmap = data->regmap;
  445. regcache_cache_only(regmap, false);
  446. return regcache_sync(regmap);
  447. }
  448. static DEFINE_SIMPLE_DEV_PM_OPS(spd5118_pm_ops, spd5118_suspend, spd5118_resume);
  449. static int spd5118_common_probe(struct device *dev, struct regmap *regmap,
  450. bool is_16bit)
  451. {
  452. unsigned int capability, revision, vendor, bank;
  453. struct spd5118_data *data;
  454. struct device *hwmon_dev;
  455. int err;
  456. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  457. if (!data)
  458. return -ENOMEM;
  459. err = regmap_read(regmap, SPD5118_REG_CAPABILITY, &capability);
  460. if (err)
  461. return err;
  462. if (!(capability & SPD5118_CAP_TS_SUPPORT))
  463. return -ENODEV;
  464. data->is_16bit = is_16bit;
  465. err = regmap_read(regmap, SPD5118_REG_REVISION, &revision);
  466. if (err)
  467. return err;
  468. err = regmap_read(regmap, SPD5118_REG_VENDOR, &bank);
  469. if (err)
  470. return err;
  471. err = regmap_read(regmap, SPD5118_REG_VENDOR + 1, &vendor);
  472. if (err)
  473. return err;
  474. if (!spd5118_vendor_valid(bank, vendor))
  475. return -ENODEV;
  476. data->regmap = regmap;
  477. mutex_init(&data->nvmem_lock);
  478. dev_set_drvdata(dev, data);
  479. err = spd5118_nvmem_init(dev, data);
  480. /* Ignore if NVMEM support is disabled */
  481. if (err && err != -EOPNOTSUPP) {
  482. dev_err_probe(dev, err, "failed to register nvmem\n");
  483. return err;
  484. }
  485. hwmon_dev = devm_hwmon_device_register_with_info(dev, "spd5118",
  486. regmap, &spd5118_chip_info,
  487. NULL);
  488. if (IS_ERR(hwmon_dev))
  489. return PTR_ERR(hwmon_dev);
  490. /*
  491. * From JESD300-5B
  492. * MR2 bits [5:4]: Major revision, 1..4
  493. * MR2 bits [3:1]: Minor revision, 0..8? Probably a typo, assume 1..8
  494. */
  495. dev_info(dev, "DDR5 temperature sensor: vendor 0x%02x:0x%02x revision %d.%d\n",
  496. bank & 0x7f, vendor, ((revision >> 4) & 0x03) + 1, ((revision >> 1) & 0x07) + 1);
  497. return 0;
  498. }
  499. /* I2C */
  500. /* Return 0 if detection is successful, -ENODEV otherwise */
  501. static int spd5118_detect(struct i2c_client *client, struct i2c_board_info *info)
  502. {
  503. struct i2c_adapter *adapter = client->adapter;
  504. int regval;
  505. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  506. I2C_FUNC_SMBUS_WORD_DATA))
  507. return -ENODEV;
  508. regval = i2c_smbus_read_word_swapped(client, SPD5118_REG_TYPE);
  509. if (regval != 0x5118)
  510. return -ENODEV;
  511. regval = i2c_smbus_read_word_data(client, SPD5118_REG_VENDOR);
  512. if (regval < 0 || !spd5118_vendor_valid(regval & 0xff, regval >> 8))
  513. return -ENODEV;
  514. regval = i2c_smbus_read_byte_data(client, SPD5118_REG_CAPABILITY);
  515. if (regval < 0)
  516. return -ENODEV;
  517. if (!(regval & SPD5118_CAP_TS_SUPPORT) || (regval & 0xfc))
  518. return -ENODEV;
  519. regval = i2c_smbus_read_byte_data(client, SPD5118_REG_TEMP_CLR);
  520. if (regval)
  521. return -ENODEV;
  522. regval = i2c_smbus_read_byte_data(client, SPD5118_REG_ERROR_CLR);
  523. if (regval)
  524. return -ENODEV;
  525. regval = i2c_smbus_read_byte_data(client, SPD5118_REG_REVISION);
  526. if (regval < 0 || (regval & 0xc1))
  527. return -ENODEV;
  528. regval = i2c_smbus_read_byte_data(client, SPD5118_REG_TEMP_CONFIG);
  529. if (regval < 0)
  530. return -ENODEV;
  531. if (regval & ~SPD5118_TS_DISABLE)
  532. return -ENODEV;
  533. strscpy(info->type, "spd5118", I2C_NAME_SIZE);
  534. return 0;
  535. }
  536. static int spd5118_i2c_init(struct i2c_client *client)
  537. {
  538. struct i2c_adapter *adapter = client->adapter;
  539. int err, regval, mode;
  540. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  541. I2C_FUNC_SMBUS_WORD_DATA))
  542. return -ENODEV;
  543. regval = i2c_smbus_read_word_swapped(client, SPD5118_REG_TYPE);
  544. if (regval < 0 || (regval && regval != 0x5118))
  545. return -ENODEV;
  546. /*
  547. * If the device type registers return 0, it is possible that the chip
  548. * has a non-zero page selected and takes the specification literally,
  549. * i.e. disables access to volatile registers besides the page register
  550. * if the page is not 0. The Renesas/ITD SPD5118 Hub Controller is known
  551. * to show this behavior. Try to identify such chips.
  552. */
  553. if (!regval) {
  554. /* Vendor ID registers must also be 0 */
  555. regval = i2c_smbus_read_word_data(client, SPD5118_REG_VENDOR);
  556. if (regval)
  557. return -ENODEV;
  558. /* The selected page in MR11 must not be 0 */
  559. mode = i2c_smbus_read_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE);
  560. if (mode < 0 || (mode & ~SPD5118_LEGACY_MODE_MASK) ||
  561. !(mode & SPD5118_LEGACY_PAGE_MASK))
  562. return -ENODEV;
  563. err = i2c_smbus_write_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE,
  564. mode & SPD5118_LEGACY_MODE_ADDR);
  565. if (err)
  566. return -ENODEV;
  567. /*
  568. * If the device type registers are still bad after selecting
  569. * page 0, this is not a SPD5118 device. Restore original
  570. * legacy mode register value and abort.
  571. */
  572. regval = i2c_smbus_read_word_swapped(client, SPD5118_REG_TYPE);
  573. if (regval != 0x5118) {
  574. i2c_smbus_write_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE, mode);
  575. return -ENODEV;
  576. }
  577. }
  578. /* We are reasonably sure that this is really a SPD5118 hub controller */
  579. return 0;
  580. }
  581. /*
  582. * 16-bit addressing note:
  583. *
  584. * If I2C_FUNC_I2C is not supported by an I2C adapter driver, regmap uses
  585. * SMBus operations as alternative. To simulate a read operation with a 16-bit
  586. * address, it writes the address using i2c_smbus_write_byte_data(), followed
  587. * by one or more calls to i2c_smbus_read_byte() to read the data.
  588. * Per spd5118 standard, a read operation after writing the address must start
  589. * with <Sr> (Repeat Start). However, a SMBus read byte operation starts with
  590. * <S> (Start). This resets the register address in the spd5118 chip. As result,
  591. * i2c_smbus_read_byte() always returns data from register address 0x00.
  592. *
  593. * A working alternative to access chips with 16-bit register addresses in the
  594. * absence of I2C_FUNC_I2C support is not known.
  595. *
  596. * For this reason, 16-bit addressing can only be supported with I2C if the
  597. * adapter supports I2C_FUNC_I2C.
  598. *
  599. * For I2C, the addressing mode selected by the BIOS must not be changed.
  600. * Experiments show that at least some PC BIOS versions will not change the
  601. * addressing mode on a soft reboot and end up in setup, claiming that some
  602. * configuration change happened. This will happen again after a power cycle,
  603. * which does reset the addressing mode. To prevent this from happening,
  604. * detect if 16-bit addressing is enabled and always use the currently
  605. * configured addressing mode.
  606. */
  607. static int spd5118_i2c_probe(struct i2c_client *client)
  608. {
  609. const struct regmap_config *config;
  610. struct device *dev = &client->dev;
  611. struct regmap *regmap;
  612. int err, mode;
  613. bool is_16bit;
  614. err = spd5118_i2c_init(client);
  615. if (err)
  616. return err;
  617. mode = i2c_smbus_read_byte_data(client, SPD5118_REG_I2C_LEGACY_MODE);
  618. if (mode < 0)
  619. return mode;
  620. is_16bit = mode & SPD5118_LEGACY_MODE_ADDR;
  621. if (is_16bit) {
  622. /*
  623. * See 16-bit addressing note above explaining why it is
  624. * necessary to check for I2C_FUNC_I2C support here.
  625. */
  626. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  627. dev_err(dev, "Adapter does not support 16-bit register addresses\n");
  628. return -ENODEV;
  629. }
  630. config = &spd5118_regmap16_config;
  631. } else {
  632. config = &spd5118_regmap8_config;
  633. }
  634. regmap = devm_regmap_init_i2c(client, config);
  635. if (IS_ERR(regmap))
  636. return dev_err_probe(dev, PTR_ERR(regmap), "regmap init failed\n");
  637. return spd5118_common_probe(dev, regmap, is_16bit);
  638. }
  639. static const struct i2c_device_id spd5118_i2c_id[] = {
  640. { "spd5118" },
  641. { }
  642. };
  643. MODULE_DEVICE_TABLE(i2c, spd5118_i2c_id);
  644. static const struct of_device_id spd5118_of_ids[] = {
  645. { .compatible = "jedec,spd5118", },
  646. { }
  647. };
  648. MODULE_DEVICE_TABLE(of, spd5118_of_ids);
  649. static struct i2c_driver spd5118_i2c_driver = {
  650. .class = I2C_CLASS_HWMON,
  651. .driver = {
  652. .name = "spd5118",
  653. .of_match_table = spd5118_of_ids,
  654. .pm = pm_sleep_ptr(&spd5118_pm_ops),
  655. },
  656. .probe = spd5118_i2c_probe,
  657. .id_table = spd5118_i2c_id,
  658. .detect = IS_ENABLED(CONFIG_SENSORS_SPD5118_DETECT) ? spd5118_detect : NULL,
  659. .address_list = IS_ENABLED(CONFIG_SENSORS_SPD5118_DETECT) ? normal_i2c : NULL,
  660. };
  661. module_i2c_driver(spd5118_i2c_driver);
  662. MODULE_AUTHOR("René Rebe <rene@exactcode.de>");
  663. MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
  664. MODULE_DESCRIPTION("SPD 5118 driver");
  665. MODULE_LICENSE("GPL");