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
415
416
/*
 * Copyright 2022 NXP
 * Copyright 2022 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#include <zephyr/device.h>
#include <zephyr/shell/shell.h>
#include <zephyr/drivers/regulator.h>
#include <zephyr/toolchain.h>

static int strtomicro(char *inp, char units, int32_t *val)
{
	size_t len, start, end;
	int32_t mult, decdiv = 1;

	len = strlen(inp);
	if (len < 2) {
		return -EINVAL;
	}

	/* suffix */
	if (tolower(inp[len - 1]) != units) {
		return -EINVAL;
	}

	if ((len > 2) && (inp[len - 2] == 'u')) {
		mult = 1;
		end = len - 3;
	} else if ((len > 2) && (inp[len - 2] == 'm')) {
		mult = 1000;
		end = len - 3;
	} else if (isdigit((unsigned char)inp[len - 2]) > 0) {
		mult = 1000000;
		end = len - 2;
	} else {
		return -EINVAL;
	}

	/* optional prefix (sign) */
	if (inp[0] == '-') {
		mult *= -1;
		start = 1;
	} else if (inp[0] == '+') {
		start = 1;
	} else {
		start = 0;
	}

	/* numeric part */
	*val = 0;
	for (size_t i = start; (i <= end) && (decdiv <= mult); i++) {
		if (isdigit((unsigned char)inp[i]) > 0) {
			*val = *val * 10 / decdiv +
			       (int32_t)(inp[i] - '0') * mult / decdiv;
			if (decdiv > 1) {
				mult /= 10;
			}
		} else if (inp[i] == '.') {
			decdiv = 10;
		} else {
			return -EINVAL;
		}
	}

	return 0;
}

static void microtoshell(const struct shell *sh, char unit, int32_t val)
{
	if (val > 100000) {
		shell_print(sh, "%d.%03d %c", val / 1000000,
			    (val % 1000000) / 1000, unit);
	} else if (val > 1000) {
		shell_print(sh, "%d.%03d m%c", val / 1000, val % 1000, unit);
	} else {
		shell_print(sh, "%d u%c", val, unit);
	}
}

static int cmd_enable(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	int ret;

	ARG_UNUSED(argc);

	dev = device_get_binding(argv[1]);
	if (dev == NULL) {
		shell_error(sh, "Regulator device %s not available", argv[1]);
		return -ENODEV;
	}

	ret = regulator_enable(dev);
	if (ret < 0) {
		shell_error(sh, "Could not enable regulator (%d)", ret);
		return ret;
	}

	return 0;
}

static int cmd_disable(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	int ret;

	ARG_UNUSED(argc);

	dev = device_get_binding(argv[1]);
	if (dev == NULL) {
		shell_error(sh, "Regulator device %s not available", argv[1]);
		return -ENODEV;
	}

	ret = regulator_disable(dev);
	if (ret < 0) {
		shell_error(sh, "Could not disable regulator (%d)", ret);
		return ret;
	}

	return 0;
}

static int cmd_vlist(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	unsigned int volt_cnt;
	int32_t last_volt_uv;

	ARG_UNUSED(argc);

	dev = device_get_binding(argv[1]);
	if (dev == NULL) {
		shell_error(sh, "Regulator device %s not available", argv[1]);
		return -ENODEV;
	}

	volt_cnt = regulator_count_voltages(dev);

	for (unsigned int i = 0U; i < volt_cnt; i++) {
		int32_t volt_uv;

		(void)regulator_list_voltage(dev, i, &volt_uv);

		/* do not print repeated voltages */
		if ((i == 0U) || (last_volt_uv != volt_uv)) {
			microtoshell(sh, 'V', volt_uv);
		}

		last_volt_uv = volt_uv;
	}

	return 0;
}

static int cmd_vset(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	int32_t min_uv, max_uv;
	int ret;

	dev = device_get_binding(argv[1]);
	if (dev == NULL) {
		shell_error(sh, "Regulator device %s not available", argv[1]);
		return -ENODEV;
	}

	ret = strtomicro(argv[2], 'v', &min_uv);
	if (ret < 0) {
		shell_error(sh, "Invalid min. voltage: %s", argv[2]);
		return ret;
	}

	if (argc == 4) {
		ret = strtomicro(argv[3], 'v', &max_uv);
		if (ret < 0) {
			shell_error(sh, "Invalid max. voltage: %s", argv[3]);
			return ret;
		}
	} else {
		max_uv = min_uv;
	}

	ret = regulator_set_voltage(dev, min_uv, max_uv);
	if (ret < 0) {
		shell_error(sh, "Could not set voltage (%d)", ret);
		return ret;
	}

	return 0;
}

static int cmd_vget(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	int32_t volt_uv;
	int ret;

	ARG_UNUSED(argc);

	dev = device_get_binding(argv[1]);
	if (dev == NULL) {
		shell_error(sh, "Regulator device %s not available", argv[1]);
		return -ENODEV;
	}

	ret = regulator_get_voltage(dev, &volt_uv);
	if (ret < 0) {
		shell_error(sh, "Could not get voltage (%d)", ret);
		return ret;
	}

	microtoshell(sh, 'V', volt_uv);

	return 0;
}

