max15301.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Hardware monitoring driver for Maxim MAX15301
  4. *
  5. * Copyright (c) 2021 Flextronics International Sweden AB
  6. *
  7. * Even though the specification does not specifically mention it,
  8. * extensive empirical testing has revealed that auto-detection of
  9. * limit-registers will fail in a random fashion unless the delay
  10. * parameter is set to above about 80us. The default delay is set
  11. * to 100us to include some safety margin.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/err.h>
  17. #include <linux/slab.h>
  18. #include <linux/i2c.h>
  19. #include <linux/ktime.h>
  20. #include <linux/delay.h>
  21. #include <linux/pmbus.h>
  22. #include "pmbus.h"
  23. static const struct i2c_device_id max15301_id[] = {
  24. {"bmr461", 0},
  25. {"max15301", 0},
  26. {}
  27. };
  28. MODULE_DEVICE_TABLE(i2c, max15301_id);
  29. struct max15301_data {
  30. int id;
  31. ktime_t access; /* Chip access time */
  32. int delay; /* Delay between chip accesses in us */
  33. struct pmbus_driver_info info;
  34. };
  35. #define to_max15301_data(x) container_of(x, struct max15301_data, info)
  36. #define MAX15301_WAIT_TIME 100 /* us */
  37. static ushort delay = MAX15301_WAIT_TIME;
  38. module_param(delay, ushort, 0644);
  39. MODULE_PARM_DESC(delay, "Delay between chip accesses in us");
  40. static struct max15301_data max15301_data = {
  41. .info = {
  42. .pages = 1,
  43. .func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  44. | PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
  45. | PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2
  46. | PMBUS_HAVE_STATUS_TEMP
  47. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  48. }
  49. };
  50. /* This chip needs a delay between accesses */
  51. static inline void max15301_wait(const struct max15301_data *data)
  52. {
  53. if (data->delay) {
  54. s64 delta = ktime_us_delta(ktime_get(), data->access);
  55. if (delta < data->delay)
  56. udelay(data->delay - delta);
  57. }
  58. }
  59. static int max15301_read_word_data(struct i2c_client *client, int page,
  60. int phase, int reg)
  61. {
  62. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  63. struct max15301_data *data = to_max15301_data(info);
  64. int ret;
  65. if (page > 0)
  66. return -ENXIO;
  67. if (reg >= PMBUS_VIRT_BASE)
  68. return -ENXIO;
  69. max15301_wait(data);
  70. ret = pmbus_read_word_data(client, page, phase, reg);
  71. data->access = ktime_get();
  72. return ret;
  73. }
  74. static int max15301_read_byte_data(struct i2c_client *client, int page, int reg)
  75. {
  76. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  77. struct max15301_data *data = to_max15301_data(info);
  78. int ret;
  79. if (page > 0)
  80. return -ENXIO;
  81. max15301_wait(data);
  82. ret = pmbus_read_byte_data(client, page, reg);
  83. data->access = ktime_get();
  84. return ret;
  85. }
  86. static int max15301_write_word_data(struct i2c_client *client, int page, int reg,
  87. u16 word)
  88. {
  89. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  90. struct max15301_data *data = to_max15301_data(info);
  91. int ret;
  92. if (page > 0)
  93. return -ENXIO;
  94. if (reg >= PMBUS_VIRT_BASE)
  95. return -ENXIO;
  96. max15301_wait(data);
  97. ret = pmbus_write_word_data(client, page, reg, word);
  98. data->access = ktime_get();
  99. return ret;
  100. }
  101. static int max15301_write_byte(struct i2c_client *client, int page, u8 value)
  102. {
  103. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  104. struct max15301_data *data = to_max15301_data(info);
  105. int ret;
  106. if (page > 0)
  107. return -ENXIO;
  108. max15301_wait(data);
  109. ret = pmbus_write_byte(client, page, value);
  110. data->access = ktime_get();
  111. return ret;
  112. }
  113. static int max15301_probe(struct i2c_client *client)
  114. {
  115. int status;
  116. u8 device_id[I2C_SMBUS_BLOCK_MAX + 1];
  117. const struct i2c_device_id *mid;
  118. struct pmbus_driver_info *info = &max15301_data.info;
  119. if (!i2c_check_functionality(client->adapter,
  120. I2C_FUNC_SMBUS_READ_BYTE_DATA
  121. | I2C_FUNC_SMBUS_BLOCK_DATA))
  122. return -ENODEV;
  123. status = i2c_smbus_read_block_data(client, PMBUS_IC_DEVICE_ID, device_id);
  124. if (status < 0) {
  125. dev_err(&client->dev, "Failed to read Device Id\n");
  126. return status;
  127. }
  128. for (mid = max15301_id; mid->name[0]; mid++) {
  129. if (!strncasecmp(mid->name, device_id, strlen(mid->name)))
  130. break;
  131. }
  132. if (!mid->name[0]) {
  133. dev_err(&client->dev, "Unsupported device\n");
  134. return -ENODEV;
  135. }
  136. max15301_data.delay = delay;
  137. info->read_byte_data = max15301_read_byte_data;
  138. info->read_word_data = max15301_read_word_data;
  139. info->write_byte = max15301_write_byte;
  140. info->write_word_data = max15301_write_word_data;
  141. return pmbus_do_probe(client, info);
  142. }
  143. static struct i2c_driver max15301_driver = {
  144. .driver = {
  145. .name = "max15301",
  146. },
  147. .probe_new = max15301_probe,
  148. .id_table = max15301_id,
  149. };
  150. module_i2c_driver(max15301_driver);
  151. MODULE_AUTHOR("Erik Rosen <[email protected]>");
  152. MODULE_DESCRIPTION("PMBus driver for Maxim MAX15301");
  153. MODULE_LICENSE("GPL");
  154. MODULE_IMPORT_NS(PMBUS);