ibm-cffps.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright 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/bitops.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/device.h>
  12. #include <linux/fs.h>
  13. #include <linux/i2c.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/leds.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/pmbus.h>
  19. #include "pmbus.h"
  20. #define CFFPS_FRU_CMD 0x9A
  21. #define CFFPS_PN_CMD 0x9B
  22. #define CFFPS_SN_CMD 0x9E
  23. #define CFFPS_CCIN_CMD 0xBD
  24. #define CFFPS_FW_CMD_START 0xFA
  25. #define CFFPS_FW_NUM_BYTES 4
  26. #define CFFPS_SYS_CONFIG_CMD 0xDA
  27. #define CFFPS_INPUT_HISTORY_CMD 0xD6
  28. #define CFFPS_INPUT_HISTORY_SIZE 100
  29. /* STATUS_MFR_SPECIFIC bits */
  30. #define CFFPS_MFR_FAN_FAULT BIT(0)
  31. #define CFFPS_MFR_THERMAL_FAULT BIT(1)
  32. #define CFFPS_MFR_OV_FAULT BIT(2)
  33. #define CFFPS_MFR_UV_FAULT BIT(3)
  34. #define CFFPS_MFR_PS_KILL BIT(4)
  35. #define CFFPS_MFR_OC_FAULT BIT(5)
  36. #define CFFPS_MFR_VAUX_FAULT BIT(6)
  37. #define CFFPS_MFR_CURRENT_SHARE_WARNING BIT(7)
  38. #define CFFPS_LED_BLINK BIT(0)
  39. #define CFFPS_LED_ON BIT(1)
  40. #define CFFPS_LED_OFF BIT(2)
  41. #define CFFPS_BLINK_RATE_MS 250
  42. enum {
  43. CFFPS_DEBUGFS_INPUT_HISTORY = 0,
  44. CFFPS_DEBUGFS_FRU,
  45. CFFPS_DEBUGFS_PN,
  46. CFFPS_DEBUGFS_SN,
  47. CFFPS_DEBUGFS_CCIN,
  48. CFFPS_DEBUGFS_FW,
  49. CFFPS_DEBUGFS_NUM_ENTRIES
  50. };
  51. struct ibm_cffps_input_history {
  52. struct mutex update_lock;
  53. unsigned long last_update;
  54. u8 byte_count;
  55. u8 data[CFFPS_INPUT_HISTORY_SIZE];
  56. };
  57. struct ibm_cffps {
  58. struct i2c_client *client;
  59. struct ibm_cffps_input_history input_history;
  60. int debugfs_entries[CFFPS_DEBUGFS_NUM_ENTRIES];
  61. char led_name[32];
  62. u8 led_state;
  63. struct led_classdev led;
  64. };
  65. #define to_psu(x, y) container_of((x), struct ibm_cffps, debugfs_entries[(y)])
  66. static ssize_t ibm_cffps_read_input_history(struct ibm_cffps *psu,
  67. char __user *buf, size_t count,
  68. loff_t *ppos)
  69. {
  70. int rc;
  71. u8 msgbuf0[1] = { CFFPS_INPUT_HISTORY_CMD };
  72. u8 msgbuf1[CFFPS_INPUT_HISTORY_SIZE + 1] = { 0 };
  73. struct i2c_msg msg[2] = {
  74. {
  75. .addr = psu->client->addr,
  76. .flags = psu->client->flags,
  77. .len = 1,
  78. .buf = msgbuf0,
  79. }, {
  80. .addr = psu->client->addr,
  81. .flags = psu->client->flags | I2C_M_RD,
  82. .len = CFFPS_INPUT_HISTORY_SIZE + 1,
  83. .buf = msgbuf1,
  84. },
  85. };
  86. if (!*ppos) {
  87. mutex_lock(&psu->input_history.update_lock);
  88. if (time_after(jiffies, psu->input_history.last_update + HZ)) {
  89. /*
  90. * Use a raw i2c transfer, since we need more bytes
  91. * than Linux I2C supports through smbus xfr (only 32).
  92. */
  93. rc = i2c_transfer(psu->client->adapter, msg, 2);
  94. if (rc < 0) {
  95. mutex_unlock(&psu->input_history.update_lock);
  96. return rc;
  97. }
  98. psu->input_history.byte_count = msgbuf1[0];
  99. memcpy(psu->input_history.data, &msgbuf1[1],
  100. CFFPS_INPUT_HISTORY_SIZE);
  101. psu->input_history.last_update = jiffies;
  102. }
  103. mutex_unlock(&psu->input_history.update_lock);
  104. }
  105. return simple_read_from_buffer(buf, count, ppos,
  106. psu->input_history.data,
  107. psu->input_history.byte_count);
  108. }
  109. static ssize_t ibm_cffps_debugfs_op(struct file *file, char __user *buf,
  110. size_t count, loff_t *ppos)
  111. {
  112. u8 cmd;
  113. int i, rc;
  114. int *idxp = file->private_data;
  115. int idx = *idxp;
  116. struct ibm_cffps *psu = to_psu(idxp, idx);
  117. char data[I2C_SMBUS_BLOCK_MAX] = { 0 };
  118. switch (idx) {
  119. case CFFPS_DEBUGFS_INPUT_HISTORY:
  120. return ibm_cffps_read_input_history(psu, buf, count, ppos);
  121. case CFFPS_DEBUGFS_FRU:
  122. cmd = CFFPS_FRU_CMD;
  123. break;
  124. case CFFPS_DEBUGFS_PN:
  125. cmd = CFFPS_PN_CMD;
  126. break;
  127. case CFFPS_DEBUGFS_SN:
  128. cmd = CFFPS_SN_CMD;
  129. break;
  130. case CFFPS_DEBUGFS_CCIN:
  131. rc = i2c_smbus_read_word_swapped(psu->client, CFFPS_CCIN_CMD);
  132. if (rc < 0)
  133. return rc;
  134. rc = snprintf(data, 5, "%04X", rc);
  135. goto done;
  136. case CFFPS_DEBUGFS_FW:
  137. for (i = 0; i < CFFPS_FW_NUM_BYTES; ++i) {
  138. rc = i2c_smbus_read_byte_data(psu->client,
  139. CFFPS_FW_CMD_START + i);
  140. if (rc < 0)
  141. return rc;
  142. snprintf(&data[i * 2], 3, "%02X", rc);
  143. }
  144. rc = i * 2;
  145. goto done;
  146. default:
  147. return -EINVAL;
  148. }
  149. rc = i2c_smbus_read_block_data(psu->client, cmd, data);
  150. if (rc < 0)
  151. return rc;
  152. done:
  153. data[rc] = '\n';
  154. rc += 2;
  155. return simple_read_from_buffer(buf, count, ppos, data, rc);
  156. }
  157. static const struct file_operations ibm_cffps_fops = {
  158. .llseek = noop_llseek,
  159. .read = ibm_cffps_debugfs_op,
  160. .open = simple_open,
  161. };
  162. static int ibm_cffps_read_byte_data(struct i2c_client *client, int page,
  163. int reg)
  164. {
  165. int rc, mfr;
  166. switch (reg) {
  167. case PMBUS_STATUS_VOUT:
  168. case PMBUS_STATUS_IOUT:
  169. case PMBUS_STATUS_TEMPERATURE:
  170. case PMBUS_STATUS_FAN_12:
  171. rc = pmbus_read_byte_data(client, page, reg);
  172. if (rc < 0)
  173. return rc;
  174. mfr = pmbus_read_byte_data(client, page,
  175. PMBUS_STATUS_MFR_SPECIFIC);
  176. if (mfr < 0)
  177. /*
  178. * Return the status register instead of an error,
  179. * since we successfully read status.
  180. */
  181. return rc;
  182. /* Add MFR_SPECIFIC bits to the standard pmbus status regs. */
  183. if (reg == PMBUS_STATUS_FAN_12) {
  184. if (mfr & CFFPS_MFR_FAN_FAULT)
  185. rc |= PB_FAN_FAN1_FAULT;
  186. } else if (reg == PMBUS_STATUS_TEMPERATURE) {
  187. if (mfr & CFFPS_MFR_THERMAL_FAULT)
  188. rc |= PB_TEMP_OT_FAULT;
  189. } else if (reg == PMBUS_STATUS_VOUT) {
  190. if (mfr & (CFFPS_MFR_OV_FAULT | CFFPS_MFR_VAUX_FAULT))
  191. rc |= PB_VOLTAGE_OV_FAULT;
  192. if (mfr & CFFPS_MFR_UV_FAULT)
  193. rc |= PB_VOLTAGE_UV_FAULT;
  194. } else if (reg == PMBUS_STATUS_IOUT) {
  195. if (mfr & CFFPS_MFR_OC_FAULT)
  196. rc |= PB_IOUT_OC_FAULT;
  197. if (mfr & CFFPS_MFR_CURRENT_SHARE_WARNING)
  198. rc |= PB_CURRENT_SHARE_FAULT;
  199. }
  200. break;
  201. default:
  202. rc = -ENODATA;
  203. break;
  204. }
  205. return rc;
  206. }
  207. static int ibm_cffps_read_word_data(struct i2c_client *client, int page,
  208. int reg)
  209. {
  210. int rc, mfr;
  211. switch (reg) {
  212. case PMBUS_STATUS_WORD:
  213. rc = pmbus_read_word_data(client, page, reg);
  214. if (rc < 0)
  215. return rc;
  216. mfr = pmbus_read_byte_data(client, page,
  217. PMBUS_STATUS_MFR_SPECIFIC);
  218. if (mfr < 0)
  219. /*
  220. * Return the status register instead of an error,
  221. * since we successfully read status.
  222. */
  223. return rc;
  224. if (mfr & CFFPS_MFR_PS_KILL)
  225. rc |= PB_STATUS_OFF;
  226. break;
  227. default:
  228. rc = -ENODATA;
  229. break;
  230. }
  231. return rc;
  232. }
  233. static int ibm_cffps_led_brightness_set(struct led_classdev *led_cdev,
  234. enum led_brightness brightness)
  235. {
  236. int rc;
  237. struct ibm_cffps *psu = container_of(led_cdev, struct ibm_cffps, led);
  238. if (brightness == LED_OFF) {
  239. psu->led_state = CFFPS_LED_OFF;
  240. } else {
  241. brightness = LED_FULL;
  242. if (psu->led_state != CFFPS_LED_BLINK)
  243. psu->led_state = CFFPS_LED_ON;
  244. }
  245. rc = i2c_smbus_write_byte_data(psu->client, CFFPS_SYS_CONFIG_CMD,
  246. psu->led_state);
  247. if (rc < 0)
  248. return rc;
  249. led_cdev->brightness = brightness;
  250. return 0;
  251. }
  252. static int ibm_cffps_led_blink_set(struct led_classdev *led_cdev,
  253. unsigned long *delay_on,
  254. unsigned long *delay_off)
  255. {
  256. int rc;
  257. struct ibm_cffps *psu = container_of(led_cdev, struct ibm_cffps, led);
  258. psu->led_state = CFFPS_LED_BLINK;
  259. if (led_cdev->brightness == LED_OFF)
  260. return 0;
  261. rc = i2c_smbus_write_byte_data(psu->client, CFFPS_SYS_CONFIG_CMD,
  262. CFFPS_LED_BLINK);
  263. if (rc < 0)
  264. return rc;
  265. *delay_on = CFFPS_BLINK_RATE_MS;
  266. *delay_off = CFFPS_BLINK_RATE_MS;
  267. return 0;
  268. }
  269. static void ibm_cffps_create_led_class(struct ibm_cffps *psu)
  270. {
  271. int rc;
  272. struct i2c_client *client = psu->client;
  273. struct device *dev = &client->dev;
  274. snprintf(psu->led_name, sizeof(psu->led_name), "%s-%02x", client->name,
  275. client->addr);
  276. psu->led.name = psu->led_name;
  277. psu->led.max_brightness = LED_FULL;
  278. psu->led.brightness_set_blocking = ibm_cffps_led_brightness_set;
  279. psu->led.blink_set = ibm_cffps_led_blink_set;
  280. rc = devm_led_classdev_register(dev, &psu->led);
  281. if (rc)
  282. dev_warn(dev, "failed to register led class: %d\n", rc);
  283. }
  284. static struct pmbus_driver_info ibm_cffps_info = {
  285. .pages = 1,
  286. .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
  287. PMBUS_HAVE_PIN | PMBUS_HAVE_FAN12 | PMBUS_HAVE_TEMP |
  288. PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3 | PMBUS_HAVE_STATUS_VOUT |
  289. PMBUS_HAVE_STATUS_IOUT | PMBUS_HAVE_STATUS_INPUT |
  290. PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_STATUS_FAN12,
  291. .read_byte_data = ibm_cffps_read_byte_data,
  292. .read_word_data = ibm_cffps_read_word_data,
  293. };
  294. static struct pmbus_platform_data ibm_cffps_pdata = {
  295. .flags = PMBUS_SKIP_STATUS_CHECK,
  296. };
  297. static int ibm_cffps_probe(struct i2c_client *client,
  298. const struct i2c_device_id *id)
  299. {
  300. int i, rc;
  301. struct dentry *debugfs;
  302. struct dentry *ibm_cffps_dir;
  303. struct ibm_cffps *psu;
  304. client->dev.platform_data = &ibm_cffps_pdata;
  305. rc = pmbus_do_probe(client, id, &ibm_cffps_info);
  306. if (rc)
  307. return rc;
  308. /*
  309. * Don't fail the probe if there isn't enough memory for leds and
  310. * debugfs.
  311. */
  312. psu = devm_kzalloc(&client->dev, sizeof(*psu), GFP_KERNEL);
  313. if (!psu)
  314. return 0;
  315. psu->client = client;
  316. mutex_init(&psu->input_history.update_lock);
  317. psu->input_history.last_update = jiffies - HZ;
  318. ibm_cffps_create_led_class(psu);
  319. /* Don't fail the probe if we can't create debugfs */
  320. debugfs = pmbus_get_debugfs_dir(client);
  321. if (!debugfs)
  322. return 0;
  323. ibm_cffps_dir = debugfs_create_dir(client->name, debugfs);
  324. if (!ibm_cffps_dir)
  325. return 0;
  326. for (i = 0; i < CFFPS_DEBUGFS_NUM_ENTRIES; ++i)
  327. psu->debugfs_entries[i] = i;
  328. debugfs_create_file("input_history", 0444, ibm_cffps_dir,
  329. &psu->debugfs_entries[CFFPS_DEBUGFS_INPUT_HISTORY],
  330. &ibm_cffps_fops);
  331. debugfs_create_file("fru", 0444, ibm_cffps_dir,
  332. &psu->debugfs_entries[CFFPS_DEBUGFS_FRU],
  333. &ibm_cffps_fops);
  334. debugfs_create_file("part_number", 0444, ibm_cffps_dir,
  335. &psu->debugfs_entries[CFFPS_DEBUGFS_PN],
  336. &ibm_cffps_fops);
  337. debugfs_create_file("serial_number", 0444, ibm_cffps_dir,
  338. &psu->debugfs_entries[CFFPS_DEBUGFS_SN],
  339. &ibm_cffps_fops);
  340. debugfs_create_file("ccin", 0444, ibm_cffps_dir,
  341. &psu->debugfs_entries[CFFPS_DEBUGFS_CCIN],
  342. &ibm_cffps_fops);
  343. debugfs_create_file("fw_version", 0444, ibm_cffps_dir,
  344. &psu->debugfs_entries[CFFPS_DEBUGFS_FW],
  345. &ibm_cffps_fops);
  346. return 0;
  347. }
  348. static const struct i2c_device_id ibm_cffps_id[] = {
  349. { "ibm_cffps1", 1 },
  350. {}
  351. };
  352. MODULE_DEVICE_TABLE(i2c, ibm_cffps_id);
  353. static const struct of_device_id ibm_cffps_of_match[] = {
  354. { .compatible = "ibm,cffps1" },
  355. {}
  356. };
  357. MODULE_DEVICE_TABLE(of, ibm_cffps_of_match);
  358. static struct i2c_driver ibm_cffps_driver = {
  359. .driver = {
  360. .name = "ibm-cffps",
  361. .of_match_table = ibm_cffps_of_match,
  362. },
  363. .probe = ibm_cffps_probe,
  364. .remove = pmbus_do_remove,
  365. .id_table = ibm_cffps_id,
  366. };
  367. module_i2c_driver(ibm_cffps_driver);
  368. MODULE_AUTHOR("Eddie James");
  369. MODULE_DESCRIPTION("PMBus driver for IBM Common Form Factor power supplies");
  370. MODULE_LICENSE("GPL");