1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Samsung Electronics
4 * Przemyslaw Marczak <p.marczak@samsung.com>
10 #include <sandbox-adc.h>
13 * struct sandbox_adc_priv - sandbox ADC device's operation status and data
15 * @conversion_status - conversion status: ACTIVE (started) / INACTIVE (stopped)
16 * @conversion_mode - conversion mode: single or multi-channel
17 * @active_channel - active channel number, valid for single channel mode
18 * data[] - channels data
20 struct sandbox_adc_priv {
21 int conversion_status;
23 int active_channel_mask;
27 int sandbox_adc_start_channel(struct udevice *dev, int channel)
29 struct sandbox_adc_priv *priv = dev_get_priv(dev);
31 /* Set single-channel mode */
32 priv->conversion_mode = SANDBOX_ADC_MODE_SINGLE_CHANNEL;
34 priv->active_channel_mask = 1 << channel;
35 /* Start conversion */
36 priv->conversion_status = SANDBOX_ADC_ACTIVE;
41 int sandbox_adc_start_channels(struct udevice *dev, unsigned int channel_mask)
43 struct sandbox_adc_priv *priv = dev_get_priv(dev);
45 /* Set single-channel mode */
46 priv->conversion_mode = SANDBOX_ADC_MODE_MULTI_CHANNEL;
48 priv->active_channel_mask = channel_mask;
49 /* Start conversion */
50 priv->conversion_status = SANDBOX_ADC_ACTIVE;
55 int sandbox_adc_channel_data(struct udevice *dev, int channel,
58 struct sandbox_adc_priv *priv = dev_get_priv(dev);
60 /* For single-channel conversion mode, check if channel was selected */
61 if ((priv->conversion_mode == SANDBOX_ADC_MODE_SINGLE_CHANNEL) &&
62 !(priv->active_channel_mask & (1 << channel))) {
63 pr_err("Request for an inactive channel!");
67 /* The conversion must be started before reading the data */
68 if (priv->conversion_status == SANDBOX_ADC_INACTIVE)
71 *data = priv->data[channel];
76 int sandbox_adc_channels_data(struct udevice *dev, unsigned int channel_mask,
77 struct adc_channel *channels)
79 struct sandbox_adc_priv *priv = dev_get_priv(dev);
82 /* Return error for single-channel conversion mode */
83 if (priv->conversion_mode == SANDBOX_ADC_MODE_SINGLE_CHANNEL) {
84 pr_err("ADC in single-channel mode!");
87 /* Check channel selection */
88 if (!(priv->active_channel_mask & channel_mask)) {
89 pr_err("Request for an inactive channel!");
92 /* The conversion must be started before reading the data */
93 if (priv->conversion_status == SANDBOX_ADC_INACTIVE)
96 for (i = 0; i < SANDBOX_ADC_CHANNELS; i++) {
97 if (!((channel_mask >> i) & 0x1))
100 channels->data = priv->data[i];
108 int sandbox_adc_stop(struct udevice *dev)
110 struct sandbox_adc_priv *priv = dev_get_priv(dev);
112 /* Start conversion */
113 priv->conversion_status = SANDBOX_ADC_INACTIVE;
118 int sandbox_adc_probe(struct udevice *dev)
120 struct sandbox_adc_priv *priv = dev_get_priv(dev);
122 /* Stop conversion */
123 priv->conversion_status = SANDBOX_ADC_INACTIVE;
124 /* Set single-channel mode */
125 priv->conversion_mode = SANDBOX_ADC_MODE_SINGLE_CHANNEL;
126 /* Deselect all channels */
127 priv->active_channel_mask = 0;
129 /* Set sandbox test data */
130 priv->data[0] = SANDBOX_ADC_CHANNEL0_DATA;
131 priv->data[1] = SANDBOX_ADC_CHANNEL1_DATA;
132 priv->data[2] = SANDBOX_ADC_CHANNEL2_DATA;
133 priv->data[3] = SANDBOX_ADC_CHANNEL3_DATA;
138 int sandbox_adc_ofdata_to_platdata(struct udevice *dev)
140 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
142 uc_pdata->data_mask = SANDBOX_ADC_DATA_MASK;
143 uc_pdata->data_format = ADC_DATA_FORMAT_BIN;
144 uc_pdata->data_timeout_us = 0;
146 /* Mask available channel bits: [0:3] */
147 uc_pdata->channel_mask = (1 << SANDBOX_ADC_CHANNELS) - 1;
152 static const struct adc_ops sandbox_adc_ops = {
153 .start_channel = sandbox_adc_start_channel,
154 .start_channels = sandbox_adc_start_channels,
155 .channel_data = sandbox_adc_channel_data,
156 .channels_data = sandbox_adc_channels_data,
157 .stop = sandbox_adc_stop,
160 static const struct udevice_id sandbox_adc_ids[] = {
161 { .compatible = "sandbox,adc" },
165 U_BOOT_DRIVER(sandbox_adc) = {
166 .name = "sandbox-adc",
168 .of_match = sandbox_adc_ids,
169 .ops = &sandbox_adc_ops,
170 .probe = sandbox_adc_probe,
171 .ofdata_to_platdata = sandbox_adc_ofdata_to_platdata,
172 .priv_auto_alloc_size = sizeof(struct sandbox_adc_priv),