2 * Copyright 2017 IBM Corp.
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.
10 #include <linux/bitops.h>
11 #include <linux/debugfs.h>
12 #include <linux/device.h>
14 #include <linux/i2c.h>
15 #include <linux/jiffies.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
21 #define CFFPS_FRU_CMD 0x9A
22 #define CFFPS_PN_CMD 0x9B
23 #define CFFPS_SN_CMD 0x9E
24 #define CFFPS_CCIN_CMD 0xBD
25 #define CFFPS_FW_CMD_START 0xFA
26 #define CFFPS_FW_NUM_BYTES 4
28 #define CFFPS_INPUT_HISTORY_CMD 0xD6
29 #define CFFPS_INPUT_HISTORY_SIZE 100
31 /* STATUS_MFR_SPECIFIC bits */
32 #define CFFPS_MFR_FAN_FAULT BIT(0)
33 #define CFFPS_MFR_THERMAL_FAULT BIT(1)
34 #define CFFPS_MFR_OV_FAULT BIT(2)
35 #define CFFPS_MFR_UV_FAULT BIT(3)
36 #define CFFPS_MFR_PS_KILL BIT(4)
37 #define CFFPS_MFR_OC_FAULT BIT(5)
38 #define CFFPS_MFR_VAUX_FAULT BIT(6)
39 #define CFFPS_MFR_CURRENT_SHARE_WARNING BIT(7)
42 CFFPS_DEBUGFS_INPUT_HISTORY = 0,
48 CFFPS_DEBUGFS_NUM_ENTRIES
51 struct ibm_cffps_input_history {
52 struct mutex update_lock;
53 unsigned long last_update;
56 u8 data[CFFPS_INPUT_HISTORY_SIZE];
60 struct i2c_client *client;
62 struct ibm_cffps_input_history input_history;
64 int debugfs_entries[CFFPS_DEBUGFS_NUM_ENTRIES];
67 #define to_psu(x, y) container_of((x), struct ibm_cffps, debugfs_entries[(y)])
69 static ssize_t ibm_cffps_read_input_history(struct ibm_cffps *psu,
70 char __user *buf, size_t count,
74 u8 msgbuf0[1] = { CFFPS_INPUT_HISTORY_CMD };
75 u8 msgbuf1[CFFPS_INPUT_HISTORY_SIZE + 1] = { 0 };
76 struct i2c_msg msg[2] = {
78 .addr = psu->client->addr,
79 .flags = psu->client->flags,
83 .addr = psu->client->addr,
84 .flags = psu->client->flags | I2C_M_RD,
85 .len = CFFPS_INPUT_HISTORY_SIZE + 1,
91 mutex_lock(&psu->input_history.update_lock);
92 if (time_after(jiffies, psu->input_history.last_update + HZ)) {
94 * Use a raw i2c transfer, since we need more bytes
95 * than Linux I2C supports through smbus xfr (only 32).
97 rc = i2c_transfer(psu->client->adapter, msg, 2);
99 mutex_unlock(&psu->input_history.update_lock);
103 psu->input_history.byte_count = msgbuf1[0];
104 memcpy(psu->input_history.data, &msgbuf1[1],
105 CFFPS_INPUT_HISTORY_SIZE);
106 psu->input_history.last_update = jiffies;
109 mutex_unlock(&psu->input_history.update_lock);
112 return simple_read_from_buffer(buf, count, ppos,
113 psu->input_history.data,
114 psu->input_history.byte_count);
117 static ssize_t ibm_cffps_debugfs_op(struct file *file, char __user *buf,
118 size_t count, loff_t *ppos)
122 int *idxp = file->private_data;
124 struct ibm_cffps *psu = to_psu(idxp, idx);
125 char data[I2C_SMBUS_BLOCK_MAX] = { 0 };
128 case CFFPS_DEBUGFS_INPUT_HISTORY:
129 return ibm_cffps_read_input_history(psu, buf, count, ppos);
130 case CFFPS_DEBUGFS_FRU:
133 case CFFPS_DEBUGFS_PN:
136 case CFFPS_DEBUGFS_SN:
139 case CFFPS_DEBUGFS_CCIN:
140 rc = i2c_smbus_read_word_swapped(psu->client, CFFPS_CCIN_CMD);
144 rc = snprintf(data, 5, "%04X", rc);
146 case CFFPS_DEBUGFS_FW:
147 for (i = 0; i < CFFPS_FW_NUM_BYTES; ++i) {
148 rc = i2c_smbus_read_byte_data(psu->client,
149 CFFPS_FW_CMD_START + i);
153 snprintf(&data[i * 2], 3, "%02X", rc);
162 rc = i2c_smbus_read_block_data(psu->client, cmd, data);
170 return simple_read_from_buffer(buf, count, ppos, data, rc);
173 static const struct file_operations ibm_cffps_fops = {
174 .llseek = noop_llseek,
175 .read = ibm_cffps_debugfs_op,
179 static int ibm_cffps_read_byte_data(struct i2c_client *client, int page,
185 case PMBUS_STATUS_VOUT:
186 case PMBUS_STATUS_IOUT:
187 case PMBUS_STATUS_TEMPERATURE:
188 case PMBUS_STATUS_FAN_12:
189 rc = pmbus_read_byte_data(client, page, reg);
193 mfr = pmbus_read_byte_data(client, page,
194 PMBUS_STATUS_MFR_SPECIFIC);
197 * Return the status register instead of an error,
198 * since we successfully read status.
202 /* Add MFR_SPECIFIC bits to the standard pmbus status regs. */
203 if (reg == PMBUS_STATUS_FAN_12) {
204 if (mfr & CFFPS_MFR_FAN_FAULT)
205 rc |= PB_FAN_FAN1_FAULT;
206 } else if (reg == PMBUS_STATUS_TEMPERATURE) {
207 if (mfr & CFFPS_MFR_THERMAL_FAULT)
208 rc |= PB_TEMP_OT_FAULT;
209 } else if (reg == PMBUS_STATUS_VOUT) {
210 if (mfr & (CFFPS_MFR_OV_FAULT | CFFPS_MFR_VAUX_FAULT))
211 rc |= PB_VOLTAGE_OV_FAULT;
212 if (mfr & CFFPS_MFR_UV_FAULT)
213 rc |= PB_VOLTAGE_UV_FAULT;
214 } else if (reg == PMBUS_STATUS_IOUT) {
215 if (mfr & CFFPS_MFR_OC_FAULT)
216 rc |= PB_IOUT_OC_FAULT;
217 if (mfr & CFFPS_MFR_CURRENT_SHARE_WARNING)
218 rc |= PB_CURRENT_SHARE_FAULT;
229 static int ibm_cffps_read_word_data(struct i2c_client *client, int page,
235 case PMBUS_STATUS_WORD:
236 rc = pmbus_read_word_data(client, page, reg);
240 mfr = pmbus_read_byte_data(client, page,
241 PMBUS_STATUS_MFR_SPECIFIC);
244 * Return the status register instead of an error,
245 * since we successfully read status.
249 if (mfr & CFFPS_MFR_PS_KILL)
260 static struct pmbus_driver_info ibm_cffps_info = {
262 .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
263 PMBUS_HAVE_PIN | PMBUS_HAVE_FAN12 | PMBUS_HAVE_TEMP |
264 PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3 | PMBUS_HAVE_STATUS_VOUT |
265 PMBUS_HAVE_STATUS_IOUT | PMBUS_HAVE_STATUS_INPUT |
266 PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_STATUS_FAN12,
267 .read_byte_data = ibm_cffps_read_byte_data,
268 .read_word_data = ibm_cffps_read_word_data,
271 static int ibm_cffps_probe(struct i2c_client *client,
272 const struct i2c_device_id *id)
275 struct dentry *debugfs;
276 struct dentry *ibm_cffps_dir;
277 struct ibm_cffps *psu;
279 rc = pmbus_do_probe(client, id, &ibm_cffps_info);
283 /* Don't fail the probe if we can't create debugfs */
284 debugfs = pmbus_get_debugfs_dir(client);
288 ibm_cffps_dir = debugfs_create_dir(client->name, debugfs);
292 psu = devm_kzalloc(&client->dev, sizeof(*psu), GFP_KERNEL);
296 psu->client = client;
297 mutex_init(&psu->input_history.update_lock);
298 psu->input_history.last_update = jiffies - HZ;
300 for (i = 0; i < CFFPS_DEBUGFS_NUM_ENTRIES; ++i)
301 psu->debugfs_entries[i] = i;
303 debugfs_create_file("input_history", 0444, ibm_cffps_dir,
304 &psu->debugfs_entries[CFFPS_DEBUGFS_INPUT_HISTORY],
306 debugfs_create_file("fru", 0444, ibm_cffps_dir,
307 &psu->debugfs_entries[CFFPS_DEBUGFS_FRU],
309 debugfs_create_file("part_number", 0444, ibm_cffps_dir,
310 &psu->debugfs_entries[CFFPS_DEBUGFS_PN],
312 debugfs_create_file("serial_number", 0444, ibm_cffps_dir,
313 &psu->debugfs_entries[CFFPS_DEBUGFS_SN],
315 debugfs_create_file("ccin", 0444, ibm_cffps_dir,
316 &psu->debugfs_entries[CFFPS_DEBUGFS_CCIN],
318 debugfs_create_file("fw_version", 0444, ibm_cffps_dir,
319 &psu->debugfs_entries[CFFPS_DEBUGFS_FW],
325 static const struct i2c_device_id ibm_cffps_id[] = {
329 MODULE_DEVICE_TABLE(i2c, ibm_cffps_id);
331 static const struct of_device_id ibm_cffps_of_match[] = {
332 { .compatible = "ibm,cffps1" },
335 MODULE_DEVICE_TABLE(of, ibm_cffps_of_match);
337 static struct i2c_driver ibm_cffps_driver = {
340 .of_match_table = ibm_cffps_of_match,
342 .probe = ibm_cffps_probe,
343 .remove = pmbus_do_remove,
344 .id_table = ibm_cffps_id,
347 module_i2c_driver(ibm_cffps_driver);
349 MODULE_AUTHOR("Eddie James");
350 MODULE_DESCRIPTION("PMBus driver for IBM Common Form Factor power supplies");
351 MODULE_LICENSE("GPL");