max127.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Hardware monitoring driver for MAX127.
  4. *
  5. * Copyright (c) 2020 Facebook Inc.
  6. */
  7. #include <linux/err.h>
  8. #include <linux/hwmon.h>
  9. #include <linux/i2c.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. /*
  13. * MAX127 Control Byte. Refer to MAX127 datasheet, Table 1 "Control-Byte
  14. * Format" for details.
  15. */
  16. #define MAX127_CTRL_START BIT(7)
  17. #define MAX127_CTRL_SEL_SHIFT 4
  18. #define MAX127_CTRL_RNG BIT(3)
  19. #define MAX127_CTRL_BIP BIT(2)
  20. #define MAX127_CTRL_PD1 BIT(1)
  21. #define MAX127_CTRL_PD0 BIT(0)
  22. #define MAX127_NUM_CHANNELS 8
  23. #define MAX127_SET_CHANNEL(ch) (((ch) & 7) << MAX127_CTRL_SEL_SHIFT)
  24. /*
  25. * MAX127 channel input ranges. Refer to MAX127 datasheet, Table 3 "Range
  26. * and Polarity Selection" for details.
  27. */
  28. #define MAX127_FULL_RANGE 10000 /* 10V */
  29. #define MAX127_HALF_RANGE 5000 /* 5V */
  30. /*
  31. * MAX127 returns 2 bytes at read:
  32. * - the first byte contains data[11:4].
  33. * - the second byte contains data[3:0] (MSB) and 4 dummy 0s (LSB).
  34. * Refer to MAX127 datasheet, "Read a Conversion (Read Cycle)" section
  35. * for details.
  36. */
  37. #define MAX127_DATA_LEN 2
  38. #define MAX127_DATA_SHIFT 4
  39. #define MAX127_SIGN_BIT BIT(11)
  40. struct max127_data {
  41. struct i2c_client *client;
  42. u8 ctrl_byte[MAX127_NUM_CHANNELS];
  43. };
  44. static int max127_select_channel(struct i2c_client *client, u8 ctrl_byte)
  45. {
  46. int status;
  47. struct i2c_msg msg = {
  48. .addr = client->addr,
  49. .flags = 0,
  50. .len = sizeof(ctrl_byte),
  51. .buf = &ctrl_byte,
  52. };
  53. status = i2c_transfer(client->adapter, &msg, 1);
  54. if (status < 0)
  55. return status;
  56. if (status != 1)
  57. return -EIO;
  58. return 0;
  59. }
  60. static int max127_read_channel(struct i2c_client *client, long *val)
  61. {
  62. int status;
  63. u8 i2c_data[MAX127_DATA_LEN];
  64. struct i2c_msg msg = {
  65. .addr = client->addr,
  66. .flags = I2C_M_RD,
  67. .len = sizeof(i2c_data),
  68. .buf = i2c_data,
  69. };
  70. status = i2c_transfer(client->adapter, &msg, 1);
  71. if (status < 0)
  72. return status;
  73. if (status != 1)
  74. return -EIO;
  75. *val = (i2c_data[1] >> MAX127_DATA_SHIFT) |
  76. ((u16)i2c_data[0] << MAX127_DATA_SHIFT);
  77. return 0;
  78. }
  79. static long max127_process_raw(u8 ctrl_byte, long raw)
  80. {
  81. long scale, weight;
  82. /*
  83. * MAX127's data coding is binary in unipolar mode with 1 LSB =
  84. * (Full-Scale/4096) and two’s complement binary in bipolar mode
  85. * with 1 LSB = [(2 x |FS|)/4096].
  86. * Refer to MAX127 datasheet, "Transfer Function" section for
  87. * details.
  88. */
  89. scale = (ctrl_byte & MAX127_CTRL_RNG) ? MAX127_FULL_RANGE :
  90. MAX127_HALF_RANGE;
  91. if (ctrl_byte & MAX127_CTRL_BIP) {
  92. weight = (raw & MAX127_SIGN_BIT);
  93. raw &= ~MAX127_SIGN_BIT;
  94. raw -= weight;
  95. raw *= 2;
  96. }
  97. return raw * scale / 4096;
  98. }
  99. static int max127_read_input(struct max127_data *data, int channel, long *val)
  100. {
  101. long raw;
  102. int status;
  103. struct i2c_client *client = data->client;
  104. u8 ctrl_byte = data->ctrl_byte[channel];
  105. status = max127_select_channel(client, ctrl_byte);
  106. if (status)
  107. return status;
  108. status = max127_read_channel(client, &raw);
  109. if (status)
  110. return status;
  111. *val = max127_process_raw(ctrl_byte, raw);
  112. return 0;
  113. }
  114. static int max127_read_min(struct max127_data *data, int channel, long *val)
  115. {
  116. u8 rng_bip = (data->ctrl_byte[channel] >> 2) & 3;
  117. static const int min_input_map[4] = {
  118. 0, /* RNG=0, BIP=0 */
  119. -MAX127_HALF_RANGE, /* RNG=0, BIP=1 */
  120. 0, /* RNG=1, BIP=0 */
  121. -MAX127_FULL_RANGE, /* RNG=1, BIP=1 */
  122. };
  123. *val = min_input_map[rng_bip];
  124. return 0;
  125. }
  126. static int max127_read_max(struct max127_data *data, int channel, long *val)
  127. {
  128. u8 rng_bip = (data->ctrl_byte[channel] >> 2) & 3;
  129. static const int max_input_map[4] = {
  130. MAX127_HALF_RANGE, /* RNG=0, BIP=0 */
  131. MAX127_HALF_RANGE, /* RNG=0, BIP=1 */
  132. MAX127_FULL_RANGE, /* RNG=1, BIP=0 */
  133. MAX127_FULL_RANGE, /* RNG=1, BIP=1 */
  134. };
  135. *val = max_input_map[rng_bip];
  136. return 0;
  137. }
  138. static int max127_write_min(struct max127_data *data, int channel, long val)
  139. {
  140. u8 ctrl;
  141. ctrl = data->ctrl_byte[channel];
  142. if (val <= -MAX127_FULL_RANGE) {
  143. ctrl |= (MAX127_CTRL_RNG | MAX127_CTRL_BIP);
  144. } else if (val < 0) {
  145. ctrl |= MAX127_CTRL_BIP;
  146. ctrl &= ~MAX127_CTRL_RNG;
  147. } else {
  148. ctrl &= ~MAX127_CTRL_BIP;
  149. }
  150. data->ctrl_byte[channel] = ctrl;
  151. return 0;
  152. }
  153. static int max127_write_max(struct max127_data *data, int channel, long val)
  154. {
  155. if (val >= MAX127_FULL_RANGE)
  156. data->ctrl_byte[channel] |= MAX127_CTRL_RNG;
  157. else
  158. data->ctrl_byte[channel] &= ~MAX127_CTRL_RNG;
  159. return 0;
  160. }
  161. static umode_t max127_is_visible(const void *_data,
  162. enum hwmon_sensor_types type,
  163. u32 attr, int channel)
  164. {
  165. if (type == hwmon_in) {
  166. switch (attr) {
  167. case hwmon_in_input:
  168. return 0444;
  169. case hwmon_in_min:
  170. case hwmon_in_max:
  171. return 0644;
  172. default:
  173. break;
  174. }
  175. }
  176. return 0;
  177. }
  178. static int max127_read(struct device *dev, enum hwmon_sensor_types type,
  179. u32 attr, int channel, long *val)
  180. {
  181. int status;
  182. struct max127_data *data = dev_get_drvdata(dev);
  183. if (type != hwmon_in)
  184. return -EOPNOTSUPP;
  185. switch (attr) {
  186. case hwmon_in_input:
  187. status = max127_read_input(data, channel, val);
  188. break;
  189. case hwmon_in_min:
  190. status = max127_read_min(data, channel, val);
  191. break;
  192. case hwmon_in_max:
  193. status = max127_read_max(data, channel, val);
  194. break;
  195. default:
  196. status = -EOPNOTSUPP;
  197. break;
  198. }
  199. return status;
  200. }
  201. static int max127_write(struct device *dev, enum hwmon_sensor_types type,
  202. u32 attr, int channel, long val)
  203. {
  204. int status;
  205. struct max127_data *data = dev_get_drvdata(dev);
  206. if (type != hwmon_in)
  207. return -EOPNOTSUPP;
  208. switch (attr) {
  209. case hwmon_in_min:
  210. status = max127_write_min(data, channel, val);
  211. break;
  212. case hwmon_in_max:
  213. status = max127_write_max(data, channel, val);
  214. break;
  215. default:
  216. status = -EOPNOTSUPP;
  217. break;
  218. }
  219. return status;
  220. }
  221. static const struct hwmon_ops max127_hwmon_ops = {
  222. .is_visible = max127_is_visible,
  223. .read = max127_read,
  224. .write = max127_write,
  225. };
  226. static const struct hwmon_channel_info * const max127_info[] = {
  227. HWMON_CHANNEL_INFO(in,
  228. HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX,
  229. HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX,
  230. HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX,
  231. HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX,
  232. HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX,
  233. HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX,
  234. HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX,
  235. HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX),
  236. NULL,
  237. };
  238. static const struct hwmon_chip_info max127_chip_info = {
  239. .ops = &max127_hwmon_ops,
  240. .info = max127_info,
  241. };
  242. static int max127_probe(struct i2c_client *client)
  243. {
  244. int i;
  245. struct device *hwmon_dev;
  246. struct max127_data *data;
  247. struct device *dev = &client->dev;
  248. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  249. if (!data)
  250. return -ENOMEM;
  251. data->client = client;
  252. for (i = 0; i < ARRAY_SIZE(data->ctrl_byte); i++)
  253. data->ctrl_byte[i] = (MAX127_CTRL_START |
  254. MAX127_SET_CHANNEL(i));
  255. hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
  256. data,
  257. &max127_chip_info,
  258. NULL);
  259. return PTR_ERR_OR_ZERO(hwmon_dev);
  260. }
  261. static const struct i2c_device_id max127_id[] = {
  262. { "max127" },
  263. { }
  264. };
  265. MODULE_DEVICE_TABLE(i2c, max127_id);
  266. static struct i2c_driver max127_driver = {
  267. .driver = {
  268. .name = "max127",
  269. },
  270. .probe = max127_probe,
  271. .id_table = max127_id,
  272. };
  273. module_i2c_driver(max127_driver);
  274. MODULE_LICENSE("GPL");
  275. MODULE_AUTHOR("Mike Choi <mikechoi@fb.com>");
  276. MODULE_AUTHOR("Tao Ren <rentao.bupt@gmail.com>");
  277. MODULE_DESCRIPTION("MAX127 Hardware Monitoring driver");