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 = err = uclass_first_device_check(UCLASS_ADC, &dev);
20 printf("- %s status: %i\n", dev->name, ret);
22 ret = uclass_next_device_check(&dev);
27 return err ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
30 static int do_adc_info(struct cmd_tbl *cmdtp, int flag, int argc,
34 unsigned int data_mask, ch_mask;
40 ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev);
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);
50 printf("channel mask: %x\n", ch_mask);
52 ret = adc_data_mask(dev, &data_mask);
54 printf("data mask: %x\n", data_mask);
56 ret = adc_vdd_value(dev, &vdd);
58 printf("vdd: %duV\n", vdd);
60 ret = adc_vss_value(dev, &vss);
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,
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)) {
92 printf("%u, %d uV\n", data, uV);
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,
107 struct adc_channel ch[ADC_MAX_CHANNEL];
109 unsigned int ch_mask;
110 int i, chan, ret, uV;
113 return CMD_RET_USAGE;
115 ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev);
117 pr_err("Can't get the ADC %s: %d\n", argv[1], ret);
118 return CMD_RET_FAILURE;
123 ch_mask = simple_strtoul(argv[2], NULL, 0);
127 ret = adc_channel_mask(dev, &ch_mask);
129 pr_err("Can't get mask for %s: %d\n", dev->name, ret);
130 return CMD_RET_FAILURE;
135 ret = adc_channels_single_shot(dev->name, ch_mask, ch);
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)))
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);
148 printf("[%02d]: %u\n", ch[i].id, ch[i].data);
152 return CMD_RET_SUCCESS;
155 static char adc_help_text[] =
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";
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));