]>
Commit | Line | Data |
---|---|---|
051ebe32 NA |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* | |
3 | * Copyright (C) 2018 BayLibre, SAS | |
4 | * Author: Neil Armstrong <[email protected]> | |
5 | */ | |
051ebe32 NA |
6 | #include <command.h> |
7 | #include <dm.h> | |
8 | #include <adc.h> | |
1e94b46f | 9 | #include <linux/printk.h> |
051ebe32 | 10 | |
09140113 | 11 | static int do_adc_list(struct cmd_tbl *cmdtp, int flag, int argc, |
051ebe32 NA |
12 | char *const argv[]) |
13 | { | |
14 | struct udevice *dev; | |
8676ae36 | 15 | int ret, err; |
051ebe32 | 16 | |
8676ae36 | 17 | ret = err = uclass_first_device_check(UCLASS_ADC, &dev); |
051ebe32 | 18 | |
8676ae36 MS |
19 | while (dev) { |
20 | printf("- %s status: %i\n", dev->name, ret); | |
051ebe32 | 21 | |
8676ae36 | 22 | ret = uclass_next_device_check(&dev); |
051ebe32 | 23 | if (ret) |
8676ae36 MS |
24 | err = ret; |
25 | } | |
051ebe32 | 26 | |
8676ae36 | 27 | return err ? CMD_RET_FAILURE : CMD_RET_SUCCESS; |
051ebe32 NA |
28 | } |
29 | ||
09140113 | 30 | static int do_adc_info(struct cmd_tbl *cmdtp, int flag, int argc, |
051ebe32 NA |
31 | char *const argv[]) |
32 | { | |
33 | struct udevice *dev; | |
1c84d904 | 34 | unsigned int data_mask, ch_mask; |
051ebe32 NA |
35 | int ret, vss, vdd; |
36 | ||
37 | if (argc < 2) | |
38 | return CMD_RET_USAGE; | |
39 | ||
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; | |
44 | } | |
45 | ||
46 | printf("ADC Device '%s' :\n", argv[1]); | |
47 | ||
1c84d904 FG |
48 | ret = adc_channel_mask(dev, &ch_mask); |
49 | if (!ret) | |
50 | printf("channel mask: %x\n", ch_mask); | |
51 | ||
051ebe32 NA |
52 | ret = adc_data_mask(dev, &data_mask); |
53 | if (!ret) | |
54 | printf("data mask: %x\n", data_mask); | |
55 | ||
56 | ret = adc_vdd_value(dev, &vdd); | |
57 | if (!ret) | |
58 | printf("vdd: %duV\n", vdd); | |
59 | ||
60 | ret = adc_vss_value(dev, &vss); | |
61 | if (!ret) | |
62 | printf("vss: %duV\n", vss); | |
63 | ||
64 | return CMD_RET_SUCCESS; | |
65 | } | |
66 | ||
09140113 | 67 | static int do_adc_single(struct cmd_tbl *cmdtp, int flag, int argc, |
051ebe32 NA |
68 | char *const argv[]) |
69 | { | |
6ac47744 | 70 | char *varname = NULL; |
c56fc49a | 71 | struct udevice *dev; |
051ebe32 | 72 | unsigned int data; |
9de612ae | 73 | int ret, uV, val; |
051ebe32 NA |
74 | |
75 | if (argc < 3) | |
76 | return CMD_RET_USAGE; | |
77 | ||
6ac47744 VB |
78 | if (argc >= 4) |
79 | varname = argv[3]; | |
80 | ||
051ebe32 NA |
81 | ret = adc_channel_single_shot(argv[1], simple_strtol(argv[2], NULL, 0), |
82 | &data); | |
83 | if (ret) { | |
4a05497a SDR |
84 | printf("Error getting single shot for device %s channel %s (ret=%d)\n", |
85 | argv[1], argv[2], ret); | |
051ebe32 NA |
86 | return CMD_RET_FAILURE; |
87 | } | |
88 | ||
c56fc49a | 89 | ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev); |
9de612ae MV |
90 | if (!ret && !adc_raw_to_uV(dev, data, &uV)) { |
91 | val = uV; | |
c56fc49a | 92 | printf("%u, %d uV\n", data, uV); |
9de612ae MV |
93 | } else { |
94 | val = data; | |
c56fc49a | 95 | printf("%u\n", data); |
9de612ae MV |
96 | } |
97 | ||
6ac47744 VB |
98 | if (varname) |
99 | env_set_ulong(varname, val); | |
051ebe32 NA |
100 | |
101 | return CMD_RET_SUCCESS; | |
102 | } | |
103 | ||
09140113 | 104 | static int do_adc_scan(struct cmd_tbl *cmdtp, int flag, int argc, |
61927d28 FG |
105 | char *const argv[]) |
106 | { | |
107 | struct adc_channel ch[ADC_MAX_CHANNEL]; | |
108 | struct udevice *dev; | |
109 | unsigned int ch_mask; | |
110 | int i, chan, ret, uV; | |
111 | ||
112 | if (argc < 2) | |
113 | return CMD_RET_USAGE; | |
114 | ||
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; | |
119 | } | |
120 | ||
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; | |
131 | } | |
132 | break; | |
133 | } | |
134 | ||
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; | |
140 | } | |
141 | ||
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++; | |
150 | } | |
151 | ||
152 | return CMD_RET_SUCCESS; | |
153 | } | |
154 | ||
b85ecb27 | 155 | U_BOOT_LONGHELP(adc, |
051ebe32 NA |
156 | "list - list ADC devices\n" |
157 | "adc info <name> - Get ADC device info\n" | |
9de612ae | 158 | "adc single <name> <channel> [varname] - Get Single data of ADC device channel\n" |
b85ecb27 | 159 | "adc scan <name> [channel mask] - Scan all [or masked] ADC channels\n"); |
051ebe32 | 160 | |
9652cfd9 BB |
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), | |
6ac47744 | 164 | U_BOOT_SUBCMD_MKENT(single, 4, 1, do_adc_single), |
9652cfd9 | 165 | U_BOOT_SUBCMD_MKENT(scan, 3, 1, do_adc_scan)); |