static int cmd_iset(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	int32_t min_ua, max_ua;
	int ret;

	dev = device_get_binding(argv[1]);
	if (dev == NULL) {
		shell_error(sh, "Regulator device %s not available", argv[1]);
		return -ENODEV;
	}

	ret = strtomicro(argv[2], 'a', &min_ua);
	if (ret < 0) {
		shell_error(sh, "Invalid min. current: %s", argv[2]);
		return ret;
	}
	if (argc == 4) {
		ret = strtomicro(argv[3], 'a', &max_ua);
		if (ret < 0) {
			shell_error(sh, "Invalid max. current: %s", argv[3]);
			return ret;
		}
	} else {
		max_ua = min_ua;
	}

	ret = regulator_set_current_limit(dev, min_ua, max_ua);
	if (ret < 0) {
		shell_error(sh, "Could not set current limit (%d)", ret);
		return ret;
	}

	return 0;
}

static int cmd_iget(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	int32_t curr_ua;
	int ret;

	ARG_UNUSED(argc);

	dev = device_get_binding(argv[1]);
	if (dev == NULL) {
		shell_error(sh, "Regulator device %s not available", argv[1]);
		return -ENODEV;
	}

	ret = regulator_get_current_limit(dev, &curr_ua);
	if (ret < 0) {
		shell_error(sh, "Could not get current limit (%d)", ret);
		return ret;
	}

	microtoshell(sh, 'A', curr_ua);

	return 0;
}

static int cmd_modeset(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	regulator_mode_t mode;
	int ret;

	ARG_UNUSED(argc);

	dev = device_get_binding(argv[1]);
	if (dev == NULL) {
		shell_error(sh, "Regulator device %s not available", argv[1]);
		return -ENODEV;
	}

	mode = (regulator_mode_t)strtoul(argv[2], NULL, 10);

	ret = regulator_set_mode(dev, mode);
	if (ret < 0) {
		shell_error(sh, "Could not set mode (%d)", ret);
		return ret;
	}

	return 0;
}

static int cmd_modeget(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	regulator_mode_t mode;
	int ret;

	ARG_UNUSED(argc);

	dev = device_get_binding(argv[1]);
	if (dev == NULL) {
		shell_error(sh, "Regulator device %s not available", argv[1]);
		return -ENODEV;
	}

	ret = regulator_get_mode(dev, &mode);
	if (ret < 0) {
		shell_error(sh, "Could not get mode (%d)", ret);
		return ret;
	}

	shell_print(sh, "Mode: %u", (unsigned int)mode);

	return 0;
}

static int cmd_errors(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	regulator_error_flags_t errors;
	int ret;

	ARG_UNUSED(argc);

	dev = device_get_binding(argv[1]);
	if (dev == NULL) {
		shell_error(sh, "Regulator device %s not available", argv[1]);
		return -ENODEV;
	}

	ret = regulator_get_error_flags(dev, &errors);
	if (ret < 0) {
		shell_error(sh, "Could not get error flags (%d)", ret);
		return ret;
	}

	shell_print(sh, "Overvoltage:\t[%s]",
		    ((errors & REGULATOR_ERROR_OVER_VOLTAGE) != 0U) ? "X"
								    : " ");
	shell_print(sh, "Overcurrent:\t[%s]",
		    ((errors & REGULATOR_ERROR_OVER_CURRENT) != 0U) ? "X"
								    : " ");
	shell_print(sh, "Overtemp.:\t[%s]",
		    ((errors & REGULATOR_ERROR_OVER_TEMP) != 0U) ? "X" : " ");

	return 0;
}

SHELL_STATIC_SUBCMD_SET_CREATE(
	sub_regulator_cmds,
	SHELL_CMD_ARG(enable, NULL,
		      "Enable regulator\n"
		      "Usage: enable <device>",
		      cmd_enable, 2, 0),
	SHELL_CMD_ARG(disable, NULL,
		      "Disable regulator\n"
		      "Usage: disable <device>",
		      cmd_disable, 2, 0),
	SHELL_CMD_ARG(vlist, NULL,
		      "List all supported voltages\n"
		      "Usage: vlist <device>",
		      cmd_vlist, 2, 0),
	SHELL_CMD_ARG(vset, NULL,
		      "Set voltage\n"
		      "Input requires units, e.g. 200mv, 20.5mv, 10uv, 1v...\n"
		      "Usage: vset <device> <minimum> [<maximum>]\n"
		      "If maximum is not set, exact voltage will be requested",
		      cmd_vset, 3, 1),
	SHELL_CMD_ARG(vget, NULL,
		      "Get voltage\n"
		      "Usage: vget <device>",
		      cmd_vget, 2, 0),
	SHELL_CMD_ARG(iset, NULL,
		      "Set current limit\n"
		      "Input requires units, e.g. 200ma, 20.5ma, 10ua, 1a...\n"
		      "Usage: iset <device> <minimum> [<maximum>]"
		      "If maximum is not set, exact current will be requested",
		      cmd_iset, 3, 1),
	SHELL_CMD_ARG(iget, NULL,
		      "Get current limit\n"
		      "Usage: iget <device>",
		      cmd_iget, 2, 0),
	SHELL_CMD_ARG(modeset, NULL,
		      "Set regulator mode\n"
		      "Usage: modeset <device> <mode identifier>",
		      cmd_modeset, 3, 0),
	SHELL_CMD_ARG(modeget, NULL,
		      "Get regulator mode\n"
		      "Usage: modeget <device>",
		      cmd_modeget, 2, 0),
	SHELL_CMD_ARG(errors, NULL,
		      "Get errors\n"
		      "Usage: errors <device>",
		      cmd_errors, 2, 0),
	SHELL_SUBCMD_SET_END);

SHELL_CMD_REGISTER(regulator, &sub_regulator_cmds, "Regulator playground",
		   NULL);