hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2018 Rockchip Electronics Co. Ltd.
 *
 * Author: Ziyuan Xu <xzy.xu@rock-chips.com>
 */
 
#include <linux/module.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/iio/consumer.h>
#include <linux/iio/iio.h>
 
/**
 * id:            the index of analog switch inputs
 * saradc_chan_id:    the index of analog switch 'x' output
 * gpio_mask:        set the value of switch-gpios with mask, that
 *            makes the 'id' connect to 'saradc_chan_id'.
 */
struct gpio_muxadc_chan_data {
   u32 id;
   u32 saradc_chan_id;
   u32 gpio_mask;
};
 
#define MUXADC_CHANNEL(_index, _id, _mask) {        \
   .id = _index,                    \
   .saradc_chan_id = _id,                \
   .gpio_mask = _mask,                \
}
 
/**
 * nr_chans:        the number of analog switch 'x' output
 * saradc_nr_chans:    the number of analog switch inputs
 * chans:        pointer to get the muxadc channel information
 */
struct gpio_muxadc_data {
   u32 nr_chans;
   u32 saradc_nr_chans;
   const struct gpio_muxadc_chan_data *chans;
};
 
/**
 * gpios:        pointer of digital enable input gpios
 * adc_chans:        pointer of the 'saraadc' channel
 * muxchans:        specification of a single analog switch 'x'
 *            output channel
 * data:        pointer to get the muxadc channels information
 * nr_chans:        the number of analog switch 'x' output
 */
struct gpio_muxadc {
   struct gpio_descs *gpios;
   struct iio_channel *adc_chans;
   struct iio_chan_spec *muxchans;
   const struct gpio_muxadc_data *data;
   u32 nr_chans;
};
 
static int gpio_muxadc_chan_read_by_index(struct gpio_muxadc *muxadc,
                         int index, int *val)
{
   struct iio_channel *saradc_chan;
   const struct gpio_muxadc_chan_data *chan_data;
   u32 i, saradc_chan_id;
 
   chan_data = &muxadc->data->chans[index];
   for (i = 0; i < muxadc->gpios->ndescs; i++) {
       struct gpio_desc *gpiod = muxadc->gpios->desc[i];
       int gpio_val = chan_data->gpio_mask & BIT(i) ? 1 : 0;
 
       gpiod_set_value(gpiod, gpio_val);
   }
 
   saradc_chan_id = chan_data->saradc_chan_id;
   saradc_chan = &muxadc->adc_chans[saradc_chan_id];
   return iio_read_channel_raw(saradc_chan, val);
}
 
static int gpio_muxadc_read_raw(struct iio_dev *indio_dev,
                   struct iio_chan_spec const *chan,
                   int *val, int *val2, long mask)
{
   struct gpio_muxadc *muxadc = iio_priv(indio_dev);
   int ret = IIO_VAL_INT;
 
   switch (mask) {
   case IIO_CHAN_INFO_RAW:
       ret = gpio_muxadc_chan_read_by_index(muxadc,
                            chan->channel, val);
       break;
   default:
       return -EINVAL;
   }
 
   return ret;
}
 
static const struct iio_info gpio_muxadc_iio_info = {
   .read_raw = gpio_muxadc_read_raw,
};
 
static const struct gpio_muxadc_chan_data mux_sgm3699_chans_data[] = {
   MUXADC_CHANNEL(0, 0, 0b00),
   MUXADC_CHANNEL(1, 1, 0b00),
   MUXADC_CHANNEL(2, 2, 0b00),
   MUXADC_CHANNEL(3, 3, 0b00),
   MUXADC_CHANNEL(4, 0, 0b01),
   MUXADC_CHANNEL(5, 1, 0b01),
   MUXADC_CHANNEL(6, 2, 0b11),
   MUXADC_CHANNEL(7, 3, 0b11),
};
 
static const struct gpio_muxadc_data mux_sgm3699_data = {
   .saradc_nr_chans = 4,
   .nr_chans = ARRAY_SIZE(mux_sgm3699_chans_data),
   .chans = mux_sgm3699_chans_data,
};
 
static const struct gpio_muxadc_chan_data mux_sgm48752_chans_data[] = {
   MUXADC_CHANNEL(0, 0, 0b00),
   MUXADC_CHANNEL(1, 1, 0b00),
   MUXADC_CHANNEL(2, 0, 0b10),
   MUXADC_CHANNEL(3, 1, 0b10),
   MUXADC_CHANNEL(4, 0, 0b01),
   MUXADC_CHANNEL(5, 1, 0b01),
   MUXADC_CHANNEL(6, 0, 0b11),
   MUXADC_CHANNEL(7, 1, 0b11),
};
 
