blob: f87f9785a114a46dde98d6b860af5ef51fec52f8 [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 */
Neil Armstrong49261c72018-04-27 15:17:57 +02006#include <command.h>
7#include <dm.h>
8#include <adc.h>
Simon Glassbdd5f812023-09-14 18:21:46 -06009#include <linux/printk.h>
Neil Armstrong49261c72018-04-27 15:17:57 +020010
Simon Glassed38aef2020-05-10 11:40:03 -060011static int do_adc_list(struct cmd_tbl *cmdtp, int flag, int argc,
Neil Armstrong49261c72018-04-27 15:17:57 +020012 char *const argv[])
13{
14 struct udevice *dev;
Michal Suchanek1d524ae2022-10-12 21:57:58 +020015 int ret, err;
Neil Armstrong49261c72018-04-27 15:17:57 +020016
Michal Suchanek1d524ae2022-10-12 21:57:58 +020017 ret = err = uclass_first_device_check(UCLASS_ADC, &dev);
Neil Armstrong49261c72018-04-27 15:17:57 +020018
Michal Suchanek1d524ae2022-10-12 21:57:58 +020019 while (dev) {
20 printf("- %s status: %i\n", dev->name, ret);
Neil Armstrong49261c72018-04-27 15:17:57 +020021
Michal Suchanek1d524ae2022-10-12 21:57:58 +020022 ret = uclass_next_device_check(&dev);
Neil Armstrong49261c72018-04-27 15:17:57 +020023 if (ret)
Michal Suchanek1d524ae2022-10-12 21:57:58 +020024 err = ret;
25 }
Neil Armstrong49261c72018-04-27 15:17:57 +020026
Michal Suchanek1d524ae2022-10-12 21:57:58 +020027 return err ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
Neil Armstrong49261c72018-04-27 15:17:57 +020028}
29
Simon Glassed38aef2020-05-10 11:40:03 -060030static int do_adc_info(struct cmd_tbl *cmdtp, int flag, int argc,
Neil Armstrong49261c72018-04-27 15:17:57 +020031 char *const argv[])
32{
33 struct udevice *dev;
Fabrice Gasnierfb408882018-11-12 14:03:59 +010034 unsigned int data_mask, ch_mask;
Neil Armstrong49261c72018-04-27 15:17:57 +020035 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
Fabrice Gasnierfb408882018-11-12 14:03:59 +010048 ret = adc_channel_mask(dev, &ch_mask);
49 if (!ret)
50 printf("channel mask: %x\n", ch_mask);
51
Neil Armstrong49261c72018-04-27 15:17:57 +020052 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
Simon Glassed38aef2020-05-10 11:40:03 -060067static int do_adc_single(struct cmd_tbl *cmdtp, int flag, int argc,
Neil Armstrong49261c72018-04-27 15:17:57 +020068 char *const argv[])
69{
Vyacheslav Bocharov8985d722022-07-03 15:49:09 +030070 char *varname = NULL;
Fabrice Gasnier05550442018-11-12 14:04:00 +010071 struct udevice *dev;
Neil Armstrong49261c72018-04-27 15:17:57 +020072 unsigned int data;
Marek Vasutbeea7be2022-04-14 18:52:25 +020073 int ret, uV, val;
Neil Armstrong49261c72018-04-27 15:17:57 +020074
75 if (argc < 3)
76 return CMD_RET_USAGE;
77
Vyacheslav Bocharov8985d722022-07-03 15:49:09 +030078 if (argc >= 4)
79 varname = argv[3];
80
Neil Armstrong49261c72018-04-27 15:17:57 +020081 ret = adc_channel_single_shot(argv[1], simple_strtol(argv[2], NULL, 0),
82 &data);
83 if (ret) {
Samuel Dionne-Riel9c7d35a2021-12-20 18:23:45 -050084 printf("Error getting single shot for device %s channel %s (ret=%d)\n",
85 argv[1], argv[2], ret);
Neil Armstrong49261c72018-04-27 15:17:57 +020086 return CMD_RET_FAILURE;
87 }
88
Fabrice Gasnier05550442018-11-12 14:04:00 +010089 ret = uclass_get_device_by_name(UCLASS_ADC, argv[1], &dev);
Marek Vasutbeea7be2022-04-14 18:52:25 +020090 if (!ret && !adc_raw_to_uV(dev, data, &uV)) {
91 val = uV;
Fabrice Gasnier05550442018-11-12 14:04:00 +010092 printf("%u, %d uV\n", data, uV);
Marek Vasutbeea7be2022-04-14 18:52:25 +020093 } else {
94 val = data;
Fabrice Gasnier05550442018-11-12 14:04:00 +010095 printf("%u\n", data);
Marek Vasutbeea7be2022-04-14 18:52:25 +020096 }
97
Vyacheslav Bocharov8985d722022-07-03 15:49:09 +030098 if (varname)
99 env_set_ulong(varname, val);
Neil Armstrong49261c72018-04-27 15:17:57 +0200100
101 return CMD_RET_SUCCESS;
102}
103
Simon Glassed38aef2020-05-10 11:40:03 -0600104static int do_adc_scan(struct cmd_tbl *cmdtp, int flag, int argc,
Fabrice Gasnier5b2449c2018-11-12 14:04:01 +0100105 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
Neil Armstrong49261c72018-04-27 15:17:57 +0200155static char adc_help_text[] =
156 "list - list ADC devices\n"
157 "adc info <name> - Get ADC device info\n"
Marek Vasutbeea7be2022-04-14 18:52:25 +0200158 "adc single <name> <channel> [varname] - Get Single data of ADC device channel\n"
Fabrice Gasnier5b2449c2018-11-12 14:04:01 +0100159 "adc scan <name> [channel mask] - Scan all [or masked] ADC channels";
Neil Armstrong49261c72018-04-27 15:17:57 +0200160
Boris Brezillon878acd72018-12-03 22:54:23 +0100161U_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),
Vyacheslav Bocharov8985d722022-07-03 15:49:09 +0300164 U_BOOT_SUBCMD_MKENT(single, 4, 1, do_adc_single),
Boris Brezillon878acd72018-12-03 22:54:23 +0100165 U_BOOT_SUBCMD_MKENT(scan, 3, 1, do_adc_scan));