1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 BayLibre, SAS
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
11 static int do_adc_list(struct cmd_tbl *cmdtp, int flag, int argc,
17 ret = uclass_first_device_err(UCLASS_ADC, &dev);
19 printf("No available ADC device\n");
20 return CMD_RET_FAILURE;
24 printf("- %s\n", dev->name);
26 ret = uclass_next_device(&dev);
28 return CMD_RET_FAILURE;
31 return CMD_RET_SUCCESS;
34 static int do_adc_info(struct cmd_tbl *cmdtp, int flag, int argc,
38 unsigned int data_mask, ch_mask;
44 ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev);
46 printf("Unknown ADC device %s\n", argv[1]);
47 return CMD_RET_FAILURE;
50 printf("ADC Device '%s' :\n", argv[1]);
52 ret = adc_channel_mask(dev, &ch_mask);
54 printf("channel mask: %x\n", ch_mask);
56 ret = adc_data_mask(dev, &data_mask);
58 printf("data mask: %x\n", data_mask);
60 ret = adc_vdd_value(dev, &vdd);
62 printf("vdd: %duV\n", vdd);
64 ret = adc_vss_value(dev, &vss);
66 printf("vss: %duV\n", vss);
68 return CMD_RET_SUCCESS;
71 static int do_adc_single(struct cmd_tbl *cmdtp, int flag, int argc,
81 ret = adc_channel_single_shot(argv[1], simple_strtol(argv[2], NULL, 0),
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 printf("%u, %d uV\n", data, uV);
95 return CMD_RET_SUCCESS;
98 static int do_adc_scan(struct cmd_tbl *cmdtp, int flag, int argc,
101 struct adc_channel ch[ADC_MAX_CHANNEL];
103 unsigned int ch_mask;
104 int i, chan, ret, uV;
107 return CMD_RET_USAGE;
109 ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev);
111 pr_err("Can't get the ADC %s: %d\n", argv[1], ret);
112 return CMD_RET_FAILURE;
117 ch_mask = simple_strtoul(argv[2], NULL, 0);
121 ret = adc_channel_mask(dev, &ch_mask);
123 pr_err("Can't get mask for %s: %d\n", dev->name, ret);
124 return CMD_RET_FAILURE;
129 ret = adc_channels_single_shot(dev->name, ch_mask, ch);
131 pr_err("Can't get single shot for %s (chans mask: 0x%x): %d\n",
132 dev->name, ch_mask, ret);
133 return CMD_RET_FAILURE;
136 for (chan = 0, i = 0; chan < ADC_MAX_CHANNEL; chan++) {
137 if (!(ch_mask & ADC_CHANNEL(chan)))
139 if (!adc_raw_to_uV(dev, ch[i].data, &uV))
140 printf("[%02d]: %u, %d uV\n", ch[i].id, ch[i].data, uV);
142 printf("[%02d]: %u\n", ch[i].id, ch[i].data);
146 return CMD_RET_SUCCESS;
149 static char adc_help_text[] =
150 "list - list ADC devices\n"
151 "adc info <name> - Get ADC device info\n"
152 "adc single <name> <channel> - Get Single data of ADC device channel\n"
153 "adc scan <name> [channel mask] - Scan all [or masked] ADC channels";
155 U_BOOT_CMD_WITH_SUBCMDS(adc, "ADC sub-system", adc_help_text,
156 U_BOOT_SUBCMD_MKENT(list, 1, 1, do_adc_list),
157 U_BOOT_SUBCMD_MKENT(info, 2, 1, do_adc_info),
158 U_BOOT_SUBCMD_MKENT(single, 3, 1, do_adc_single),
159 U_BOOT_SUBCMD_MKENT(scan, 3, 1, do_adc_scan));