Linux Audio

Check our new training course

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 * Copyright (c) 2018 Prevas A/S
 * Copyright (c) 2022 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stdlib.h>
#include <errno.h>
#include <zephyr/sys/slist.h>
#include <zephyr/drivers/smbus.h>
#include <zephyr/shell/shell.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(smbus_shell, CONFIG_LOG_DEFAULT_LEVEL);

/**
 * smbus_shell is a highly modified version from i2c_shell. Basically only scan
 * logic remains from i2c_shell
 */

/**
 * Simplify argument parsing, smbus arguments always go in this order:
 * smbus <shell command> <device> <peripheral address> <command byte>
 */
#define ARGV_DEV	1
#define ARGV_ADDR	2
#define ARGV_CMD	3

/**
 * This sends SMBUS messages without any data (i.e. stop condition after
 * sending just the address). If there is an ACK for the address, it
 * is assumed there is a device present.
 *
 * WARNING: As there is no standard SMBUS detection command, this code
 * uses arbitrary SMBus commands (namely SMBus quick write to probe for
 * devices.
 * This operation can confuse your SMBUS bus, cause data loss, and is
 * known to corrupt the Atmel AT24RF08 EEPROM found on many IBM
 * Thinkpad laptops.
 *
 * https://manpages.debian.org/buster/i2c-tools/i2cdetect.8.en.html
 */

/* smbus scan <device> */
static int cmd_smbus_scan(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	uint8_t cnt = 0, first = 0x04, last = 0x77;

	dev = device_get_binding(argv[ARGV_DEV]);
	if (!dev) {
		shell_error(sh, "SMBus: Device %s not found", argv[ARGV_DEV]);
		return -ENODEV;
	}

	shell_print(sh, "     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f");
	for (uint8_t i = 0; i <= last; i += 16) {
		shell_fprintf(sh, SHELL_NORMAL, "%02x: ", i);

		for (uint8_t j = 0; j < 16; j++) {
			if (i + j < first || i + j > last) {
				shell_fprintf(sh, SHELL_NORMAL, "   ");
				continue;
			}

			if (smbus_quick(dev, i + j, SMBUS_MSG_WRITE) == 0) {
				shell_fprintf(sh, SHELL_NORMAL, "%02x ", i + j);
				++cnt;
			} else {
				shell_fprintf(sh, SHELL_NORMAL, "-- ");
			}
		}

		shell_print(sh, "");
	}

	shell_print(sh, "%u devices found on %s", cnt, argv[ARGV_DEV]);

	return 0;
}

/* smbus quick <device> <dev_addr> */
static int cmd_smbus_quick(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	uint8_t addr;
	int ret;

	dev = device_get_binding(argv[ARGV_DEV]);
	if (!dev) {
		shell_error(sh, "SMBus: Device %s not found", argv[ARGV_DEV]);
		return -ENODEV;
	}

	addr = strtol(argv[ARGV_ADDR], NULL, 16);

	ret = smbus_quick(dev, addr, SMBUS_MSG_WRITE);
	if (ret < 0) {
		shell_error(sh, "SMBus: Failed quick cmd, perip: 0x%02x", addr);
	}

	return ret;
}

/* smbus byte_read <device> <dev_addr> */
static int cmd_smbus_byte_read(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	uint8_t addr;
	uint8_t out;
	int ret;

	dev = device_get_binding(argv[ARGV_DEV]);
	if (!dev) {
		shell_error(sh, "SMBus: Device %s not found", argv[ARGV_DEV]);
		return -ENODEV;
	}

	addr = strtol(argv[ARGV_ADDR], NULL, 16);

	ret = smbus_byte_read(dev, addr, &out);
	if (ret < 0) {
		shell_error(sh, "SMBus: Failed to read from periph: 0x%02x",
			    addr);
		return -EIO;
	}

	shell_print(sh, "Output: 0x%x", out);

	return 0;
}

/* smbus byte_write <device> <dev_addr> <value> */
static int cmd_smbus_byte_write(const struct shell *sh,
				size_t argc, char **argv)
{
	const struct device *dev;
	uint8_t addr;
	uint8_t value;
	int ret;

	dev = device_get_binding(argv[ARGV_DEV]);
	if (!dev) {
		shell_error(sh, "SMBus: Device %s not found", argv[ARGV_DEV]);
		return -ENODEV;
	}

	addr = strtol(argv[ARGV_ADDR], NULL, 16);
	/* First byte is command */
	value = strtol(argv[ARGV_CMD], NULL, 16);

	ret = smbus_byte_write(dev, addr, value);
	if (ret < 0) {
		shell_error(sh, "SMBus: Failed to write to periph: 0x%02x",
			    addr);
		return -EIO;
	}

	return 0;
}

