Merge tag 'xilinx-for-v2025.01-rc3-v2' of https://source.denx.de/u-boot/custodians...
[u-boot.git] / cmd / adc.c
blob4d3b5b61f6f0236c1d74db5b2a86adc38a94340b
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2018 BayLibre, SAS
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
5 */
6 #include <command.h>
7 #include <dm.h>
8 #include <adc.h>
9 #include <linux/printk.h>
11 static int do_adc_list(struct cmd_tbl *cmdtp, int flag, int argc,
12 char *const argv[])
14 struct udevice *dev;
15 int ret, err;
17 ret = err = uclass_first_device_check(UCLASS_ADC, &dev);
19 while (dev) {
20 printf("- %s status: %i\n", dev->name, ret);
22 ret = uclass_next_device_check(&dev);
23 if (ret)
24 err = ret;
27 return err ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
30 static int do_adc_info(struct cmd_tbl *cmdtp, int flag, int argc,
31 char *const argv[])
33 struct udevice *dev;
34 unsigned int data_mask, ch_mask;
35 int ret, vss, vdd;
37 if (argc < 2)
38 return CMD_RET_USAGE;
40 ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev);
41 if (ret) {
42 printf("Unknown ADC device %s\n", argv[1]);
43 return CMD_RET_FAILURE;
46 printf("ADC Device '%s' :\n", argv[1]);
48 ret = adc_channel_mask(dev, &ch_mask);
49 if (!ret)
50 printf("channel mask: %x\n", ch_mask);
52 ret = adc_data_mask(dev, &data_mask);
53 if (!ret)
54 printf("data mask: %x\n", data_mask);
56 ret = adc_vdd_value(dev, &vdd);
57 if (!ret)
58 printf("vdd: %duV\n", vdd);
60 ret = adc_vss_value(dev, &vss);
61 if (!ret)
62 printf("vss: %duV\n", vss);
64 return CMD_RET_SUCCESS;
67 static int do_adc_single(struct cmd_tbl *cmdtp, int flag, int argc,
68 char *const argv[])
70 char *varname = NULL;
71 struct udevice *dev;
72 unsigned int data;
73 int ret, uV, val;
75 if (argc < 3)
76 return CMD_RET_USAGE;
78 if (argc >= 4)
79 varname = argv[3];
81 ret = adc_channel_single_shot(argv[1], simple_strtol(argv[2], NULL, 0),
82 &data);
83 if (ret) {
84 printf("Error getting single shot for device %s channel %s (ret=%d)\n",
85 argv[1], argv[2], ret);
86 return CMD_RET_FAILURE;
89 ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev);
90 if (!ret && !adc_raw_to_uV(dev, data, &uV)) {
91 val = uV;
92 printf("%u, %d uV\n", data, uV);
93 } else {
94 val = data;
95 printf("%u\n", data);
98 if (varname)
99 env_set_ulong(varname, val);
101 return CMD_RET_SUCCESS;
104 static int do_adc_scan(struct cmd_tbl *cmdtp, int flag, int argc,
105 char *const argv[])
107 struct adc_channel ch[ADC_MAX_CHANNEL];
108 struct udevice *dev;
109 unsigned int ch_mask;
110 int i, chan, ret, uV;
112 if (argc < 2)
113 return CMD_RET_USAGE;
115 ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev);
116 if (ret) {
117 pr_err("Can't get the ADC %s: %d\n", argv[1], ret);
118 return CMD_RET_FAILURE;
121 switch (argc) {
122 case 3:
123 ch_mask = simple_strtoul(argv[2], NULL, 0);
124 if (ch_mask)
125 break;
126 case 2:
127 ret = adc_channel_mask(dev, &ch_mask);
128 if (ret) {
129 pr_err("Can't get mask for %s: %d\n", dev->name, ret);
130 return CMD_RET_FAILURE;
132 break;
135 ret = adc_channels_single_shot(dev->name, ch_mask, ch);
136 if (ret) {
137 pr_err("Can't get single shot for %s (chans mask: 0x%x): %d\n",
138 dev->name, ch_mask, ret);
139 return CMD_RET_FAILURE;
142 for (chan = 0, i = 0; chan < ADC_MAX_CHANNEL; chan++) {
143 if (!(ch_mask & ADC_CHANNEL(chan)))
144 continue;
145 if (!adc_raw_to_uV(dev, ch[i].data, &uV))
146 printf("[%02d]: %u, %d uV\n", ch[i].id, ch[i].data, uV);
147 else
148 printf("[%02d]: %u\n", ch[i].id, ch[i].data);
149 i++;
152 return CMD_RET_SUCCESS;
155 U_BOOT_LONGHELP(adc,
156 "list - list ADC devices\n"
157 "adc info <name> - Get ADC device info\n"
158 "adc single <name> <channel> [varname] - Get Single data of ADC device channel\n"
159 "adc scan <name> [channel mask] - Scan all [or masked] ADC channels\n");
161 U_BOOT_CMD_WITH_SUBCMDS(adc, "ADC sub-system", adc_help_text,
162 U_BOOT_SUBCMD_MKENT(list, 1, 1, do_adc_list),
163 U_BOOT_SUBCMD_MKENT(info, 2, 1, do_adc_info),
164 U_BOOT_SUBCMD_MKENT(single, 4, 1, do_adc_single),
165 U_BOOT_SUBCMD_MKENT(scan, 3, 1, do_adc_scan));