]> Git Repo - J-u-boot.git/blame - cmd/adc.c
w1: Fix bus counting in w1_get_bus
[J-u-boot.git] / cmd / adc.c
CommitLineData
051ebe32
NA
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 BayLibre, SAS
4 * Author: Neil Armstrong <[email protected]>
5 */
6#include <common.h>
7#include <command.h>
8#include <dm.h>
9#include <adc.h>
10
09140113 11static int do_adc_list(struct cmd_tbl *cmdtp, int flag, int argc,
051ebe32
NA
12 char *const argv[])
13{
14 struct udevice *dev;
15 int ret;
16
17 ret = uclass_first_device_err(UCLASS_ADC, &dev);
18 if (ret) {
19 printf("No available ADC device\n");
20 return CMD_RET_FAILURE;
21 }
22
23 do {
24 printf("- %s\n", dev->name);
25
26 ret = uclass_next_device(&dev);
27 if (ret)
28 return CMD_RET_FAILURE;
29 } while (dev);
30
31 return CMD_RET_SUCCESS;
32}
33
09140113 34static int do_adc_info(struct cmd_tbl *cmdtp, int flag, int argc,
051ebe32
NA
35 char *const argv[])
36{
37 struct udevice *dev;
1c84d904 38 unsigned int data_mask, ch_mask;
051ebe32
NA
39 int ret, vss, vdd;
40
41 if (argc < 2)
42 return CMD_RET_USAGE;
43
44 ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev);
45 if (ret) {
46 printf("Unknown ADC device %s\n", argv[1]);
47 return CMD_RET_FAILURE;
48 }
49
50 printf("ADC Device '%s' :\n", argv[1]);
51
1c84d904
FG
52 ret = adc_channel_mask(dev, &ch_mask);
53 if (!ret)
54 printf("channel mask: %x\n", ch_mask);
55
051ebe32
NA
56 ret = adc_data_mask(dev, &data_mask);
57 if (!ret)
58 printf("data mask: %x\n", data_mask);
59
60 ret = adc_vdd_value(dev, &vdd);
61 if (!ret)
62 printf("vdd: %duV\n", vdd);
63
64 ret = adc_vss_value(dev, &vss);
65 if (!ret)
66 printf("vss: %duV\n", vss);
67
68 return CMD_RET_SUCCESS;
69}
70
09140113 71static int do_adc_single(struct cmd_tbl *cmdtp, int flag, int argc,
051ebe32
NA
72 char *const argv[])
73{
6ac47744 74 char *varname = NULL;
c56fc49a 75 struct udevice *dev;
051ebe32 76 unsigned int data;
9de612ae 77 int ret, uV, val;
051ebe32
NA
78
79 if (argc < 3)
80 return CMD_RET_USAGE;
81
6ac47744
VB
82 if (argc >= 4)
83 varname = argv[3];
84
051ebe32
NA
85 ret = adc_channel_single_shot(argv[1], simple_strtol(argv[2], NULL, 0),
86 &data);
87 if (ret) {
4a05497a
SDR
88 printf("Error getting single shot for device %s channel %s (ret=%d)\n",
89 argv[1], argv[2], ret);
051ebe32
NA
90 return CMD_RET_FAILURE;
91 }
92
c56fc49a 93 ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev);
9de612ae
MV
94 if (!ret && !adc_raw_to_uV(dev, data, &uV)) {
95 val = uV;
c56fc49a 96 printf("%u, %d uV\n", data, uV);
9de612ae
MV
97 } else {
98 val = data;
c56fc49a 99 printf("%u\n", data);
9de612ae
MV
100 }
101
6ac47744
VB
102 if (varname)
103 env_set_ulong(varname, val);
051ebe32
NA
104
105 return CMD_RET_SUCCESS;
106}
107
09140113 108static int do_adc_scan(struct cmd_tbl *cmdtp, int flag, int argc,
61927d28
FG
109 char *const argv[])
110{
111 struct adc_channel ch[ADC_MAX_CHANNEL];
112 struct udevice *dev;
113 unsigned int ch_mask;
114 int i, chan, ret, uV;
115
116 if (argc < 2)
117 return CMD_RET_USAGE;
118
119 ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev);
120 if (ret) {
121 pr_err("Can't get the ADC %s: %d\n", argv[1], ret);
122 return CMD_RET_FAILURE;
123 }
124
125 switch (argc) {
126 case 3:
127 ch_mask = simple_strtoul(argv[2], NULL, 0);
128 if (ch_mask)
129 break;
130 case 2:
131 ret = adc_channel_mask(dev, &ch_mask);
132 if (ret) {
133 pr_err("Can't get mask for %s: %d\n", dev->name, ret);
134 return CMD_RET_FAILURE;
135 }
136 break;
137 }
138
139 ret = adc_channels_single_shot(dev->name, ch_mask, ch);
140 if (ret) {
141 pr_err("Can't get single shot for %s (chans mask: 0x%x): %d\n",
142 dev->name, ch_mask, ret);
143 return CMD_RET_FAILURE;
144 }
145
146 for (chan = 0, i = 0; chan < ADC_MAX_CHANNEL; chan++) {
147 if (!(ch_mask & ADC_CHANNEL(chan)))
148 continue;
149 if (!adc_raw_to_uV(dev, ch[i].data, &uV))
150 printf("[%02d]: %u, %d uV\n", ch[i].id, ch[i].data, uV);
151 else
152 printf("[%02d]: %u\n", ch[i].id, ch[i].data);
153 i++;
154 }
155
156 return CMD_RET_SUCCESS;
157}
158
051ebe32
NA
159static char adc_help_text[] =
160 "list - list ADC devices\n"
161 "adc info <name> - Get ADC device info\n"
9de612ae 162 "adc single <name> <channel> [varname] - Get Single data of ADC device channel\n"
61927d28 163 "adc scan <name> [channel mask] - Scan all [or masked] ADC channels";
051ebe32 164
9652cfd9
BB
165U_BOOT_CMD_WITH_SUBCMDS(adc, "ADC sub-system", adc_help_text,
166 U_BOOT_SUBCMD_MKENT(list, 1, 1, do_adc_list),
167 U_BOOT_SUBCMD_MKENT(info, 2, 1, do_adc_info),
6ac47744 168 U_BOOT_SUBCMD_MKENT(single, 4, 1, do_adc_single),
9652cfd9 169 U_BOOT_SUBCMD_MKENT(scan, 3, 1, do_adc_scan));
This page took 0.168689 seconds and 4 git commands to generate.