/* smbus byte_data_read <device> <dev_addr> <cmd> */
static int cmd_smbus_byte_data_read(const struct shell *sh,
				    size_t argc, char **argv)
{
	const struct device *dev;
	uint8_t addr, command;
	uint8_t out;
	int ret;

	dev = device_get_binding(argv[ARGV_DEV]);
	if (!dev) {
		shell_error(sh, "SMBus: Device %s not found", argv[ARGV_DEV]);
		return -ENODEV;
	}

	addr = strtol(argv[ARGV_ADDR], NULL, 16);
	command = strtol(argv[ARGV_CMD], NULL, 16);

	ret = smbus_byte_data_read(dev, addr, command, &out);
	if (ret < 0) {
		shell_error(sh, "SMBus: Failed to read from periph: 0x%02x",
			    addr);
		return -EIO;
	}

	shell_print(sh, "Output: 0x%x", out);

	return 0;
}

/* smbus byte_data_write <device> <dev_addr> <cmd> <value> */
static int cmd_smbus_byte_data_write(const struct shell *sh,
				     size_t argc, char **argv)
{
	const struct device *dev;
	uint8_t addr, command;
	uint8_t value;
	int ret;

	dev = device_get_binding(argv[ARGV_DEV]);
	if (!dev) {
		shell_error(sh, "SMBus: Device %s not found", argv[ARGV_DEV]);
		return -ENODEV;
	}

	addr = strtol(argv[ARGV_ADDR], NULL, 16);
	command = strtol(argv[ARGV_CMD], NULL, 16);
	value = strtol(argv[4], NULL, 16);

	ret = smbus_byte_data_write(dev, addr, command, value);
	if (ret < 0) {
		shell_error(sh, "SMBus: Failed to write to periph: 0x%02x",
			    addr);
		return -EIO;
	}

	return 0;
}

/* smbus word_data_read <device> <dev_addr> <cmd> */
static int cmd_smbus_word_data_read(const struct shell *sh,
				    size_t argc, char **argv)
{
	const struct device *dev;
	uint8_t addr, command;
	uint16_t out;
	int ret;

	dev = device_get_binding(argv[ARGV_DEV]);
	if (!dev) {
		shell_error(sh, "SMBus: Device %s not found", argv[ARGV_DEV]);
		return -ENODEV;
	}

	addr = strtol(argv[ARGV_ADDR], NULL, 16);
	command = strtol(argv[ARGV_CMD], NULL, 16);

	ret = smbus_word_data_read(dev, addr, command, &out);
	if (ret < 0) {
		shell_error(sh, "SMBus: Failed to read from periph: 0x%02x",
			    addr);
		return -EIO;
	}

	shell_print(sh, "Output: 0x%04x", out);

	return 0;
}

/* smbus word_data_write <device> <dev_addr> <cmd> <value> */
static int cmd_smbus_word_data_write(const struct shell *sh,
				     size_t argc, char **argv)
{
	const struct device *dev;
	uint8_t addr, command;
	uint16_t value;
	int ret;

	dev = device_get_binding(argv[ARGV_DEV]);
	if (!dev) {
		shell_error(sh, "SMBus: Device %s not found", argv[ARGV_DEV]);
		return -ENODEV;
	}

	addr = strtol(argv[ARGV_ADDR], NULL, 16);
	command = strtol(argv[ARGV_CMD], NULL, 16);
	value = strtol(argv[4], NULL, 16);

	ret = smbus_word_data_write(dev, addr, command, value);
	if (ret < 0) {
		shell_error(sh, "SMBus: Failed to write to periph: 0x%02x",
			    addr);
		return -EIO;
	}

	return 0;
}

/* smbus block_write <device> <dev_addr> <cmd> <bytes ... > */
static int cmd_smbus_block_write(const struct shell *sh,
				 size_t argc, char **argv)
{
	const struct device *dev;
	uint8_t addr, command;
	uint8_t count = argc - 4;
	char **p = &argv[4]; /* start data bytes */
	uint8_t buf[32]; /* max block count */
	int ret;

	if (count == 0 || count > sizeof(buf)) {
		return -EINVAL;
	}

	dev = device_get_binding(argv[ARGV_DEV]);
	if (!dev) {
		shell_error(sh, "SMBus: Device %s not found", argv[ARGV_DEV]);
		return -ENODEV;
	}

	addr = strtol(argv[ARGV_ADDR], NULL, 16);
	command = strtol(argv[ARGV_CMD], NULL, 16);

	for (int i = 0; i < count; i++) {
		buf[i] = (uint8_t)strtoul(p[i], NULL, 16);
	}

	LOG_HEXDUMP_DBG(buf, count, "Constructed block buffer");

	ret = smbus_block_write(dev, addr, command, count, buf);
	if (ret < 0) {
		shell_error(sh, "Failed block write to periph: 0x%02x",
			    addr);
		return ret;
	}

	return 0;
}

