xref: /openbmc/linux/drivers/hwmon/pmbus/ibm-cffps.c (revision 9a6b55ac)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright 2017 IBM Corp.
4  */
5 
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>
18 
19 #include "pmbus.h"
20 
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
29 
30 #define CFFPS_INPUT_HISTORY_CMD			0xD6
31 #define CFFPS_INPUT_HISTORY_SIZE		100
32 
33 #define CFFPS_CCIN_VERSION			GENMASK(15, 8)
34 #define CFFPS_CCIN_VERSION_1			 0x2b
35 #define CFFPS_CCIN_VERSION_2			 0x2e
36 
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)
46 
47 /*
48  * LED off state actually relinquishes LED control to PSU firmware, so it can
49  * turn on the LED for faults.
50  */
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
55 
56 enum {
57 	CFFPS_DEBUGFS_INPUT_HISTORY = 0,
58 	CFFPS_DEBUGFS_FRU,
59 	CFFPS_DEBUGFS_PN,
60 	CFFPS_DEBUGFS_SN,
61 	CFFPS_DEBUGFS_CCIN,
62 	CFFPS_DEBUGFS_FW,
63 	CFFPS_DEBUGFS_NUM_ENTRIES
64 };
65 
66 enum versions { cffps1, cffps2, cffps_unknown };
67 
68 struct ibm_cffps_input_history {
69 	struct mutex update_lock;
70 	unsigned long last_update;
71 
72 	u8 byte_count;
73 	u8 data[CFFPS_INPUT_HISTORY_SIZE];
74 };
75 
76 struct ibm_cffps {
77 	enum versions version;
78 	struct i2c_client *client;
79 
80 	struct ibm_cffps_input_history input_history;
81 
82 	int debugfs_entries[CFFPS_DEBUGFS_NUM_ENTRIES];
83 
84 	char led_name[32];
85 	u8 led_state;
86 	struct led_classdev led;
87 };
88 
89 #define to_psu(x, y) container_of((x), struct ibm_cffps, debugfs_entries[(y)])
90 
91 static ssize_t ibm_cffps_read_input_history(struct ibm_cffps *psu,
92 					    char __user *buf, size_t count,
93 					    loff_t *ppos)
94 {
95 	int rc;
96 	u8 msgbuf0[1] = { CFFPS_INPUT_HISTORY_CMD };
97 	u8 msgbuf1[CFFPS_INPUT_HISTORY_SIZE + 1] = { 0 };
98 	struct i2c_msg msg[2] = {
99 		{
100 			.addr = psu->client->addr,
101 			.flags = psu->client->flags,
102 			.len = 1,
103 			.buf = msgbuf0,
104 		}, {
105 			.addr = psu->client->addr,
106 			.flags = psu->client->flags | I2C_M_RD,
107 			.len = CFFPS_INPUT_HISTORY_SIZE + 1,
108 			.buf = msgbuf1,
109 		},
110 	};
111 
112 	if (!*ppos) {
113 		mutex_lock(&psu->input_history.update_lock);
114 		if (time_after(jiffies, psu->input_history.last_update + HZ)) {
115 			/*
116 			 * Use a raw i2c transfer, since we need more bytes
117 			 * than Linux I2C supports through smbus xfr (only 32).
118 			 */
119 			rc = i2c_transfer(psu->client->adapter, msg, 2);
120 			if (rc < 0) {
121 				mutex_unlock(&psu->input_history.update_lock);
122 				return rc;
123 			}
124 
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;
129 		}
130 
131 		mutex_unlock(&psu->input_history.update_lock);
132 	}
133 
134 	return simple_read_from_buffer(buf, count, ppos,
135 				       psu->input_history.data,
136 				       psu->input_history.byte_count);
137 }
138 
139 static ssize_t ibm_cffps_debugfs_op(struct file *file, char __user *buf,
140 				    size_t count, loff_t *ppos)
141 {
142 	u8 cmd;
143 	int i, rc;
144 	int *idxp = file->private_data;
145 	int idx = *idxp;
146 	struct ibm_cffps *psu = to_psu(idxp, idx);
147 	char data[I2C_SMBUS_BLOCK_MAX] = { 0 };
148 
149 	pmbus_set_page(psu->client, 0);
150 
151 	switch (idx) {
152 	case CFFPS_DEBUGFS_INPUT_HISTORY:
153 		return ibm_cffps_read_input_history(psu, buf, count, ppos);
154 	case CFFPS_DEBUGFS_FRU:
155 		cmd = CFFPS_FRU_CMD;
156 		break;
157 	case CFFPS_DEBUGFS_PN:
158 		cmd = CFFPS_PN_CMD;
159 		break;
160 	case CFFPS_DEBUGFS_SN:
161 		cmd = CFFPS_SN_CMD;
162 		break;
163 	case CFFPS_DEBUGFS_CCIN:
164 		rc = i2c_smbus_read_word_swapped(psu->client, CFFPS_CCIN_CMD);
165 		if (rc < 0)
166 			return rc;
167 
168 		rc = snprintf(data, 5, "%04X", rc);
169 		goto done;
170 	case CFFPS_DEBUGFS_FW:
171 		switch (psu->version) {
172 		case cffps1:
173 			for (i = 0; i < CFFPS1_FW_NUM_BYTES; ++i) {
174 				rc = i2c_smbus_read_byte_data(psu->client,
175 							      CFFPS_FW_CMD +
176 								i);
177 				if (rc < 0)
178 					return rc;
179 
180 				snprintf(&data[i * 2], 3, "%02X", rc);
181 			}
182 
183 			rc = i * 2;
184 			break;
185 		case cffps2:
186 			for (i = 0; i < CFFPS2_FW_NUM_WORDS; ++i) {
187 				rc = i2c_smbus_read_word_data(psu->client,
188 							      CFFPS_FW_CMD +
189 								i);
190 				if (rc < 0)
191 					return rc;
192 
193 				snprintf(&data[i * 4], 5, "%04X", rc);
194 			}
195 
196 			rc = i * 4;
197 			break;
198 		default:
199 			return -EOPNOTSUPP;
200 		}
201 		goto done;
202 	default:
203 		return -EINVAL;
204 	}
205 
206 	rc = i2c_smbus_read_block_data(psu->client, cmd, data);
207 	if (rc < 0)
208 		return rc;
209 
210 done:
211 	data[rc] = '\n';
212 	rc += 2;
213 
214 	return simple_read_from_buffer(buf, count, ppos, data, rc);
215 }
216 
217 static const struct file_operations ibm_cffps_fops = {
218 	.llseek = noop_llseek,
219 	.read = ibm_cffps_debugfs_op,
220 	.open = simple_open,
221 };
222 
223 static int ibm_cffps_read_byte_data(struct i2c_client *client, int page,
224 				    int reg)
225 {
226 	int rc, mfr;
227 
228 	switch (reg) {
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);
234 		if (rc < 0)
235 			return rc;
236 
237 		mfr = pmbus_read_byte_data(client, page,
238 					   PMBUS_STATUS_MFR_SPECIFIC);
239 		if (mfr < 0)
240 			/*
241 			 * Return the status register instead of an error,
242 			 * since we successfully read status.
243 			 */
244 			return rc;
245 
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;
263 		}
264 		break;
265 	default:
266 		rc = -ENODATA;
267 		break;
268 	}
269 
270 	return rc;
271 }
272 
273 static int ibm_cffps_read_word_data(struct i2c_client *client, int page,
274 				    int reg)
275 {
276 	int rc, mfr;
277 
278 	switch (reg) {
279 	case PMBUS_STATUS_WORD:
280 		rc = pmbus_read_word_data(client, page, reg);
281 		if (rc < 0)
282 			return rc;
283 
284 		mfr = pmbus_read_byte_data(client, page,
285 					   PMBUS_STATUS_MFR_SPECIFIC);
286 		if (mfr < 0)
287 			/*
288 			 * Return the status register instead of an error,
289 			 * since we successfully read status.
290 			 */
291 			return rc;
292 
293 		if (mfr & CFFPS_MFR_PS_KILL)
294 			rc |= PB_STATUS_OFF;
295 		break;
296 	default:
297 		rc = -ENODATA;
298 		break;
299 	}
300 
301 	return rc;
302 }
303 
304 static int ibm_cffps_led_brightness_set(struct led_classdev *led_cdev,
305 					enum led_brightness brightness)
306 {
307 	int rc;
308 	u8 next_led_state;
309 	struct ibm_cffps *psu = container_of(led_cdev, struct ibm_cffps, led);
310 
311 	if (brightness == LED_OFF) {
312 		next_led_state = CFFPS_LED_OFF;
313 	} else {
314 		brightness = LED_FULL;
315 
316 		if (psu->led_state != CFFPS_LED_BLINK)
317 			next_led_state = CFFPS_LED_ON;
318 		else
319 			next_led_state = CFFPS_LED_BLINK;
320 	}
321 
322 	dev_dbg(&psu->client->dev, "LED brightness set: %d. Command: %d.\n",
323 		brightness, next_led_state);
324 
325 	pmbus_set_page(psu->client, 0);
326 
327 	rc = i2c_smbus_write_byte_data(psu->client, CFFPS_SYS_CONFIG_CMD,
328 				       next_led_state);
329 	if (rc < 0)
330 		return rc;
331 
332 	psu->led_state = next_led_state;
333 	led_cdev->brightness = brightness;
334 
335 	return 0;
336 }
337 
338 static int ibm_cffps_led_blink_set(struct led_classdev *led_cdev,
339 				   unsigned long *delay_on,
340 				   unsigned long *delay_off)
341 {
342 	int rc;
343 	struct ibm_cffps *psu = container_of(led_cdev, struct ibm_cffps, led);
344 
345 	dev_dbg(&psu->client->dev, "LED blink set.\n");
346 
347 	pmbus_set_page(psu->client, 0);
348 
349 	rc = i2c_smbus_write_byte_data(psu->client, CFFPS_SYS_CONFIG_CMD,
350 				       CFFPS_LED_BLINK);
351 	if (rc < 0)
352 		return rc;
353 
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;
358 
359 	return 0;
360 }
361 
362 static void ibm_cffps_create_led_class(struct ibm_cffps *psu)
363 {
364 	int rc;
365 	struct i2c_client *client = psu->client;
366 	struct device *dev = &client->dev;
367 
368 	snprintf(psu->led_name, sizeof(psu->led_name), "%s-%02x", client->name,
369 		 client->addr);
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;
374 
375 	rc = devm_led_classdev_register(dev, &psu->led);
376 	if (rc)
377 		dev_warn(dev, "failed to register led class: %d\n", rc);
378 }
379 
380 static struct pmbus_driver_info ibm_cffps_info[] = {
381 	[cffps1] = {
382 		.pages = 1,
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,
391 	},
392 	[cffps2] = {
393 		.pages = 2,
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,
405 	},
406 };
407 
408 static struct pmbus_platform_data ibm_cffps_pdata = {
409 	.flags = PMBUS_SKIP_STATUS_CHECK,
410 };
411 
412 static int ibm_cffps_probe(struct i2c_client *client,
413 			   const struct i2c_device_id *id)
414 {
415 	int i, rc;
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);
421 
422 	if (md)
423 		vs = (enum versions)md;
424 	else if (id)
425 		vs = (enum versions)id->driver_data;
426 
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);
430 
431 		if (ccin > 0)
432 			ccin_version = FIELD_GET(CFFPS_CCIN_VERSION, ccin);
433 
434 		switch (ccin_version) {
435 		default:
436 		case CFFPS_CCIN_VERSION_1:
437 			vs = cffps1;
438 			break;
439 		case CFFPS_CCIN_VERSION_2:
440 			vs = cffps2;
441 			break;
442 		}
443 
444 		/* Set the client name to include the version number. */
445 		snprintf(client->name, I2C_NAME_SIZE, "cffps%d", vs + 1);
446 	}
447 
448 	client->dev.platform_data = &ibm_cffps_pdata;
449 	rc = pmbus_do_probe(client, id, &ibm_cffps_info[vs]);
450 	if (rc)
451 		return rc;
452 
453 	/*
454 	 * Don't fail the probe if there isn't enough memory for leds and
455 	 * debugfs.
456 	 */
457 	psu = devm_kzalloc(&client->dev, sizeof(*psu), GFP_KERNEL);
458 	if (!psu)
459 		return 0;
460 
461 	psu->version = vs;
462 	psu->client = client;
463 	mutex_init(&psu->input_history.update_lock);
464 	psu->input_history.last_update = jiffies - HZ;
465 
466 	ibm_cffps_create_led_class(psu);
467 
468 	/* Don't fail the probe if we can't create debugfs */
469 	debugfs = pmbus_get_debugfs_dir(client);
470 	if (!debugfs)
471 		return 0;
472 
473 	ibm_cffps_dir = debugfs_create_dir(client->name, debugfs);
474 	if (!ibm_cffps_dir)
475 		return 0;
476 
477 	for (i = 0; i < CFFPS_DEBUGFS_NUM_ENTRIES; ++i)
478 		psu->debugfs_entries[i] = i;
479 
480 	debugfs_create_file("input_history", 0444, ibm_cffps_dir,
481 			    &psu->debugfs_entries[CFFPS_DEBUGFS_INPUT_HISTORY],
482 			    &ibm_cffps_fops);
483 	debugfs_create_file("fru", 0444, ibm_cffps_dir,
484 			    &psu->debugfs_entries[CFFPS_DEBUGFS_FRU],
485 			    &ibm_cffps_fops);
486 	debugfs_create_file("part_number", 0444, ibm_cffps_dir,
487 			    &psu->debugfs_entries[CFFPS_DEBUGFS_PN],
488 			    &ibm_cffps_fops);
489 	debugfs_create_file("serial_number", 0444, ibm_cffps_dir,
490 			    &psu->debugfs_entries[CFFPS_DEBUGFS_SN],
491 			    &ibm_cffps_fops);
492 	debugfs_create_file("ccin", 0444, ibm_cffps_dir,
493 			    &psu->debugfs_entries[CFFPS_DEBUGFS_CCIN],
494 			    &ibm_cffps_fops);
495 	debugfs_create_file("fw_version", 0444, ibm_cffps_dir,
496 			    &psu->debugfs_entries[CFFPS_DEBUGFS_FW],
497 			    &ibm_cffps_fops);
498 
499 	return 0;
500 }
501 
502 static const struct i2c_device_id ibm_cffps_id[] = {
503 	{ "ibm_cffps1", cffps1 },
504 	{ "ibm_cffps2", cffps2 },
505 	{ "ibm_cffps", cffps_unknown },
506 	{}
507 };
508 MODULE_DEVICE_TABLE(i2c, ibm_cffps_id);
509 
510 static const struct of_device_id ibm_cffps_of_match[] = {
511 	{
512 		.compatible = "ibm,cffps1",
513 		.data = (void *)cffps1
514 	},
515 	{
516 		.compatible = "ibm,cffps2",
517 		.data = (void *)cffps2
518 	},
519 	{
520 		.compatible = "ibm,cffps",
521 		.data = (void *)cffps_unknown
522 	},
523 	{}
524 };
525 MODULE_DEVICE_TABLE(of, ibm_cffps_of_match);
526 
527 static struct i2c_driver ibm_cffps_driver = {
528 	.driver = {
529 		.name = "ibm-cffps",
530 		.of_match_table = ibm_cffps_of_match,
531 	},
532 	.probe = ibm_cffps_probe,
533 	.remove = pmbus_do_remove,
534 	.id_table = ibm_cffps_id,
535 };
536 
537 module_i2c_driver(ibm_cffps_driver);
538 
539 MODULE_AUTHOR("Eddie James");
540 MODULE_DESCRIPTION("PMBus driver for IBM Common Form Factor power supplies");
541 MODULE_LICENSE("GPL");
542