Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / hwmon / pmbus / ibm-cffps.c
blobd6bbbb223871b3fe3a289b11b386599e484d39d1
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright 2017 IBM Corp.
4 */
6 #include <linux/bitfield.h>
7 #include <linux/bitops.h>
8 #include <linux/debugfs.h>
9 #include <linux/device.h>
10 #include <linux/fs.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>
19 #include "pmbus.h"
21 #define CFFPS_FRU_CMD 0x9A
22 #define CFFPS_PN_CMD 0x9B
23 #define CFFPS_HEADER_CMD 0x9C
24 #define CFFPS_SN_CMD 0x9E
25 #define CFFPS_MAX_POWER_OUT_CMD 0xA7
26 #define CFFPS_CCIN_CMD 0xBD
27 #define CFFPS_FW_CMD 0xFA
28 #define CFFPS1_FW_NUM_BYTES 4
29 #define CFFPS2_FW_NUM_WORDS 3
30 #define CFFPS_SYS_CONFIG_CMD 0xDA
31 #define CFFPS_12VCS_VOUT_CMD 0xDE
33 #define CFFPS_INPUT_HISTORY_CMD 0xD6
34 #define CFFPS_INPUT_HISTORY_SIZE 100
36 #define CFFPS_CCIN_REVISION GENMASK(7, 0)
37 #define CFFPS_CCIN_REVISION_LEGACY 0xde
38 #define CFFPS_CCIN_VERSION GENMASK(15, 8)
39 #define CFFPS_CCIN_VERSION_1 0x2b
40 #define CFFPS_CCIN_VERSION_2 0x2e
41 #define CFFPS_CCIN_VERSION_3 0x51
43 /* STATUS_MFR_SPECIFIC bits */
44 #define CFFPS_MFR_FAN_FAULT BIT(0)
45 #define CFFPS_MFR_THERMAL_FAULT BIT(1)
46 #define CFFPS_MFR_OV_FAULT BIT(2)
47 #define CFFPS_MFR_UV_FAULT BIT(3)
48 #define CFFPS_MFR_PS_KILL BIT(4)
49 #define CFFPS_MFR_OC_FAULT BIT(5)
50 #define CFFPS_MFR_VAUX_FAULT BIT(6)
51 #define CFFPS_MFR_CURRENT_SHARE_WARNING BIT(7)
53 #define CFFPS_LED_BLINK BIT(0)
54 #define CFFPS_LED_ON BIT(1)
55 #define CFFPS_LED_OFF BIT(2)
56 #define CFFPS_BLINK_RATE_MS 250
58 enum {
59 CFFPS_DEBUGFS_INPUT_HISTORY = 0,
60 CFFPS_DEBUGFS_FRU,
61 CFFPS_DEBUGFS_PN,
62 CFFPS_DEBUGFS_HEADER,
63 CFFPS_DEBUGFS_SN,
64 CFFPS_DEBUGFS_MAX_POWER_OUT,
65 CFFPS_DEBUGFS_CCIN,
66 CFFPS_DEBUGFS_FW,
67 CFFPS_DEBUGFS_ON_OFF_CONFIG,
68 CFFPS_DEBUGFS_NUM_ENTRIES
71 enum versions { cffps1, cffps2, cffps_unknown };
73 struct ibm_cffps_input_history {
74 struct mutex update_lock;
75 unsigned long last_update;
77 u8 byte_count;
78 u8 data[CFFPS_INPUT_HISTORY_SIZE];
81 struct ibm_cffps {
82 enum versions version;
83 struct i2c_client *client;
85 struct ibm_cffps_input_history input_history;
87 int debugfs_entries[CFFPS_DEBUGFS_NUM_ENTRIES];
89 char led_name[32];
90 u8 led_state;
91 struct led_classdev led;
94 static const struct i2c_device_id ibm_cffps_id[];
96 #define to_psu(x, y) container_of((x), struct ibm_cffps, debugfs_entries[(y)])
98 static ssize_t ibm_cffps_read_input_history(struct ibm_cffps *psu,
99 char __user *buf, size_t count,
100 loff_t *ppos)
102 int rc;
103 u8 msgbuf0[1] = { CFFPS_INPUT_HISTORY_CMD };
104 u8 msgbuf1[CFFPS_INPUT_HISTORY_SIZE + 1] = { 0 };
105 struct i2c_msg msg[2] = {
107 .addr = psu->client->addr,
108 .flags = psu->client->flags,
109 .len = 1,
110 .buf = msgbuf0,
111 }, {
112 .addr = psu->client->addr,
113 .flags = psu->client->flags | I2C_M_RD,
114 .len = CFFPS_INPUT_HISTORY_SIZE + 1,
115 .buf = msgbuf1,
119 if (!*ppos) {
120 mutex_lock(&psu->input_history.update_lock);
121 if (time_after(jiffies, psu->input_history.last_update + HZ)) {
123 * Use a raw i2c transfer, since we need more bytes
124 * than Linux I2C supports through smbus xfr (only 32).
126 rc = i2c_transfer(psu->client->adapter, msg, 2);
127 if (rc < 0) {
128 mutex_unlock(&psu->input_history.update_lock);
129 return rc;
132 psu->input_history.byte_count = msgbuf1[0];
133 memcpy(psu->input_history.data, &msgbuf1[1],
134 CFFPS_INPUT_HISTORY_SIZE);
135 psu->input_history.last_update = jiffies;
138 mutex_unlock(&psu->input_history.update_lock);
141 return simple_read_from_buffer(buf, count, ppos,
142 psu->input_history.data,
143 psu->input_history.byte_count);
146 static ssize_t ibm_cffps_debugfs_read(struct file *file, char __user *buf,
147 size_t count, loff_t *ppos)
149 u8 cmd;
150 int i, rc;
151 int *idxp = file->private_data;
152 int idx = *idxp;
153 struct ibm_cffps *psu = to_psu(idxp, idx);
154 char data[I2C_SMBUS_BLOCK_MAX + 2] = { 0 };
156 pmbus_set_page(psu->client, 0, 0xff);
158 switch (idx) {
159 case CFFPS_DEBUGFS_INPUT_HISTORY:
160 return ibm_cffps_read_input_history(psu, buf, count, ppos);
161 case CFFPS_DEBUGFS_FRU:
162 cmd = CFFPS_FRU_CMD;
163 break;
164 case CFFPS_DEBUGFS_PN:
165 cmd = CFFPS_PN_CMD;
166 break;
167 case CFFPS_DEBUGFS_HEADER:
168 cmd = CFFPS_HEADER_CMD;
169 break;
170 case CFFPS_DEBUGFS_SN:
171 cmd = CFFPS_SN_CMD;
172 break;
173 case CFFPS_DEBUGFS_MAX_POWER_OUT:
174 rc = i2c_smbus_read_word_swapped(psu->client,
175 CFFPS_MAX_POWER_OUT_CMD);
176 if (rc < 0)
177 return rc;
179 rc = snprintf(data, I2C_SMBUS_BLOCK_MAX, "%d", rc);
180 goto done;
181 case CFFPS_DEBUGFS_CCIN:
182 rc = i2c_smbus_read_word_swapped(psu->client, CFFPS_CCIN_CMD);
183 if (rc < 0)
184 return rc;
186 rc = snprintf(data, 5, "%04X", rc);
187 goto done;
188 case CFFPS_DEBUGFS_FW:
189 switch (psu->version) {
190 case cffps1:
191 for (i = 0; i < CFFPS1_FW_NUM_BYTES; ++i) {
192 rc = i2c_smbus_read_byte_data(psu->client,
193 CFFPS_FW_CMD +
195 if (rc < 0)
196 return rc;
198 snprintf(&data[i * 2], 3, "%02X", rc);
201 rc = i * 2;
202 break;
203 case cffps2:
204 for (i = 0; i < CFFPS2_FW_NUM_WORDS; ++i) {
205 rc = i2c_smbus_read_word_data(psu->client,
206 CFFPS_FW_CMD +
208 if (rc < 0)
209 return rc;
211 snprintf(&data[i * 4], 5, "%04X", rc);
214 rc = i * 4;
215 break;
216 default:
217 return -EOPNOTSUPP;
219 goto done;
220 case CFFPS_DEBUGFS_ON_OFF_CONFIG:
221 rc = i2c_smbus_read_byte_data(psu->client,
222 PMBUS_ON_OFF_CONFIG);
223 if (rc < 0)
224 return rc;
226 rc = snprintf(data, 3, "%02x", rc);
227 goto done;
228 default:
229 return -EINVAL;
232 rc = i2c_smbus_read_block_data(psu->client, cmd, data);
233 if (rc < 0)
234 return rc;
236 done:
237 data[rc] = '\n';
238 rc += 2;
240 return simple_read_from_buffer(buf, count, ppos, data, rc);
243 static ssize_t ibm_cffps_debugfs_write(struct file *file,
244 const char __user *buf, size_t count,
245 loff_t *ppos)
247 u8 data;
248 ssize_t rc;
249 int *idxp = file->private_data;
250 int idx = *idxp;
251 struct ibm_cffps *psu = to_psu(idxp, idx);
253 switch (idx) {
254 case CFFPS_DEBUGFS_ON_OFF_CONFIG:
255 pmbus_set_page(psu->client, 0, 0xff);
257 rc = simple_write_to_buffer(&data, 1, ppos, buf, count);
258 if (rc <= 0)
259 return rc;
261 rc = i2c_smbus_write_byte_data(psu->client,
262 PMBUS_ON_OFF_CONFIG, data);
263 if (rc)
264 return rc;
266 rc = 1;
267 break;
268 default:
269 return -EINVAL;
272 return rc;
275 static const struct file_operations ibm_cffps_fops = {
276 .llseek = noop_llseek,
277 .read = ibm_cffps_debugfs_read,
278 .write = ibm_cffps_debugfs_write,
279 .open = simple_open,
282 static int ibm_cffps_read_byte_data(struct i2c_client *client, int page,
283 int reg)
285 int rc, mfr;
287 switch (reg) {
288 case PMBUS_STATUS_VOUT:
289 case PMBUS_STATUS_IOUT:
290 case PMBUS_STATUS_TEMPERATURE:
291 case PMBUS_STATUS_FAN_12:
292 rc = pmbus_read_byte_data(client, page, reg);
293 if (rc < 0)
294 return rc;
296 mfr = pmbus_read_byte_data(client, page,
297 PMBUS_STATUS_MFR_SPECIFIC);
298 if (mfr < 0)
300 * Return the status register instead of an error,
301 * since we successfully read status.
303 return rc;
305 /* Add MFR_SPECIFIC bits to the standard pmbus status regs. */
306 if (reg == PMBUS_STATUS_FAN_12) {
307 if (mfr & CFFPS_MFR_FAN_FAULT)
308 rc |= PB_FAN_FAN1_FAULT;
309 } else if (reg == PMBUS_STATUS_TEMPERATURE) {
310 if (mfr & CFFPS_MFR_THERMAL_FAULT)
311 rc |= PB_TEMP_OT_FAULT;
312 } else if (reg == PMBUS_STATUS_VOUT) {
313 if (mfr & (CFFPS_MFR_OV_FAULT | CFFPS_MFR_VAUX_FAULT))
314 rc |= PB_VOLTAGE_OV_FAULT;
315 if (mfr & CFFPS_MFR_UV_FAULT)
316 rc |= PB_VOLTAGE_UV_FAULT;
317 } else if (reg == PMBUS_STATUS_IOUT) {
318 if (mfr & CFFPS_MFR_OC_FAULT)
319 rc |= PB_IOUT_OC_FAULT;
320 if (mfr & CFFPS_MFR_CURRENT_SHARE_WARNING)
321 rc |= PB_CURRENT_SHARE_FAULT;
323 break;
324 default:
325 rc = -ENODATA;
326 break;
329 return rc;
332 static int ibm_cffps_read_word_data(struct i2c_client *client, int page,
333 int phase, int reg)
335 int rc, mfr;
337 switch (reg) {
338 case PMBUS_STATUS_WORD:
339 rc = pmbus_read_word_data(client, page, phase, reg);
340 if (rc < 0)
341 return rc;
343 mfr = pmbus_read_byte_data(client, page,
344 PMBUS_STATUS_MFR_SPECIFIC);
345 if (mfr < 0)
347 * Return the status register instead of an error,
348 * since we successfully read status.
350 return rc;
352 if (mfr & CFFPS_MFR_PS_KILL)
353 rc |= PB_STATUS_OFF;
354 break;
355 case PMBUS_VIRT_READ_VMON:
356 rc = pmbus_read_word_data(client, page, phase,
357 CFFPS_12VCS_VOUT_CMD);
358 break;
359 default:
360 rc = -ENODATA;
361 break;
364 return rc;
367 static int ibm_cffps_led_brightness_set(struct led_classdev *led_cdev,
368 enum led_brightness brightness)
370 int rc;
371 u8 next_led_state;
372 struct ibm_cffps *psu = container_of(led_cdev, struct ibm_cffps, led);
374 if (brightness == LED_OFF) {
375 next_led_state = CFFPS_LED_OFF;
376 } else {
377 brightness = LED_FULL;
379 if (psu->led_state != CFFPS_LED_BLINK)
380 next_led_state = CFFPS_LED_ON;
381 else
382 next_led_state = CFFPS_LED_BLINK;
385 dev_dbg(&psu->client->dev, "LED brightness set: %d. Command: %d.\n",
386 brightness, next_led_state);
388 pmbus_set_page(psu->client, 0, 0xff);
390 rc = i2c_smbus_write_byte_data(psu->client, CFFPS_SYS_CONFIG_CMD,
391 next_led_state);
392 if (rc < 0)
393 return rc;
395 psu->led_state = next_led_state;
396 led_cdev->brightness = brightness;
398 return 0;
401 static int ibm_cffps_led_blink_set(struct led_classdev *led_cdev,
402 unsigned long *delay_on,
403 unsigned long *delay_off)
405 int rc;
406 struct ibm_cffps *psu = container_of(led_cdev, struct ibm_cffps, led);
408 dev_dbg(&psu->client->dev, "LED blink set.\n");
410 pmbus_set_page(psu->client, 0, 0xff);
412 rc = i2c_smbus_write_byte_data(psu->client, CFFPS_SYS_CONFIG_CMD,
413 CFFPS_LED_BLINK);
414 if (rc < 0)
415 return rc;
417 psu->led_state = CFFPS_LED_BLINK;
418 led_cdev->brightness = LED_FULL;
419 *delay_on = CFFPS_BLINK_RATE_MS;
420 *delay_off = CFFPS_BLINK_RATE_MS;
422 return 0;
425 static void ibm_cffps_create_led_class(struct ibm_cffps *psu)
427 int rc;
428 struct i2c_client *client = psu->client;
429 struct device *dev = &client->dev;
431 snprintf(psu->led_name, sizeof(psu->led_name), "%s-%02x", client->name,
432 client->addr);
433 psu->led.name = psu->led_name;
434 psu->led.max_brightness = LED_FULL;
435 psu->led.brightness_set_blocking = ibm_cffps_led_brightness_set;
436 psu->led.blink_set = ibm_cffps_led_blink_set;
438 rc = devm_led_classdev_register(dev, &psu->led);
439 if (rc)
440 dev_warn(dev, "failed to register led class: %d\n", rc);
441 else
442 i2c_smbus_write_byte_data(client, CFFPS_SYS_CONFIG_CMD,
443 CFFPS_LED_OFF);
446 static struct pmbus_driver_info ibm_cffps_info[] = {
447 [cffps1] = {
448 .pages = 1,
449 .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
450 PMBUS_HAVE_PIN | PMBUS_HAVE_FAN12 | PMBUS_HAVE_TEMP |
451 PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3 |
452 PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT |
453 PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP |
454 PMBUS_HAVE_STATUS_FAN12,
455 .read_byte_data = ibm_cffps_read_byte_data,
456 .read_word_data = ibm_cffps_read_word_data,
458 [cffps2] = {
459 .pages = 2,
460 .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
461 PMBUS_HAVE_PIN | PMBUS_HAVE_FAN12 | PMBUS_HAVE_TEMP |
462 PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3 |
463 PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT |
464 PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP |
465 PMBUS_HAVE_STATUS_FAN12 | PMBUS_HAVE_VMON,
466 .func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
467 PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3 |
468 PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT,
469 .read_byte_data = ibm_cffps_read_byte_data,
470 .read_word_data = ibm_cffps_read_word_data,
474 static struct pmbus_platform_data ibm_cffps_pdata = {
475 .flags = PMBUS_SKIP_STATUS_CHECK,
478 static int ibm_cffps_probe(struct i2c_client *client)
480 int i, rc;
481 enum versions vs = cffps_unknown;
482 struct dentry *debugfs;
483 struct dentry *ibm_cffps_dir;
484 struct ibm_cffps *psu;
485 const void *md = of_device_get_match_data(&client->dev);
486 const struct i2c_device_id *id;
488 if (md) {
489 vs = (enum versions)md;
490 } else {
491 id = i2c_match_id(ibm_cffps_id, client);
492 if (id)
493 vs = (enum versions)id->driver_data;
496 if (vs == cffps_unknown) {
497 u16 ccin_revision = 0;
498 u16 ccin_version = CFFPS_CCIN_VERSION_1;
499 int ccin = i2c_smbus_read_word_swapped(client, CFFPS_CCIN_CMD);
501 if (ccin > 0) {
502 ccin_revision = FIELD_GET(CFFPS_CCIN_REVISION, ccin);
503 ccin_version = FIELD_GET(CFFPS_CCIN_VERSION, ccin);
506 switch (ccin_version) {
507 default:
508 case CFFPS_CCIN_VERSION_1:
509 vs = cffps1;
510 break;
511 case CFFPS_CCIN_VERSION_2:
512 vs = cffps2;
513 break;
514 case CFFPS_CCIN_VERSION_3:
515 if (ccin_revision == CFFPS_CCIN_REVISION_LEGACY)
516 vs = cffps1;
517 else
518 vs = cffps2;
519 break;
522 /* Set the client name to include the version number. */
523 snprintf(client->name, I2C_NAME_SIZE, "cffps%d", vs + 1);
526 client->dev.platform_data = &ibm_cffps_pdata;
527 rc = pmbus_do_probe(client, &ibm_cffps_info[vs]);
528 if (rc)
529 return rc;
532 * Don't fail the probe if there isn't enough memory for leds and
533 * debugfs.
535 psu = devm_kzalloc(&client->dev, sizeof(*psu), GFP_KERNEL);
536 if (!psu)
537 return 0;
539 psu->version = vs;
540 psu->client = client;
541 mutex_init(&psu->input_history.update_lock);
542 psu->input_history.last_update = jiffies - HZ;
544 ibm_cffps_create_led_class(psu);
546 /* Don't fail the probe if we can't create debugfs */
547 debugfs = pmbus_get_debugfs_dir(client);
548 if (!debugfs)
549 return 0;
551 ibm_cffps_dir = debugfs_create_dir(client->name, debugfs);
552 if (!ibm_cffps_dir)
553 return 0;
555 for (i = 0; i < CFFPS_DEBUGFS_NUM_ENTRIES; ++i)
556 psu->debugfs_entries[i] = i;
558 debugfs_create_file("input_history", 0444, ibm_cffps_dir,
559 &psu->debugfs_entries[CFFPS_DEBUGFS_INPUT_HISTORY],
560 &ibm_cffps_fops);
561 debugfs_create_file("fru", 0444, ibm_cffps_dir,
562 &psu->debugfs_entries[CFFPS_DEBUGFS_FRU],
563 &ibm_cffps_fops);
564 debugfs_create_file("part_number", 0444, ibm_cffps_dir,
565 &psu->debugfs_entries[CFFPS_DEBUGFS_PN],
566 &ibm_cffps_fops);
567 debugfs_create_file("header", 0444, ibm_cffps_dir,
568 &psu->debugfs_entries[CFFPS_DEBUGFS_HEADER],
569 &ibm_cffps_fops);
570 debugfs_create_file("serial_number", 0444, ibm_cffps_dir,
571 &psu->debugfs_entries[CFFPS_DEBUGFS_SN],
572 &ibm_cffps_fops);
573 debugfs_create_file("max_power_out", 0444, ibm_cffps_dir,
574 &psu->debugfs_entries[CFFPS_DEBUGFS_MAX_POWER_OUT],
575 &ibm_cffps_fops);
576 debugfs_create_file("ccin", 0444, ibm_cffps_dir,
577 &psu->debugfs_entries[CFFPS_DEBUGFS_CCIN],
578 &ibm_cffps_fops);
579 debugfs_create_file("fw_version", 0444, ibm_cffps_dir,
580 &psu->debugfs_entries[CFFPS_DEBUGFS_FW],
581 &ibm_cffps_fops);
582 debugfs_create_file("on_off_config", 0644, ibm_cffps_dir,
583 &psu->debugfs_entries[CFFPS_DEBUGFS_ON_OFF_CONFIG],
584 &ibm_cffps_fops);
586 return 0;
589 static const struct i2c_device_id ibm_cffps_id[] = {
590 { "ibm_cffps1", cffps1 },
591 { "ibm_cffps2", cffps2 },
592 { "ibm_cffps", cffps_unknown },
595 MODULE_DEVICE_TABLE(i2c, ibm_cffps_id);
597 static const struct of_device_id ibm_cffps_of_match[] = {
599 .compatible = "ibm,cffps1",
600 .data = (void *)cffps1
603 .compatible = "ibm,cffps2",
604 .data = (void *)cffps2
607 .compatible = "ibm,cffps",
608 .data = (void *)cffps_unknown
612 MODULE_DEVICE_TABLE(of, ibm_cffps_of_match);
614 static struct i2c_driver ibm_cffps_driver = {
615 .driver = {
616 .name = "ibm-cffps",
617 .of_match_table = ibm_cffps_of_match,
619 .probe_new = ibm_cffps_probe,
620 .id_table = ibm_cffps_id,
623 module_i2c_driver(ibm_cffps_driver);
625 MODULE_AUTHOR("Eddie James");
626 MODULE_DESCRIPTION("PMBus driver for IBM Common Form Factor power supplies");
627 MODULE_LICENSE("GPL");