nct6775-i2c.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * nct6775-i2c - I2C driver for the hardware monitoring functionality of
  4. * Nuvoton NCT677x Super-I/O chips
  5. *
  6. * Copyright (C) 2022 Zev Weiss <[email protected]>
  7. *
  8. * This driver interacts with the chip via it's "back door" i2c interface, as
  9. * is often exposed to a BMC. Because the host may still be operating the
  10. * chip via the ("front door") LPC interface, this driver cannot assume that
  11. * it actually has full control of the chip, and in particular must avoid
  12. * making any changes that could confuse the host's LPC usage of it. It thus
  13. * operates in a strictly read-only fashion, with the only exception being the
  14. * bank-select register (which seems, thankfully, to be replicated for the i2c
  15. * interface so it doesn't affect the LPC interface).
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/i2c.h>
  20. #include <linux/hwmon.h>
  21. #include <linux/hwmon-sysfs.h>
  22. #include <linux/err.h>
  23. #include <linux/of_device.h>
  24. #include <linux/regmap.h>
  25. #include "nct6775.h"
  26. static int nct6775_i2c_read(void *ctx, unsigned int reg, unsigned int *val)
  27. {
  28. int ret;
  29. u32 tmp;
  30. u8 bank = reg >> 8;
  31. struct nct6775_data *data = ctx;
  32. struct i2c_client *client = data->driver_data;
  33. if (bank != data->bank) {
  34. ret = i2c_smbus_write_byte_data(client, NCT6775_REG_BANK, bank);
  35. if (ret)
  36. return ret;
  37. data->bank = bank;
  38. }
  39. ret = i2c_smbus_read_byte_data(client, reg & 0xff);
  40. if (ret < 0)
  41. return ret;
  42. tmp = ret;
  43. if (nct6775_reg_is_word_sized(data, reg)) {
  44. ret = i2c_smbus_read_byte_data(client, (reg & 0xff) + 1);
  45. if (ret < 0)
  46. return ret;
  47. tmp = (tmp << 8) | ret;
  48. }
  49. *val = tmp;
  50. return 0;
  51. }
  52. /*
  53. * The write operation is a dummy so as not to disturb anything being done
  54. * with the chip via LPC.
  55. */
  56. static int nct6775_i2c_write(void *ctx, unsigned int reg, unsigned int value)
  57. {
  58. struct nct6775_data *data = ctx;
  59. struct i2c_client *client = data->driver_data;
  60. dev_dbg(&client->dev, "skipping attempted write: %02x -> %03x\n", value, reg);
  61. /*
  62. * This is a lie, but writing anything but the bank-select register is
  63. * something this driver shouldn't be doing.
  64. */
  65. return 0;
  66. }
  67. static const struct of_device_id __maybe_unused nct6775_i2c_of_match[] = {
  68. { .compatible = "nuvoton,nct6106", .data = (void *)nct6106, },
  69. { .compatible = "nuvoton,nct6116", .data = (void *)nct6116, },
  70. { .compatible = "nuvoton,nct6775", .data = (void *)nct6775, },
  71. { .compatible = "nuvoton,nct6776", .data = (void *)nct6776, },
  72. { .compatible = "nuvoton,nct6779", .data = (void *)nct6779, },
  73. { .compatible = "nuvoton,nct6791", .data = (void *)nct6791, },
  74. { .compatible = "nuvoton,nct6792", .data = (void *)nct6792, },
  75. { .compatible = "nuvoton,nct6793", .data = (void *)nct6793, },
  76. { .compatible = "nuvoton,nct6795", .data = (void *)nct6795, },
  77. { .compatible = "nuvoton,nct6796", .data = (void *)nct6796, },
  78. { .compatible = "nuvoton,nct6797", .data = (void *)nct6797, },
  79. { .compatible = "nuvoton,nct6798", .data = (void *)nct6798, },
  80. { },
  81. };
  82. MODULE_DEVICE_TABLE(of, nct6775_i2c_of_match);
  83. static const struct i2c_device_id nct6775_i2c_id[] = {
  84. { "nct6106", nct6106 },
  85. { "nct6116", nct6116 },
  86. { "nct6775", nct6775 },
  87. { "nct6776", nct6776 },
  88. { "nct6779", nct6779 },
  89. { "nct6791", nct6791 },
  90. { "nct6792", nct6792 },
  91. { "nct6793", nct6793 },
  92. { "nct6795", nct6795 },
  93. { "nct6796", nct6796 },
  94. { "nct6797", nct6797 },
  95. { "nct6798", nct6798 },
  96. { }
  97. };
  98. MODULE_DEVICE_TABLE(i2c, nct6775_i2c_id);
  99. static int nct6775_i2c_probe_init(struct nct6775_data *data)
  100. {
  101. u32 tsi_channel_mask;
  102. struct i2c_client *client = data->driver_data;
  103. /*
  104. * The i2c interface doesn't provide access to the control registers
  105. * needed to determine the presence of other fans, but fans 1 and 2
  106. * are (in principle) always there.
  107. *
  108. * In practice this is perhaps a little silly, because the system
  109. * using this driver is mostly likely a BMC, and hence probably has
  110. * totally separate fan tachs & pwms of its own that are actually
  111. * controlling/monitoring the fans -- these are thus unlikely to be
  112. * doing anything actually useful.
  113. */
  114. data->has_fan = 0x03;
  115. data->has_fan_min = 0x03;
  116. data->has_pwm = 0x03;
  117. /*
  118. * Because on a BMC this driver may be bound very shortly after power
  119. * is first applied to the device, the automatic TSI channel detection
  120. * in nct6775_probe() (which has already been run at this point) may
  121. * not find anything if a channel hasn't yet produced a temperature
  122. * reading. Augment whatever was found via autodetection (if
  123. * anything) with the channels DT says should be active.
  124. */
  125. if (!of_property_read_u32(client->dev.of_node, "nuvoton,tsi-channel-mask",
  126. &tsi_channel_mask))
  127. data->have_tsi_temp |= tsi_channel_mask & GENMASK(NUM_TSI_TEMP - 1, 0);
  128. return 0;
  129. }
  130. static const struct regmap_config nct6775_i2c_regmap_config = {
  131. .reg_bits = 16,
  132. .val_bits = 16,
  133. .reg_read = nct6775_i2c_read,
  134. .reg_write = nct6775_i2c_write,
  135. };
  136. static int nct6775_i2c_probe(struct i2c_client *client)
  137. {
  138. struct nct6775_data *data;
  139. const struct of_device_id *of_id;
  140. const struct i2c_device_id *i2c_id;
  141. struct device *dev = &client->dev;
  142. of_id = of_match_device(nct6775_i2c_of_match, dev);
  143. i2c_id = i2c_match_id(nct6775_i2c_id, client);
  144. if (of_id && (unsigned long)of_id->data != i2c_id->driver_data)
  145. dev_notice(dev, "Device mismatch: %s in device tree, %s detected\n",
  146. of_id->name, i2c_id->name);
  147. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  148. if (!data)
  149. return -ENOMEM;
  150. data->kind = i2c_id->driver_data;
  151. data->read_only = true;
  152. data->driver_data = client;
  153. data->driver_init = nct6775_i2c_probe_init;
  154. return nct6775_probe(dev, data, &nct6775_i2c_regmap_config);
  155. }
  156. static struct i2c_driver nct6775_i2c_driver = {
  157. .class = I2C_CLASS_HWMON,
  158. .driver = {
  159. .name = "nct6775-i2c",
  160. .of_match_table = of_match_ptr(nct6775_i2c_of_match),
  161. },
  162. .probe_new = nct6775_i2c_probe,
  163. .id_table = nct6775_i2c_id,
  164. };
  165. module_i2c_driver(nct6775_i2c_driver);
  166. MODULE_AUTHOR("Zev Weiss <[email protected]>");
  167. MODULE_DESCRIPTION("I2C driver for NCT6775F and compatible chips");
  168. MODULE_LICENSE("GPL");
  169. MODULE_IMPORT_NS(HWMON_NCT6775);