1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2017 IBM Corp.
6 #include <linux/bitops.h>
7 #include <linux/debugfs.h>
8 #include <linux/device.h>
10 #include <linux/i2c.h>
11 #include <linux/jiffies.h>
12 #include <linux/leds.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/of_device.h>
16 #include <linux/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 0xFA
25 #define CFFPS1_FW_NUM_BYTES 4
26 #define CFFPS2_FW_NUM_WORDS 3
27 #define CFFPS_SYS_CONFIG_CMD 0xDA
29 #define CFFPS_INPUT_HISTORY_CMD 0xD6
30 #define CFFPS_INPUT_HISTORY_SIZE 100
32 /* STATUS_MFR_SPECIFIC bits */
33 #define CFFPS_MFR_FAN_FAULT BIT(0)
34 #define CFFPS_MFR_THERMAL_FAULT BIT(1)
35 #define CFFPS_MFR_OV_FAULT BIT(2)
36 #define CFFPS_MFR_UV_FAULT BIT(3)
37 #define CFFPS_MFR_PS_KILL BIT(4)
38 #define CFFPS_MFR_OC_FAULT BIT(5)
39 #define CFFPS_MFR_VAUX_FAULT BIT(6)
40 #define CFFPS_MFR_CURRENT_SHARE_WARNING BIT(7)
42 #define CFFPS_LED_BLINK BIT(0)
43 #define CFFPS_LED_ON BIT(1)
44 #define CFFPS_LED_OFF BIT(2)
45 #define CFFPS_BLINK_RATE_MS 250
48 CFFPS_DEBUGFS_INPUT_HISTORY = 0,
54 CFFPS_DEBUGFS_NUM_ENTRIES
57 enum versions { cffps1, cffps2 };
59 struct ibm_cffps_input_history {
60 struct mutex update_lock;
61 unsigned long last_update;
64 u8 data[CFFPS_INPUT_HISTORY_SIZE];
68 enum versions version;
69 struct i2c_client *client;
71 struct ibm_cffps_input_history input_history;
73 int debugfs_entries[CFFPS_DEBUGFS_NUM_ENTRIES];
77 struct led_classdev led;
80 #define to_psu(x, y) container_of((x), struct ibm_cffps, debugfs_entries[(y)])
82 static ssize_t ibm_cffps_read_input_history(struct ibm_cffps *psu,
83 char __user *buf, size_t count,
87 u8 msgbuf0[1] = { CFFPS_INPUT_HISTORY_CMD };
88 u8 msgbuf1[CFFPS_INPUT_HISTORY_SIZE + 1] = { 0 };
89 struct i2c_msg msg[2] = {
91 .addr = psu->client->addr,
92 .flags = psu->client->flags,
96 .addr = psu->client->addr,
97 .flags = psu->client->flags | I2C_M_RD,
98 .len = CFFPS_INPUT_HISTORY_SIZE + 1,
104 mutex_lock(&psu->input_history.update_lock);
105 if (time_after(jiffies, psu->input_history.last_update + HZ)) {
107 * Use a raw i2c transfer, since we need more bytes
108 * than Linux I2C supports through smbus xfr (only 32).
110 rc = i2c_transfer(psu->client->adapter, msg, 2);
112 mutex_unlock(&psu->input_history.update_lock);
116 psu->input_history.byte_count = msgbuf1[0];
117 memcpy(psu->input_history.data, &msgbuf1[1],
118 CFFPS_INPUT_HISTORY_SIZE);
119 psu->input_history.last_update = jiffies;
122 mutex_unlock(&psu->input_history.update_lock);
125 return simple_read_from_buffer(buf, count, ppos,
126 psu->input_history.data,
127 psu->input_history.byte_count);
130 static ssize_t ibm_cffps_debugfs_op(struct file *file, char __user *buf,
131 size_t count, loff_t *ppos)
135 int *idxp = file->private_data;
137 struct ibm_cffps *psu = to_psu(idxp, idx);
138 char data[I2C_SMBUS_BLOCK_MAX] = { 0 };
140 pmbus_set_page(psu->client, 0);
143 case CFFPS_DEBUGFS_INPUT_HISTORY:
144 return ibm_cffps_read_input_history(psu, buf, count, ppos);
145 case CFFPS_DEBUGFS_FRU:
148 case CFFPS_DEBUGFS_PN:
151 case CFFPS_DEBUGFS_SN:
154 case CFFPS_DEBUGFS_CCIN:
155 rc = i2c_smbus_read_word_swapped(psu->client, CFFPS_CCIN_CMD);
159 rc = snprintf(data, 5, "%04X", rc);
161 case CFFPS_DEBUGFS_FW:
162 switch (psu->version) {
164 for (i = 0; i < CFFPS1_FW_NUM_BYTES; ++i) {
165 rc = i2c_smbus_read_byte_data(psu->client,
171 snprintf(&data[i * 2], 3, "%02X", rc);
177 for (i = 0; i < CFFPS2_FW_NUM_WORDS; ++i) {
178 rc = i2c_smbus_read_word_data(psu->client,
184 snprintf(&data[i * 4], 5, "%04X", rc);
197 rc = i2c_smbus_read_block_data(psu->client, cmd, data);
205 return simple_read_from_buffer(buf, count, ppos, data, rc);
208 static const struct file_operations ibm_cffps_fops = {
209 .llseek = noop_llseek,
210 .read = ibm_cffps_debugfs_op,
214 static int ibm_cffps_read_byte_data(struct i2c_client *client, int page,
220 case PMBUS_STATUS_VOUT:
221 case PMBUS_STATUS_IOUT:
222 case PMBUS_STATUS_TEMPERATURE:
223 case PMBUS_STATUS_FAN_12:
224 rc = pmbus_read_byte_data(client, page, reg);
228 mfr = pmbus_read_byte_data(client, page,
229 PMBUS_STATUS_MFR_SPECIFIC);
232 * Return the status register instead of an error,
233 * since we successfully read status.
237 /* Add MFR_SPECIFIC bits to the standard pmbus status regs. */
238 if (reg == PMBUS_STATUS_FAN_12) {
239 if (mfr & CFFPS_MFR_FAN_FAULT)
240 rc |= PB_FAN_FAN1_FAULT;
241 } else if (reg == PMBUS_STATUS_TEMPERATURE) {
242 if (mfr & CFFPS_MFR_THERMAL_FAULT)
243 rc |= PB_TEMP_OT_FAULT;
244 } else if (reg == PMBUS_STATUS_VOUT) {
245 if (mfr & (CFFPS_MFR_OV_FAULT | CFFPS_MFR_VAUX_FAULT))
246 rc |= PB_VOLTAGE_OV_FAULT;
247 if (mfr & CFFPS_MFR_UV_FAULT)
248 rc |= PB_VOLTAGE_UV_FAULT;
249 } else if (reg == PMBUS_STATUS_IOUT) {
250 if (mfr & CFFPS_MFR_OC_FAULT)
251 rc |= PB_IOUT_OC_FAULT;
252 if (mfr & CFFPS_MFR_CURRENT_SHARE_WARNING)
253 rc |= PB_CURRENT_SHARE_FAULT;
264 static int ibm_cffps_read_word_data(struct i2c_client *client, int page,
270 case PMBUS_STATUS_WORD:
271 rc = pmbus_read_word_data(client, page, reg);
275 mfr = pmbus_read_byte_data(client, page,
276 PMBUS_STATUS_MFR_SPECIFIC);
279 * Return the status register instead of an error,
280 * since we successfully read status.
284 if (mfr & CFFPS_MFR_PS_KILL)
295 static void ibm_cffps_led_brightness_set(struct led_classdev *led_cdev,
296 enum led_brightness brightness)
299 struct ibm_cffps *psu = container_of(led_cdev, struct ibm_cffps, led);
301 if (brightness == LED_OFF) {
302 psu->led_state = CFFPS_LED_OFF;
304 brightness = LED_FULL;
305 if (psu->led_state != CFFPS_LED_BLINK)
306 psu->led_state = CFFPS_LED_ON;
309 pmbus_set_page(psu->client, 0);
311 rc = i2c_smbus_write_byte_data(psu->client, CFFPS_SYS_CONFIG_CMD,
316 led_cdev->brightness = brightness;
319 static int ibm_cffps_led_blink_set(struct led_classdev *led_cdev,
320 unsigned long *delay_on,
321 unsigned long *delay_off)
324 struct ibm_cffps *psu = container_of(led_cdev, struct ibm_cffps, led);
326 psu->led_state = CFFPS_LED_BLINK;
328 if (led_cdev->brightness == LED_OFF)
331 pmbus_set_page(psu->client, 0);
333 rc = i2c_smbus_write_byte_data(psu->client, CFFPS_SYS_CONFIG_CMD,
338 *delay_on = CFFPS_BLINK_RATE_MS;
339 *delay_off = CFFPS_BLINK_RATE_MS;
344 static void ibm_cffps_create_led_class(struct ibm_cffps *psu)
347 struct i2c_client *client = psu->client;
348 struct device *dev = &client->dev;
350 snprintf(psu->led_name, sizeof(psu->led_name), "%s-%02x", client->name,
352 psu->led.name = psu->led_name;
353 psu->led.max_brightness = LED_FULL;
354 psu->led.brightness_set = ibm_cffps_led_brightness_set;
355 psu->led.blink_set = ibm_cffps_led_blink_set;
357 rc = devm_led_classdev_register(dev, &psu->led);
359 dev_warn(dev, "failed to register led class: %d\n", rc);
362 static struct pmbus_driver_info ibm_cffps_info[] = {
365 .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
366 PMBUS_HAVE_PIN | PMBUS_HAVE_FAN12 | PMBUS_HAVE_TEMP |
367 PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3 |
368 PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT |
369 PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP |
370 PMBUS_HAVE_STATUS_FAN12,
371 .read_byte_data = ibm_cffps_read_byte_data,
372 .read_word_data = ibm_cffps_read_word_data,
376 .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
377 PMBUS_HAVE_PIN | PMBUS_HAVE_FAN12 | PMBUS_HAVE_TEMP |
378 PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3 |
379 PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT |
380 PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP |
381 PMBUS_HAVE_STATUS_FAN12,
382 .func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
383 PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3 |
384 PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT,
385 .read_byte_data = ibm_cffps_read_byte_data,
386 .read_word_data = ibm_cffps_read_word_data,
390 static struct pmbus_platform_data ibm_cffps_pdata = {
391 .flags = PMBUS_SKIP_STATUS_CHECK,
394 static int ibm_cffps_probe(struct i2c_client *client,
395 const struct i2c_device_id *id)
399 struct dentry *debugfs;
400 struct dentry *ibm_cffps_dir;
401 struct ibm_cffps *psu;
402 const void *md = of_device_get_match_data(&client->dev);
405 vs = (enum versions)md;
407 vs = (enum versions)id->driver_data;
411 client->dev.platform_data = &ibm_cffps_pdata;
412 rc = pmbus_do_probe(client, id, &ibm_cffps_info[vs]);
417 * Don't fail the probe if there isn't enough memory for leds and
420 psu = devm_kzalloc(&client->dev, sizeof(*psu), GFP_KERNEL);
425 psu->client = client;
426 mutex_init(&psu->input_history.update_lock);
427 psu->input_history.last_update = jiffies - HZ;
429 ibm_cffps_create_led_class(psu);
431 /* Don't fail the probe if we can't create debugfs */
432 debugfs = pmbus_get_debugfs_dir(client);
436 ibm_cffps_dir = debugfs_create_dir(client->name, debugfs);
440 for (i = 0; i < CFFPS_DEBUGFS_NUM_ENTRIES; ++i)
441 psu->debugfs_entries[i] = i;
443 debugfs_create_file("input_history", 0444, ibm_cffps_dir,
444 &psu->debugfs_entries[CFFPS_DEBUGFS_INPUT_HISTORY],
446 debugfs_create_file("fru", 0444, ibm_cffps_dir,
447 &psu->debugfs_entries[CFFPS_DEBUGFS_FRU],
449 debugfs_create_file("part_number", 0444, ibm_cffps_dir,
450 &psu->debugfs_entries[CFFPS_DEBUGFS_PN],
452 debugfs_create_file("serial_number", 0444, ibm_cffps_dir,
453 &psu->debugfs_entries[CFFPS_DEBUGFS_SN],
455 debugfs_create_file("ccin", 0444, ibm_cffps_dir,
456 &psu->debugfs_entries[CFFPS_DEBUGFS_CCIN],
458 debugfs_create_file("fw_version", 0444, ibm_cffps_dir,
459 &psu->debugfs_entries[CFFPS_DEBUGFS_FW],
465 static const struct i2c_device_id ibm_cffps_id[] = {
466 { "ibm_cffps1", cffps1 },
467 { "ibm_cffps2", cffps2 },
470 MODULE_DEVICE_TABLE(i2c, ibm_cffps_id);
472 static const struct of_device_id ibm_cffps_of_match[] = {
474 .compatible = "ibm,cffps1",
475 .data = (void *)cffps1
478 .compatible = "ibm,cffps2",
479 .data = (void *)cffps2
483 MODULE_DEVICE_TABLE(of, ibm_cffps_of_match);
485 static struct i2c_driver ibm_cffps_driver = {
488 .of_match_table = ibm_cffps_of_match,
490 .probe = ibm_cffps_probe,
491 .remove = pmbus_do_remove,
492 .id_table = ibm_cffps_id,
495 module_i2c_driver(ibm_cffps_driver);
497 MODULE_AUTHOR("Eddie James");
498 MODULE_DESCRIPTION("PMBus driver for IBM Common Form Factor power supplies");
499 MODULE_LICENSE("GPL");