static const struct gpio_muxadc_data mux_sgm48752_data = {
   .saradc_nr_chans = 2,
   .nr_chans = ARRAY_SIZE(mux_sgm48752_chans_data),
   .chans = mux_sgm48752_chans_data,
};
 
static const struct of_device_id of_gpio_muxadc_match[] = {
   {
       .compatible = "sgm3699",
       .data = &mux_sgm3699_data,
   },
   {
       .compatible = "sgm48752",
       .data = &mux_sgm48752_data,
   },
   {},
};
MODULE_DEVICE_TABLE(of, of_gpio_muxadc_match);
 
static int gpio_muxadc_probe(struct platform_device *pdev)
{
   struct gpio_muxadc *muxadc;
   struct iio_dev *indio_dev;
   const struct of_device_id *match;
   struct device *dev = &pdev->dev;
   struct device_node *np = pdev->dev.of_node;
   u32 i, nr_adc_chans = 0;
 
   indio_dev = devm_iio_device_alloc(dev, sizeof(*muxadc));
   if (!indio_dev)
       return -ENOMEM;
 
   muxadc = iio_priv(indio_dev);
 
   match = of_match_device(of_gpio_muxadc_match, dev);
   if (!match) {
       dev_err(dev, "failed to match device\n");
       return -ENODEV;
   }
   muxadc->data = match->data;
 
   muxadc->gpios = devm_gpiod_get_array(dev, "switch", GPIOD_OUT_LOW);
   if (IS_ERR(muxadc->gpios)) {
       dev_err(dev, "property of switch-gpios not specified\n");
       return PTR_ERR(muxadc->gpios);
   }
 
   muxadc->adc_chans = iio_channel_get_all(dev);
   if (IS_ERR(muxadc->adc_chans))
       return PTR_ERR(muxadc->adc_chans);
   /*
    * It's necessary to get the number of input ADC, and make a
    * comparison with chan_data->saradc_nr_chans. Otherwise it
    * might fall in to trap.
    */
   while (muxadc->adc_chans[nr_adc_chans].indio_dev)
       nr_adc_chans++;
   if (muxadc->data->saradc_nr_chans != nr_adc_chans) {
       dev_err(dev, "the number of io-channels is mismatch\n");
       return -EINVAL;
   }
 
   muxadc->nr_chans = of_property_count_strings(np, "labels");
   if (muxadc->nr_chans != muxadc->data->nr_chans) {
       dev_err(dev, "should provide %d label\n",
           muxadc->nr_chans);
       return -EINVAL;
   }
 
   muxadc->muxchans = devm_kcalloc(dev, muxadc->nr_chans,
                   sizeof(struct iio_chan_spec),
                   GFP_KERNEL);
   if (!muxadc->muxchans)
       return -ENOMEM;
 
   for (i = 0; i < muxadc->nr_chans; i++) {
       /*
        * The specification of each muxadc channel will be
        * in_voltage_<label> without been indexed.
        */
       muxadc->muxchans[i].type = IIO_VOLTAGE;
       muxadc->muxchans[i].channel = i;
       muxadc->muxchans[i].info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
       of_property_read_string_index(np, "labels", i,
                         &muxadc->muxchans[i].extend_name);
   }
 
   indio_dev->name = dev_name(dev);
   indio_dev->dev.parent = dev;
   indio_dev->dev.of_node = dev->of_node;
   indio_dev->info = &gpio_muxadc_iio_info;
   indio_dev->modes = INDIO_DIRECT_MODE;
   indio_dev->channels = muxadc->muxchans;
   indio_dev->num_channels = muxadc->nr_chans;
 
   return iio_device_register(indio_dev);
}
 
static int gpio_muxadc_remove(struct platform_device *pdev)
{
   struct iio_dev *indio_dev = platform_get_drvdata(pdev);
 
   iio_device_unregister(indio_dev);
   return 0;
}
 
static struct platform_driver gpio_muxadc_driver = {
   .probe        = gpio_muxadc_probe,
   .remove        = gpio_muxadc_remove,
   .driver        = {
       .name    = "gpio-muxadc",
       .of_match_table = of_gpio_muxadc_match,
   },
};
 
module_platform_driver(gpio_muxadc_driver);
 
MODULE_AUTHOR("Ziyuan Xu <xzy.xu@rock-chips.com>");
MODULE_DESCRIPTION("GPIO MUX ADC driver");
MODULE_LICENSE("GPL v2");