blob: 4cb18b66d4aad3234b3a7b64a0e2546874e0b714 [file] [log] [blame]
Neil Armstrong49261c72018-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 Glassbdd5f812023-09-14 18:21:46 -060010#include <linux/printk.h>
Neil Armstrong49261c72018-04-27 15:17:57 +020011
Simon Glassed38aef2020-05-10 11:40:03 -060012static int do_adc_list(struct cmd_tbl *cmdtp, int flag, int argc,
Neil Armstrong49261c72018-04-27 15:17:57 +020013 char *const argv[])
14{
15 struct udevice *dev;
Michal Suchanek1d524ae2022-10-12 21:57:58 +020016 int ret, err;
Neil Armstrong49261c72018-04-27 15:17:57 +020017
Michal Suchanek1d524ae2022-10-12 21:57:58 +020018 ret = err = uclass_first_device_check(UCLASS_ADC, &dev);
Neil Armstrong49261c72018-04-27 15:17:57 +020019
Michal Suchanek1d524ae2022-10-12 21:57:58 +020020 while (dev) {
21 printf("- %s status: %i\n", dev->name, ret);
Neil Armstrong49261c72018-04-27 15:17:57 +020022
Michal Suchanek1d524ae2022-10-12 21:57:58 +020023 ret = uclass_next_device_check(&dev);
Neil Armstrong49261c72018-04-27 15:17:57 +020024 if (ret)
Michal Suchanek1d524ae2022-10-12 21:57:58 +020025 err = ret;
26 }
Neil Armstrong49261c72018-04-27 15:17:57 +020027
Michal Suchanek1d524ae2022-10-12 21:57:58 +020028 return err ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
Neil Armstrong49261c72018-04-27 15:17:57 +020029}
30
Simon Glassed38aef2020-05-10 11:40:03 -060031static int do_adc_info(struct cmd_tbl *cmdtp, int flag, int argc,
Neil Armstrong49261c72018-04-27 15:17:57 +020032 char *const argv[])
33{
34 struct udevice *dev;
Fabrice Gasnierfb408882018-11-12 14:03:59 +010035 unsigned int data_mask, ch_mask;
Neil Armstrong49261c72018-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 Gasnierfb408882018-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 Armstrong49261c72018-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 Glassed38aef2020-05-10 11:40:03 -060068static int do_adc_single(struct cmd_tbl *cmdtp, int flag, int argc,
Neil Armstrong49261c72018-04-27 15:17:57 +020069 char *const argv[])
70{
Vyacheslav Bocharov8985d722022-07-03 15:49:09 +030071 char *varname = NULL;
Fabrice Gasnier05550442018-11-12 14:04:00 +010072 struct udevice *dev;
Neil Armstrong49261c72018-04-27 15:17:57 +020073 unsigned int data;
Marek Vasutbeea7be2022-04-14 18:52:25 +020074 int ret, uV, val;
Neil Armstrong49261c72018-04-27 15:17:57 +020075
76 if (argc < 3)
77 return CMD_RET_USAGE;
78
Vyacheslav Bocharov8985d722022-07-03 15:49:09 +030079 if (argc >= 4)
80 varname = argv[3];
81
Neil Armstrong49261c72018-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-Riel9c7d35a2021-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 Armstrong49261c72018-04-27 15:17:57 +020087 return CMD_RET_FAILURE;
88 }
89
Fabrice Gasnier05550442018-11-12 14:04:00 +010090 ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev);
Marek Vasutbeea7be2022-04-14 18:52:25 +020091 if (!ret && !adc_raw_to_uV(dev, data, &uV)) {
92 val = uV;
Fabrice Gasnier05550442018-11-12 14:04:00 +010093 printf("%u, %d uV\n", data, uV);
Marek Vasutbeea7be2022-04-14 18:52:25 +020094 } else {
95 val = data;
Fabrice Gasnier05550442018-11-12 14:04:00 +010096 printf("%u\n", data);
Marek Vasutbeea7be2022-04-14 18:52:25 +020097 }
98
Vyacheslav Bocharov8985d722022-07-03 15:49:09 +030099 if (varname)
100 env_set_ulong(varname, val);
Neil Armstrong49261c72018-04-27 15:17:57 +0200101
102 return CMD_RET_SUCCESS;
103}
104
Simon Glassed38aef2020-05-10 11:40:03 -0600105static int do_adc_scan(struct cmd_tbl *cmdtp, int flag, int argc,
Fabrice Gasnier5b2449c2018-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 Armstrong49261c72018-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 Vasutbeea7be2022-04-14 18:52:25 +0200159 "adc single <name> <channel> [varname] - Get Single data of ADC device channel\n"
Fabrice Gasnier5b2449c2018-11-12 14:04:01 +0100160 "adc scan <name> [channel mask] - Scan all [or masked] ADC channels";
Neil Armstrong49261c72018-04-27 15:17:57 +0200161
Boris Brezillon878acd72018-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 Bocharov8985d722022-07-03 15:49:09 +0300165 U_BOOT_SUBCMD_MKENT(single, 4, 1, do_adc_single),
Boris Brezillon878acd72018-12-03 22:54:23 +0100166 U_BOOT_SUBCMD_MKENT(scan, 3, 1, do_adc_scan));