1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2017 IBM Corp.
6 #include <linux/bitfield.h>
7 #include <linux/bitops.h>
8 #include <linux/debugfs.h>
9 #include <linux/device.h>
11 #include <linux/i2c.h>
12 #include <linux/jiffies.h>
13 #include <linux/leds.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/of_device.h>
17 #include <linux/pmbus.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 0xFA
26 #define CFFPS1_FW_NUM_BYTES 4
27 #define CFFPS2_FW_NUM_WORDS 3
28 #define CFFPS_SYS_CONFIG_CMD 0xDA
30 #define CFFPS_INPUT_HISTORY_CMD 0xD6
31 #define CFFPS_INPUT_HISTORY_SIZE 100
33 #define CFFPS_CCIN_VERSION GENMASK(15, 8)
34 #define CFFPS_CCIN_VERSION_1 0x2b
35 #define CFFPS_CCIN_VERSION_2 0x2e
37 /* STATUS_MFR_SPECIFIC bits */
38 #define CFFPS_MFR_FAN_FAULT BIT(0)
39 #define CFFPS_MFR_THERMAL_FAULT BIT(1)
40 #define CFFPS_MFR_OV_FAULT BIT(2)
41 #define CFFPS_MFR_UV_FAULT BIT(3)
42 #define CFFPS_MFR_PS_KILL BIT(4)
43 #define CFFPS_MFR_OC_FAULT BIT(5)
44 #define CFFPS_MFR_VAUX_FAULT BIT(6)
45 #define CFFPS_MFR_CURRENT_SHARE_WARNING BIT(7)
48 * LED off state actually relinquishes LED control to PSU firmware, so it can
49 * turn on the LED for faults.
51 #define CFFPS_LED_OFF 0
52 #define CFFPS_LED_BLINK BIT(0)
53 #define CFFPS_LED_ON BIT(1)
54 #define CFFPS_BLINK_RATE_MS 250
57 CFFPS_DEBUGFS_INPUT_HISTORY = 0,
63 CFFPS_DEBUGFS_NUM_ENTRIES
66 enum versions { cffps1, cffps2, cffps_unknown };
68 struct ibm_cffps_input_history {
69 struct mutex update_lock;
70 unsigned long last_update;
73 u8 data[CFFPS_INPUT_HISTORY_SIZE];
77 enum versions version;
78 struct i2c_client *client;
80 struct ibm_cffps_input_history input_history;
82 int debugfs_entries[CFFPS_DEBUGFS_NUM_ENTRIES];
86 struct led_classdev led;
89 #define to_psu(x, y) container_of((x), struct ibm_cffps, debugfs_entries[(y)])
91 static ssize_t ibm_cffps_read_input_history(struct ibm_cffps *psu,
92 char __user *buf, size_t count,
96 u8 msgbuf0[1] = { CFFPS_INPUT_HISTORY_CMD };
97 u8 msgbuf1[CFFPS_INPUT_HISTORY_SIZE + 1] = { 0 };
98 struct i2c_msg msg[2] = {
100 .addr = psu->client->addr,
101 .flags = psu->client->flags,
105 .addr = psu->client->addr,
106 .flags = psu->client->flags | I2C_M_RD,
107 .len = CFFPS_INPUT_HISTORY_SIZE + 1,
113 mutex_lock(&psu->input_history.update_lock);
114 if (time_after(jiffies, psu->input_history.last_update + HZ)) {
116 * Use a raw i2c transfer, since we need more bytes
117 * than Linux I2C supports through smbus xfr (only 32).
119 rc = i2c_transfer(psu->client->adapter, msg, 2);
121 mutex_unlock(&psu->input_history.update_lock);
125 psu->input_history.byte_count = msgbuf1[0];
126 memcpy(psu->input_history.data, &msgbuf1[1],
127 CFFPS_INPUT_HISTORY_SIZE);
128 psu->input_history.last_update = jiffies;
131 mutex_unlock(&psu->input_history.update_lock);
134 return simple_read_from_buffer(buf, count, ppos,
135 psu->input_history.data,
136 psu->input_history.byte_count);
139 static ssize_t ibm_cffps_debugfs_op(struct file *file, char __user *buf,
140 size_t count, loff_t *ppos)
144 int *idxp = file->private_data;
146 struct ibm_cffps *psu = to_psu(idxp, idx);
147 char data[I2C_SMBUS_BLOCK_MAX] = { 0 };
149 pmbus_set_page(psu->client, 0);
152 case CFFPS_DEBUGFS_INPUT_HISTORY:
153 return ibm_cffps_read_input_history(psu, buf, count, ppos);
154 case CFFPS_DEBUGFS_FRU:
157 case CFFPS_DEBUGFS_PN:
160 case CFFPS_DEBUGFS_SN:
163 case CFFPS_DEBUGFS_CCIN:
164 rc = i2c_smbus_read_word_swapped(psu->client, CFFPS_CCIN_CMD);
168 rc = snprintf(data, 5, "%04X", rc);
170 case CFFPS_DEBUGFS_FW:
171 switch (psu->version) {
173 for (i = 0; i < CFFPS1_FW_NUM_BYTES; ++i) {
174 rc = i2c_smbus_read_byte_data(psu->client,
180 snprintf(&data[i * 2], 3, "%02X", rc);
186 for (i = 0; i < CFFPS2_FW_NUM_WORDS; ++i) {
187 rc = i2c_smbus_read_word_data(psu->client,
193 snprintf(&data[i * 4], 5, "%04X", rc);
206 rc = i2c_smbus_read_block_data(psu->client, cmd, data);
214 return simple_read_from_buffer(buf, count, ppos, data, rc);
217 static const struct file_operations ibm_cffps_fops = {
218 .llseek = noop_llseek,
219 .read = ibm_cffps_debugfs_op,
223 static int ibm_cffps_read_byte_data(struct i2c_client *client, int page,
229 case PMBUS_STATUS_VOUT:
230 case PMBUS_STATUS_IOUT:
231 case PMBUS_STATUS_TEMPERATURE:
232 case PMBUS_STATUS_FAN_12:
233 rc = pmbus_read_byte_data(client, page, reg);
237 mfr = pmbus_read_byte_data(client, page,
238 PMBUS_STATUS_MFR_SPECIFIC);
241 * Return the status register instead of an error,
242 * since we successfully read status.
246 /* Add MFR_SPECIFIC bits to the standard pmbus status regs. */
247 if (reg == PMBUS_STATUS_FAN_12) {
248 if (mfr & CFFPS_MFR_FAN_FAULT)
249 rc |= PB_FAN_FAN1_FAULT;
250 } else if (reg == PMBUS_STATUS_TEMPERATURE) {
251 if (mfr & CFFPS_MFR_THERMAL_FAULT)
252 rc |= PB_TEMP_OT_FAULT;
253 } else if (reg == PMBUS_STATUS_VOUT) {
254 if (mfr & (CFFPS_MFR_OV_FAULT | CFFPS_MFR_VAUX_FAULT))
255 rc |= PB_VOLTAGE_OV_FAULT;
256 if (mfr & CFFPS_MFR_UV_FAULT)
257 rc |= PB_VOLTAGE_UV_FAULT;
258 } else if (reg == PMBUS_STATUS_IOUT) {
259 if (mfr & CFFPS_MFR_OC_FAULT)
260 rc |= PB_IOUT_OC_FAULT;
261 if (mfr & CFFPS_MFR_CURRENT_SHARE_WARNING)
262 rc |= PB_CURRENT_SHARE_FAULT;
273 static int ibm_cffps_read_word_data(struct i2c_client *client, int page,
279 case PMBUS_STATUS_WORD:
280 rc = pmbus_read_word_data(client, page, reg);
284 mfr = pmbus_read_byte_data(client, page,
285 PMBUS_STATUS_MFR_SPECIFIC);
288 * Return the status register instead of an error,
289 * since we successfully read status.
293 if (mfr & CFFPS_MFR_PS_KILL)
304 static int ibm_cffps_led_brightness_set(struct led_classdev *led_cdev,
305 enum led_brightness brightness)
309 struct ibm_cffps *psu = container_of(led_cdev, struct ibm_cffps, led);
311 if (brightness == LED_OFF) {
312 next_led_state = CFFPS_LED_OFF;
314 brightness = LED_FULL;
316 if (psu->led_state != CFFPS_LED_BLINK)
317 next_led_state = CFFPS_LED_ON;
319 next_led_state = CFFPS_LED_BLINK;
322 dev_dbg(&psu->client->dev, "LED brightness set: %d. Command: %d.\n",
323 brightness, next_led_state);
325 pmbus_set_page(psu->client, 0);
327 rc = i2c_smbus_write_byte_data(psu->client, CFFPS_SYS_CONFIG_CMD,
332 psu->led_state = next_led_state;
333 led_cdev->brightness = brightness;
338 static int ibm_cffps_led_blink_set(struct led_classdev *led_cdev,
339 unsigned long *delay_on,
340 unsigned long *delay_off)
343 struct ibm_cffps *psu = container_of(led_cdev, struct ibm_cffps, led);
345 dev_dbg(&psu->client->dev, "LED blink set.\n");
347 pmbus_set_page(psu->client, 0);
349 rc = i2c_smbus_write_byte_data(psu->client, CFFPS_SYS_CONFIG_CMD,
354 psu->led_state = CFFPS_LED_BLINK;
355 led_cdev->brightness = LED_FULL;
356 *delay_on = CFFPS_BLINK_RATE_MS;
357 *delay_off = CFFPS_BLINK_RATE_MS;
362 static void ibm_cffps_create_led_class(struct ibm_cffps *psu)
365 struct i2c_client *client = psu->client;
366 struct device *dev = &client->dev;
368 snprintf(psu->led_name, sizeof(psu->led_name), "%s-%02x", client->name,
370 psu->led.name = psu->led_name;
371 psu->led.max_brightness = LED_FULL;
372 psu->led.brightness_set_blocking = ibm_cffps_led_brightness_set;
373 psu->led.blink_set = ibm_cffps_led_blink_set;
375 rc = devm_led_classdev_register(dev, &psu->led);
377 dev_warn(dev, "failed to register led class: %d\n", rc);
380 static struct pmbus_driver_info ibm_cffps_info[] = {
383 .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
384 PMBUS_HAVE_PIN | PMBUS_HAVE_FAN12 | PMBUS_HAVE_TEMP |
385 PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3 |
386 PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT |
387 PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP |
388 PMBUS_HAVE_STATUS_FAN12,
389 .read_byte_data = ibm_cffps_read_byte_data,
390 .read_word_data = ibm_cffps_read_word_data,
394 .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
395 PMBUS_HAVE_PIN | PMBUS_HAVE_FAN12 | PMBUS_HAVE_TEMP |
396 PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3 |
397 PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT |
398 PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP |
399 PMBUS_HAVE_STATUS_FAN12,
400 .func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
401 PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3 |
402 PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT,
403 .read_byte_data = ibm_cffps_read_byte_data,
404 .read_word_data = ibm_cffps_read_word_data,
408 static struct pmbus_platform_data ibm_cffps_pdata = {
409 .flags = PMBUS_SKIP_STATUS_CHECK,
412 static int ibm_cffps_probe(struct i2c_client *client,
413 const struct i2c_device_id *id)
416 enum versions vs = cffps_unknown;
417 struct dentry *debugfs;
418 struct dentry *ibm_cffps_dir;
419 struct ibm_cffps *psu;
420 const void *md = of_device_get_match_data(&client->dev);
423 vs = (enum versions)md;
425 vs = (enum versions)id->driver_data;
427 if (vs == cffps_unknown) {
428 u16 ccin_version = CFFPS_CCIN_VERSION_1;
429 int ccin = i2c_smbus_read_word_swapped(client, CFFPS_CCIN_CMD);
432 ccin_version = FIELD_GET(CFFPS_CCIN_VERSION, ccin);
434 switch (ccin_version) {
436 case CFFPS_CCIN_VERSION_1:
439 case CFFPS_CCIN_VERSION_2:
444 /* Set the client name to include the version number. */
445 snprintf(client->name, I2C_NAME_SIZE, "cffps%d", vs + 1);
448 client->dev.platform_data = &ibm_cffps_pdata;
449 rc = pmbus_do_probe(client, id, &ibm_cffps_info[vs]);
454 * Don't fail the probe if there isn't enough memory for leds and
457 psu = devm_kzalloc(&client->dev, sizeof(*psu), GFP_KERNEL);
462 psu->client = client;
463 mutex_init(&psu->input_history.update_lock);
464 psu->input_history.last_update = jiffies - HZ;
466 ibm_cffps_create_led_class(psu);
468 /* Don't fail the probe if we can't create debugfs */
469 debugfs = pmbus_get_debugfs_dir(client);
473 ibm_cffps_dir = debugfs_create_dir(client->name, debugfs);
477 for (i = 0; i < CFFPS_DEBUGFS_NUM_ENTRIES; ++i)
478 psu->debugfs_entries[i] = i;
480 debugfs_create_file("input_history", 0444, ibm_cffps_dir,
481 &psu->debugfs_entries[CFFPS_DEBUGFS_INPUT_HISTORY],
483 debugfs_create_file("fru", 0444, ibm_cffps_dir,
484 &psu->debugfs_entries[CFFPS_DEBUGFS_FRU],
486 debugfs_create_file("part_number", 0444, ibm_cffps_dir,
487 &psu->debugfs_entries[CFFPS_DEBUGFS_PN],
489 debugfs_create_file("serial_number", 0444, ibm_cffps_dir,
490 &psu->debugfs_entries[CFFPS_DEBUGFS_SN],
492 debugfs_create_file("ccin", 0444, ibm_cffps_dir,
493 &psu->debugfs_entries[CFFPS_DEBUGFS_CCIN],
495 debugfs_create_file("fw_version", 0444, ibm_cffps_dir,
496 &psu->debugfs_entries[CFFPS_DEBUGFS_FW],
502 static const struct i2c_device_id ibm_cffps_id[] = {
503 { "ibm_cffps1", cffps1 },
504 { "ibm_cffps2", cffps2 },
505 { "ibm_cffps", cffps_unknown },
508 MODULE_DEVICE_TABLE(i2c, ibm_cffps_id);
510 static const struct of_device_id ibm_cffps_of_match[] = {
512 .compatible = "ibm,cffps1",
513 .data = (void *)cffps1
516 .compatible = "ibm,cffps2",
517 .data = (void *)cffps2
520 .compatible = "ibm,cffps",
521 .data = (void *)cffps_unknown
525 MODULE_DEVICE_TABLE(of, ibm_cffps_of_match);
527 static struct i2c_driver ibm_cffps_driver = {
530 .of_match_table = ibm_cffps_of_match,
532 .probe = ibm_cffps_probe,
533 .remove = pmbus_do_remove,
534 .id_table = ibm_cffps_id,
537 module_i2c_driver(ibm_cffps_driver);
539 MODULE_AUTHOR("Eddie James");
540 MODULE_DESCRIPTION("PMBus driver for IBM Common Form Factor power supplies");
541 MODULE_LICENSE("GPL");