blob: 4cb18b66d4aad3234b3a7b64a0e2546874e0b714 [file] [log] [blame]
Neil Armstrong051ebe32018-04-27 15:17:57 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 BayLibre, SAS
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
5 */
6#include <common.h>
7#include <command.h>
8#include <dm.h>
9#include <adc.h>
Simon Glass1e94b462023-09-14 18:21:46 -060010#include <linux/printk.h>
Neil Armstrong051ebe32018-04-27 15:17:57 +020011
Simon Glass09140112020-05-10 11:40:03 -060012static int do_adc_list(struct cmd_tbl *cmdtp, int flag, int argc,
Neil Armstrong051ebe32018-04-27 15:17:57 +020013 char *const argv[])
14{
15 struct udevice *dev;
Michal Suchanek8676ae32022-10-12 21:57:58 +020016 int ret, err;
Neil Armstrong051ebe32018-04-27 15:17:57 +020017
Michal Suchanek8676ae32022-10-12 21:57:58 +020018 ret = err = uclass_first_device_check(UCLASS_ADC, &dev);
19
20 while (dev) {
21 printf("- %s status: %i\n", dev->name, ret);
22
23 ret = uclass_next_device_check(&dev);
24 if (ret)
25 err = ret;
Neil Armstrong051ebe32018-04-27 15:17:57 +020026 }
27
Michal Suchanek8676ae32022-10-12 21:57:58 +020028 return err ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
Neil Armstrong051ebe32018-04-27 15:17:57 +020029}
30
Simon Glass09140112020-05-10 11:40:03 -060031static int do_adc_info(struct cmd_tbl *cmdtp, int flag, int argc,
Neil Armstrong051ebe32018-04-27 15:17:57 +020032 char *const argv[])
33{
34 struct udevice *dev;
Fabrice Gasnier1c84d902018-11-12 14:03:59 +010035 unsigned int data_mask, ch_mask;
Neil Armstrong051ebe32018-04-27 15:17:57 +020036 int ret, vss, vdd;
37
38 if (argc < 2)
39 return CMD_RET_USAGE;
40
41 ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev);
42 if (ret) {
43 printf("Unknown ADC device %s\n", argv[1]);
44 return CMD_RET_FAILURE;
45 }
46
47 printf("ADC Device '%s' :\n", argv[1]);
48
Fabrice Gasnier1c84d902018-11-12 14:03:59 +010049 ret = adc_channel_mask(dev, &ch_mask);
50 if (!ret)
51 printf("channel mask: %x\n", ch_mask);
52
Neil Armstrong051ebe32018-04-27 15:17:57 +020053 ret = adc_data_mask(dev, &data_mask);
54 if (!ret)
55 printf("data mask: %x\n", data_mask);
56
57 ret = adc_vdd_value(dev, &vdd);
58 if (!ret)
59 printf("vdd: %duV\n", vdd);
60
61 ret = adc_vss_value(dev, &vss);
62 if (!ret)
63 printf("vss: %duV\n", vss);
64
65 return CMD_RET_SUCCESS;
66}
67
Simon Glass09140112020-05-10 11:40:03 -060068static int do_adc_single(struct cmd_tbl *cmdtp, int flag, int argc,
Neil Armstrong051ebe32018-04-27 15:17:57 +020069 char *const argv[])
70{
Vyacheslav Bocharov6ac47742022-07-03 15:49:09 +030071 char *varname = NULL;
Fabrice Gasnierc56fc492018-11-12 14:04:00 +010072 struct udevice *dev;
Neil Armstrong051ebe32018-04-27 15:17:57 +020073 unsigned int data;
Marek Vasut9de612a2022-04-14 18:52:25 +020074 int ret, uV, val;
Neil Armstrong051ebe32018-04-27 15:17:57 +020075
76 if (argc < 3)
77 return CMD_RET_USAGE;
78
Vyacheslav Bocharov6ac47742022-07-03 15:49:09 +030079 if (argc >= 4)
80 varname = argv[3];
81
Neil Armstrong051ebe32018-04-27 15:17:57 +020082 ret = adc_channel_single_shot(argv[1], simple_strtol(argv[2], NULL, 0),
83 &data);
84 if (ret) {
Samuel Dionne-Riel4a054972021-12-20 18:23:45 -050085 printf("Error getting single shot for device %s channel %s (ret=%d)\n",
86 argv[1], argv[2], ret);
Neil Armstrong051ebe32018-04-27 15:17:57 +020087 return CMD_RET_FAILURE;
88 }
89
Fabrice Gasnierc56fc492018-11-12 14:04:00 +010090 ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev);
Marek Vasut9de612a2022-04-14 18:52:25 +020091 if (!ret && !adc_raw_to_uV(dev, data, &uV)) {
92 val = uV;
Fabrice Gasnierc56fc492018-11-12 14:04:00 +010093 printf("%u, %d uV\n", data, uV);
Marek Vasut9de612a2022-04-14 18:52:25 +020094 } else {
95 val = data;
Fabrice Gasnierc56fc492018-11-12 14:04:00 +010096 printf("%u\n", data);
Marek Vasut9de612a2022-04-14 18:52:25 +020097 }
98
Vyacheslav Bocharov6ac47742022-07-03 15:49:09 +030099 if (varname)
100 env_set_ulong(varname, val);
Neil Armstrong051ebe32018-04-27 15:17:57 +0200101
102 return CMD_RET_SUCCESS;
103}
104
Simon Glass09140112020-05-10 11:40:03 -0600105static int do_adc_scan(struct cmd_tbl *cmdtp, int flag, int argc,
Fabrice Gasnier61927d22018-11-12 14:04:01 +0100106 char *const argv[])
107{
108 struct adc_channel ch[ADC_MAX_CHANNEL];
109 struct udevice *dev;
110 unsigned int ch_mask;
111 int i, chan, ret, uV;
112
113 if (argc < 2)
114 return CMD_RET_USAGE;
115
116 ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev);
117 if (ret) {
118 pr_err("Can't get the ADC %s: %d\n", argv[1], ret);
119 return CMD_RET_FAILURE;
120 }
121
122 switch (argc) {
123 case 3:
124 ch_mask = simple_strtoul(argv[2], NULL, 0);
125 if (ch_mask)
126 break;
127 case 2:
128 ret = adc_channel_mask(dev, &ch_mask);
129 if (ret) {
130 pr_err("Can't get mask for %s: %d\n", dev->name, ret);
131 return CMD_RET_FAILURE;
132 }
133 break;
134 }
135
136 ret = adc_channels_single_shot(dev->name, ch_mask, ch);
137 if (ret) {
138 pr_err("Can't get single shot for %s (chans mask: 0x%x): %d\n",
139 dev->name, ch_mask, ret);
140 return CMD_RET_FAILURE;
141 }
142
143 for (chan = 0, i = 0; chan < ADC_MAX_CHANNEL; chan++) {
144 if (!(ch_mask & ADC_CHANNEL(chan)))
145 continue;
146 if (!adc_raw_to_uV(dev, ch[i].data, &uV))
147 printf("[%02d]: %u, %d uV\n", ch[i].id, ch[i].data, uV);
148 else
149 printf("[%02d]: %u\n", ch[i].id, ch[i].data);
150 i++;
151 }
152
153 return CMD_RET_SUCCESS;
154}
155
Neil Armstrong051ebe32018-04-27 15:17:57 +0200156static char adc_help_text[] =
157 "list - list ADC devices\n"
158 "adc info <name> - Get ADC device info\n"
Marek Vasut9de612a2022-04-14 18:52:25 +0200159 "adc single <name> <channel> [varname] - Get Single data of ADC device channel\n"
Fabrice Gasnier61927d22018-11-12 14:04:01 +0100160 "adc scan <name> [channel mask] - Scan all [or masked] ADC channels";
Neil Armstrong051ebe32018-04-27 15:17:57 +0200161
Boris Brezillon9652cfd2018-12-03 22:54:23 +0100162U_BOOT_CMD_WITH_SUBCMDS(adc, "ADC sub-system", adc_help_text,
163 U_BOOT_SUBCMD_MKENT(list, 1, 1, do_adc_list),
164 U_BOOT_SUBCMD_MKENT(info, 2, 1, do_adc_info),
Vyacheslav Bocharov6ac47742022-07-03 15:49:09 +0300165 U_BOOT_SUBCMD_MKENT(single, 4, 1, do_adc_single),
Boris Brezillon9652cfd2018-12-03 22:54:23 +0100166 U_BOOT_SUBCMD_MKENT(scan, 3, 1, do_adc_scan));