max31785.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright (C) 2017 IBM Corp.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/err.h>
  13. #include <linux/i2c.h>
  14. #include "pmbus.h"
  15. enum max31785_regs {
  16. MFR_REVISION = 0x9b,
  17. MFR_FAN_CONFIG = 0xf1,
  18. };
  19. #define MAX31785 0x3030
  20. #define MAX31785A 0x3040
  21. #define MFR_FAN_CONFIG_DUAL_TACH BIT(12)
  22. #define MAX31785_NR_PAGES 23
  23. #define MAX31785_NR_FAN_PAGES 6
  24. static int max31785_read_byte_data(struct i2c_client *client, int page,
  25. int reg)
  26. {
  27. if (page < MAX31785_NR_PAGES)
  28. return -ENODATA;
  29. switch (reg) {
  30. case PMBUS_VOUT_MODE:
  31. return -ENOTSUPP;
  32. case PMBUS_FAN_CONFIG_12:
  33. return pmbus_read_byte_data(client, page - MAX31785_NR_PAGES,
  34. reg);
  35. }
  36. return -ENODATA;
  37. }
  38. static int max31785_write_byte(struct i2c_client *client, int page, u8 value)
  39. {
  40. if (page < MAX31785_NR_PAGES)
  41. return -ENODATA;
  42. return -ENOTSUPP;
  43. }
  44. static int max31785_read_long_data(struct i2c_client *client, int page,
  45. int reg, u32 *data)
  46. {
  47. unsigned char cmdbuf[1];
  48. unsigned char rspbuf[4];
  49. int rc;
  50. struct i2c_msg msg[2] = {
  51. {
  52. .addr = client->addr,
  53. .flags = 0,
  54. .len = sizeof(cmdbuf),
  55. .buf = cmdbuf,
  56. },
  57. {
  58. .addr = client->addr,
  59. .flags = I2C_M_RD,
  60. .len = sizeof(rspbuf),
  61. .buf = rspbuf,
  62. },
  63. };
  64. cmdbuf[0] = reg;
  65. rc = pmbus_set_page(client, page);
  66. if (rc < 0)
  67. return rc;
  68. rc = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  69. if (rc < 0)
  70. return rc;
  71. *data = (rspbuf[0] << (0 * 8)) | (rspbuf[1] << (1 * 8)) |
  72. (rspbuf[2] << (2 * 8)) | (rspbuf[3] << (3 * 8));
  73. return rc;
  74. }
  75. static int max31785_get_pwm(struct i2c_client *client, int page)
  76. {
  77. int rv;
  78. rv = pmbus_get_fan_rate_device(client, page, 0, percent);
  79. if (rv < 0)
  80. return rv;
  81. else if (rv >= 0x8000)
  82. return 0;
  83. else if (rv >= 0x2711)
  84. return 0x2710;
  85. return rv;
  86. }
  87. static int max31785_get_pwm_mode(struct i2c_client *client, int page)
  88. {
  89. int config;
  90. int command;
  91. config = pmbus_read_byte_data(client, page, PMBUS_FAN_CONFIG_12);
  92. if (config < 0)
  93. return config;
  94. command = pmbus_read_word_data(client, page, PMBUS_FAN_COMMAND_1);
  95. if (command < 0)
  96. return command;
  97. if (config & PB_FAN_1_RPM)
  98. return (command >= 0x8000) ? 3 : 2;
  99. if (command >= 0x8000)
  100. return 3;
  101. else if (command >= 0x2711)
  102. return 0;
  103. return 1;
  104. }
  105. static int max31785_read_word_data(struct i2c_client *client, int page,
  106. int reg)
  107. {
  108. u32 val;
  109. int rv;
  110. switch (reg) {
  111. case PMBUS_READ_FAN_SPEED_1:
  112. if (page < MAX31785_NR_PAGES)
  113. return -ENODATA;
  114. rv = max31785_read_long_data(client, page - MAX31785_NR_PAGES,
  115. reg, &val);
  116. if (rv < 0)
  117. return rv;
  118. rv = (val >> 16) & 0xffff;
  119. break;
  120. case PMBUS_FAN_COMMAND_1:
  121. /*
  122. * PMBUS_FAN_COMMAND_x is probed to judge whether or not to
  123. * expose fan control registers.
  124. *
  125. * Don't expose fan_target attribute for virtual pages.
  126. */
  127. rv = (page >= MAX31785_NR_PAGES) ? -ENOTSUPP : -ENODATA;
  128. break;
  129. case PMBUS_VIRT_PWM_1:
  130. rv = max31785_get_pwm(client, page);
  131. break;
  132. case PMBUS_VIRT_PWM_ENABLE_1:
  133. rv = max31785_get_pwm_mode(client, page);
  134. break;
  135. default:
  136. rv = -ENODATA;
  137. break;
  138. }
  139. return rv;
  140. }
  141. static inline u32 max31785_scale_pwm(u32 sensor_val)
  142. {
  143. /*
  144. * The datasheet describes the accepted value range for manual PWM as
  145. * [0, 0x2710], while the hwmon pwmX sysfs interface accepts values in
  146. * [0, 255]. The MAX31785 uses DIRECT mode to scale the FAN_COMMAND
  147. * registers and in PWM mode the coefficients are m=1, b=0, R=2. The
  148. * important observation here is that 0x2710 == 10000 == 100 * 100.
  149. *
  150. * R=2 (== 10^2 == 100) accounts for scaling the value provided at the
  151. * sysfs interface into the required hardware resolution, but it does
  152. * not yet yield a value that we can write to the device (this initial
  153. * scaling is handled by pmbus_data2reg()). Multiplying by 100 below
  154. * translates the parameter value into the percentage units required by
  155. * PMBus, and then we scale back by 255 as required by the hwmon pwmX
  156. * interface to yield the percentage value at the appropriate
  157. * resolution for hardware.
  158. */
  159. return (sensor_val * 100) / 255;
  160. }
  161. static int max31785_pwm_enable(struct i2c_client *client, int page,
  162. u16 word)
  163. {
  164. int config = 0;
  165. int rate;
  166. switch (word) {
  167. case 0:
  168. rate = 0x7fff;
  169. break;
  170. case 1:
  171. rate = pmbus_get_fan_rate_cached(client, page, 0, percent);
  172. if (rate < 0)
  173. return rate;
  174. rate = max31785_scale_pwm(rate);
  175. break;
  176. case 2:
  177. config = PB_FAN_1_RPM;
  178. rate = pmbus_get_fan_rate_cached(client, page, 0, rpm);
  179. if (rate < 0)
  180. return rate;
  181. break;
  182. case 3:
  183. rate = 0xffff;
  184. break;
  185. default:
  186. return -EINVAL;
  187. }
  188. return pmbus_update_fan(client, page, 0, config, PB_FAN_1_RPM, rate);
  189. }
  190. static int max31785_write_word_data(struct i2c_client *client, int page,
  191. int reg, u16 word)
  192. {
  193. switch (reg) {
  194. case PMBUS_VIRT_PWM_1:
  195. return pmbus_update_fan(client, page, 0, 0, PB_FAN_1_RPM,
  196. max31785_scale_pwm(word));
  197. case PMBUS_VIRT_PWM_ENABLE_1:
  198. return max31785_pwm_enable(client, page, word);
  199. default:
  200. break;
  201. }
  202. return -ENODATA;
  203. }
  204. #define MAX31785_FAN_FUNCS \
  205. (PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12 | PMBUS_HAVE_PWM12)
  206. #define MAX31785_TEMP_FUNCS \
  207. (PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP)
  208. #define MAX31785_VOUT_FUNCS \
  209. (PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT)
  210. #define MAX37185_NUM_FAN_PAGES 6
  211. static const struct pmbus_driver_info max31785_info = {
  212. .pages = MAX31785_NR_PAGES,
  213. .write_word_data = max31785_write_word_data,
  214. .read_byte_data = max31785_read_byte_data,
  215. .read_word_data = max31785_read_word_data,
  216. .write_byte = max31785_write_byte,
  217. /* RPM */
  218. .format[PSC_FAN] = direct,
  219. .m[PSC_FAN] = 1,
  220. .b[PSC_FAN] = 0,
  221. .R[PSC_FAN] = 0,
  222. /* PWM */
  223. .format[PSC_PWM] = direct,
  224. .m[PSC_PWM] = 1,
  225. .b[PSC_PWM] = 0,
  226. .R[PSC_PWM] = 2,
  227. .func[0] = MAX31785_FAN_FUNCS,
  228. .func[1] = MAX31785_FAN_FUNCS,
  229. .func[2] = MAX31785_FAN_FUNCS,
  230. .func[3] = MAX31785_FAN_FUNCS,
  231. .func[4] = MAX31785_FAN_FUNCS,
  232. .func[5] = MAX31785_FAN_FUNCS,
  233. .format[PSC_TEMPERATURE] = direct,
  234. .m[PSC_TEMPERATURE] = 1,
  235. .b[PSC_TEMPERATURE] = 0,
  236. .R[PSC_TEMPERATURE] = 2,
  237. .func[6] = MAX31785_TEMP_FUNCS,
  238. .func[7] = MAX31785_TEMP_FUNCS,
  239. .func[8] = MAX31785_TEMP_FUNCS,
  240. .func[9] = MAX31785_TEMP_FUNCS,
  241. .func[10] = MAX31785_TEMP_FUNCS,
  242. .func[11] = MAX31785_TEMP_FUNCS,
  243. .func[12] = MAX31785_TEMP_FUNCS,
  244. .func[13] = MAX31785_TEMP_FUNCS,
  245. .func[14] = MAX31785_TEMP_FUNCS,
  246. .func[15] = MAX31785_TEMP_FUNCS,
  247. .func[16] = MAX31785_TEMP_FUNCS,
  248. .format[PSC_VOLTAGE_OUT] = direct,
  249. .m[PSC_VOLTAGE_OUT] = 1,
  250. .b[PSC_VOLTAGE_OUT] = 0,
  251. .R[PSC_VOLTAGE_OUT] = 0,
  252. .func[17] = MAX31785_VOUT_FUNCS,
  253. .func[18] = MAX31785_VOUT_FUNCS,
  254. .func[19] = MAX31785_VOUT_FUNCS,
  255. .func[20] = MAX31785_VOUT_FUNCS,
  256. .func[21] = MAX31785_VOUT_FUNCS,
  257. .func[22] = MAX31785_VOUT_FUNCS,
  258. };
  259. static int max31785_configure_dual_tach(struct i2c_client *client,
  260. struct pmbus_driver_info *info)
  261. {
  262. int ret;
  263. int i;
  264. for (i = 0; i < MAX31785_NR_FAN_PAGES; i++) {
  265. ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
  266. if (ret < 0)
  267. return ret;
  268. ret = i2c_smbus_read_word_data(client, MFR_FAN_CONFIG);
  269. if (ret < 0)
  270. return ret;
  271. if (ret & MFR_FAN_CONFIG_DUAL_TACH) {
  272. int virtual = MAX31785_NR_PAGES + i;
  273. info->pages = virtual + 1;
  274. info->func[virtual] |= PMBUS_HAVE_FAN12;
  275. info->func[virtual] |= PMBUS_PAGE_VIRTUAL;
  276. }
  277. }
  278. return 0;
  279. }
  280. static int max31785_probe(struct i2c_client *client,
  281. const struct i2c_device_id *id)
  282. {
  283. struct device *dev = &client->dev;
  284. struct pmbus_driver_info *info;
  285. bool dual_tach = false;
  286. s64 ret;
  287. if (!i2c_check_functionality(client->adapter,
  288. I2C_FUNC_SMBUS_BYTE_DATA |
  289. I2C_FUNC_SMBUS_WORD_DATA))
  290. return -ENODEV;
  291. info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), GFP_KERNEL);
  292. if (!info)
  293. return -ENOMEM;
  294. *info = max31785_info;
  295. ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 255);
  296. if (ret < 0)
  297. return ret;
  298. ret = i2c_smbus_read_word_data(client, MFR_REVISION);
  299. if (ret < 0)
  300. return ret;
  301. if (ret == MAX31785A) {
  302. dual_tach = true;
  303. } else if (ret == MAX31785) {
  304. if (!strcmp("max31785a", id->name))
  305. dev_warn(dev, "Expected max3175a, found max31785: cannot provide secondary tachometer readings\n");
  306. } else {
  307. return -ENODEV;
  308. }
  309. if (dual_tach) {
  310. ret = max31785_configure_dual_tach(client, info);
  311. if (ret < 0)
  312. return ret;
  313. }
  314. return pmbus_do_probe(client, id, info);
  315. }
  316. static const struct i2c_device_id max31785_id[] = {
  317. { "max31785", 0 },
  318. { "max31785a", 0 },
  319. { },
  320. };
  321. MODULE_DEVICE_TABLE(i2c, max31785_id);
  322. static const struct of_device_id max31785_of_match[] = {
  323. { .compatible = "maxim,max31785" },
  324. { .compatible = "maxim,max31785a" },
  325. { },
  326. };
  327. MODULE_DEVICE_TABLE(of, max31785_of_match);
  328. static struct i2c_driver max31785_driver = {
  329. .driver = {
  330. .name = "max31785",
  331. .of_match_table = max31785_of_match,
  332. },
  333. .probe = max31785_probe,
  334. .remove = pmbus_do_remove,
  335. .id_table = max31785_id,
  336. };
  337. module_i2c_driver(max31785_driver);
  338. MODULE_AUTHOR("Andrew Jeffery <andrew@aj.id.au>");
  339. MODULE_DESCRIPTION("PMBus driver for the Maxim MAX31785");
  340. MODULE_LICENSE("GPL");