/* smbus block_read <device> <dev_addr> <cmd> */
static int cmd_smbus_block_read(const struct shell *sh,
				size_t argc, char **argv)
{
	const struct device *dev;
	uint8_t addr, command;
	uint8_t buf[32]; /* max block count */
	uint8_t count;
	int ret;

	dev = device_get_binding(argv[ARGV_DEV]);
	if (!dev) {
		shell_error(sh, "SMBus: Device %s not found", argv[ARGV_DEV]);
		return -ENODEV;
	}

	addr = strtol(argv[ARGV_ADDR], NULL, 16);
	command = strtol(argv[ARGV_CMD], NULL, 16);

	ret = smbus_block_read(dev, addr, command, &count, buf);
	if (ret < 0) {
		shell_error(sh, "Failed block read from periph: 0x%02x",
			    addr);
		return ret;
	}

	if (count == 0 || count > sizeof(buf)) {
		shell_error(sh, "Returned count %u", count);
		return -ENODATA;
	}

	shell_hexdump(sh, buf, count);

	return 0;
}

/* Device name autocompletion support */
static void device_name_get(size_t idx, struct shell_static_entry *entry)
{
	const struct device *dev = shell_device_lookup(idx, "smbus");

	entry->syntax = (dev != NULL) ? dev->name : NULL;
	entry->handler = NULL;
	entry->help = NULL;
	entry->subcmd = NULL;
}

SHELL_DYNAMIC_CMD_CREATE(dsub_device_name, device_name_get);

SHELL_STATIC_SUBCMD_SET_CREATE(sub_smbus_cmds,
	SHELL_CMD_ARG(quick, &dsub_device_name,
		      "SMBus Quick command\n"
		      "Usage: quick <device> <addr>",
		      cmd_smbus_quick, 3, 0),
	SHELL_CMD_ARG(scan, &dsub_device_name,
		      "Scan SMBus peripheral devices command\n"
		      "Usage: scan <device>",
		      cmd_smbus_scan, 2, 0),
	SHELL_CMD_ARG(byte_read, &dsub_device_name,
		      "SMBus: byte read command\n"
		      "Usage: byte_read <device> <addr>",
		      cmd_smbus_byte_read, 3, 0),
	SHELL_CMD_ARG(byte_write, &dsub_device_name,
		      "SMBus: byte write command\n"
		      "Usage: byte_write <device> <addr> <value>",
		      cmd_smbus_byte_write, 4, 0),
	SHELL_CMD_ARG(byte_data_read, &dsub_device_name,
		      "SMBus: byte data read command\n"
		      "Usage: byte_data_read <device> <addr> <cmd>",
		      cmd_smbus_byte_data_read, 4, 0),
	SHELL_CMD_ARG(byte_data_write, &dsub_device_name,
		      "SMBus: byte data write command\n"
		      "Usage: byte_data_write <device> <addr> <cmd> <value>",
		      cmd_smbus_byte_data_write, 5, 0),
	SHELL_CMD_ARG(word_data_read, &dsub_device_name,
		      "SMBus: word data read command\n"
		      "Usage: word_data_read <device> <addr> <cmd>",
		      cmd_smbus_word_data_read, 4, 0),
	SHELL_CMD_ARG(word_data_write, &dsub_device_name,
		      "SMBus: word data write command\n"
		      "Usage: word_data_write <device> <addr> <cmd> <value>",
		      cmd_smbus_word_data_write, 5, 0),
	SHELL_CMD_ARG(block_write, &dsub_device_name,
		      "SMBus: Block Write command\n"
		      "Usage: block_write <device> <addr> <cmd> [<byte1>, ...]",
		      cmd_smbus_block_write, 4, 32),
	SHELL_CMD_ARG(block_read, &dsub_device_name,
		      "SMBus: Block Read command\n"
		      "Usage: block_read <device> <addr> <cmd>",
		      cmd_smbus_block_read, 4, 0),
	SHELL_SUBCMD_SET_END     /* Array terminated. */
);

SHELL_CMD_REGISTER(smbus, &sub_smbus_cmds, "smbus commands", NULL);