blob: a6eb5d38da64bde758b551d6a9d7533197697924 [file] [log] [blame]
Daniel Leungf3361f42019-09-13 10:16:04 -07001/*
2 * Copyright (c) 2019 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
Kumar Gala7a81cd92020-03-24 15:45:46 -05007#define DT_DRV_COMPAT microchip_xec_adc
8
Daniel Leungf3361f42019-09-13 10:16:04 -07009#define LOG_LEVEL CONFIG_ADC_LOG_LEVEL
10#include <logging/log.h>
11LOG_MODULE_REGISTER(adc_mchp_xec);
12
13#include <drivers/adc.h>
14#include <soc.h>
15#include <errno.h>
Daniel Leungf3361f42019-09-13 10:16:04 -070016
17#define ADC_CONTEXT_USES_KERNEL_TIMER
18#include "adc_context.h"
19
20#define XEC_ADC_VREF_ANALOG 3300
21
22/* ADC Control Register */
23#define XEC_ADC_CTRL_SINGLE_DONE_STATUS BIT(7)
24#define XEC_ADC_CTRL_REPEAT_DONE_STATUS BIT(6)
25#define XER_ADC_CTRL_SOFT_RESET BIT(4)
26#define XEC_ADC_CTRL_POWER_SAVER_DIS BIT(3)
27#define XEC_ADC_CTRL_START_REPEAT BIT(2)
28#define XEC_ADC_CTRL_START_SINGLE BIT(1)
29#define XEC_ADC_CTRL_ACTIVATE BIT(0)
30
31struct adc_xec_data {
32 struct adc_context ctx;
Kumar Galaa1b77fd2020-05-27 11:26:57 -050033 uint16_t *buffer;
34 uint16_t *repeat_buffer;
Daniel Leungf3361f42019-09-13 10:16:04 -070035};
36
37struct adc_xec_regs {
Kumar Galaa1b77fd2020-05-27 11:26:57 -050038 uint32_t control_reg;
39 uint32_t delay_reg;
40 uint32_t status_reg;
41 uint32_t single_reg;
42 uint32_t repeat_reg;
43 uint32_t channel_read_reg[8];
44 uint32_t unused[18];
45 uint32_t config_reg;
46 uint32_t vref_channel_reg;
47 uint32_t vref_control_reg;
48 uint32_t sar_control_reg;
Daniel Leungf3361f42019-09-13 10:16:04 -070049};
50
51#define ADC_XEC_REG_BASE \
Kumar Gala7a81cd92020-03-24 15:45:46 -050052 ((struct adc_xec_regs *)(DT_INST_REG_ADDR(0)))
Daniel Leungf3361f42019-09-13 10:16:04 -070053
Daniel Leungf3361f42019-09-13 10:16:04 -070054static void adc_context_start_sampling(struct adc_context *ctx)
55{
56 struct adc_xec_data *data = CONTAINER_OF(ctx, struct adc_xec_data, ctx);
57 struct adc_xec_regs *adc_regs = ADC_XEC_REG_BASE;
58
59 data->repeat_buffer = data->buffer;
60
61 adc_regs->single_reg = ctx->sequence.channels;
62 adc_regs->control_reg |= XEC_ADC_CTRL_START_SINGLE;
63}
64
65static void adc_context_update_buffer_pointer(struct adc_context *ctx,
66 bool repeat_sampling)
67{
68 struct adc_xec_data *data = CONTAINER_OF(ctx, struct adc_xec_data, ctx);
69
70 if (repeat_sampling) {
71 data->buffer = data->repeat_buffer;
72 }
73}
74
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +020075static int adc_xec_channel_setup(const struct device *dev,
Daniel Leungf3361f42019-09-13 10:16:04 -070076 const struct adc_channel_cfg *channel_cfg)
77{
78 struct adc_xec_regs *adc_regs = ADC_XEC_REG_BASE;
Kumar Galaa1b77fd2020-05-27 11:26:57 -050079 uint32_t reg;
Daniel Leungf3361f42019-09-13 10:16:04 -070080
81 ARG_UNUSED(dev);
82
83 if (channel_cfg->acquisition_time != ADC_ACQ_TIME_DEFAULT) {
84 return -EINVAL;
85 }
86
87 if (channel_cfg->channel_id >= MCHP_ADC_MAX_CHAN) {
88 return -EINVAL;
89 }
90
91 if (channel_cfg->gain != ADC_GAIN_1) {
92 return -EINVAL;
93 }
94
95 /* Setup VREF */
96 reg = adc_regs->vref_channel_reg;
97 reg &= ~MCHP_ADC_CH_VREF_SEL_MASK(channel_cfg->channel_id);
98
99 if (channel_cfg->reference == ADC_REF_INTERNAL) {
100 reg |= MCHP_ADC_CH_VREF_SEL_PAD(channel_cfg->channel_id);
101 } else if (channel_cfg->reference == ADC_REF_EXTERNAL0) {
102 reg |= MCHP_ADC_CH_VREF_SEL_GPIO(channel_cfg->channel_id);
103 } else {
104 return -EINVAL;
105 }
106
107 adc_regs->vref_channel_reg = reg;
108
109 /* Differential mode? */
110 reg = adc_regs->sar_control_reg;
111 reg &= ~BIT(MCHP_ADC_SAR_CTRL_SELDIFF_POS);
112 if (channel_cfg->differential != 0) {
113 reg |= MCHP_ADC_SAR_CTRL_SELDIFF_EN;
114 }
115 adc_regs->sar_control_reg = reg;
116
117 return 0;
118}
119
120static bool adc_xec_validate_buffer_size(const struct adc_sequence *sequence)
121{
122 int chan_count = 0;
123 size_t buff_need;
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500124 uint32_t chan_mask;
Daniel Leungf3361f42019-09-13 10:16:04 -0700125
126 for (chan_mask = 0x80; chan_mask != 0; chan_mask >>= 1) {
127 if (chan_mask & sequence->channels) {
128 chan_count++;
129 }
130 }
131
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500132 buff_need = chan_count * sizeof(uint16_t);
Daniel Leungf3361f42019-09-13 10:16:04 -0700133
134 if (sequence->options) {
135 buff_need *= 1 + sequence->options->extra_samplings;
136 }
137
138 if (buff_need > sequence->buffer_size) {
139 return false;
140 }
141
142 return true;
143}
144
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +0200145static int adc_xec_start_read(const struct device *dev,
Daniel Leungf3361f42019-09-13 10:16:04 -0700146 const struct adc_sequence *sequence)
147{
148 struct adc_xec_regs *adc_regs = ADC_XEC_REG_BASE;
Tomasz Bursztyka98d9b012020-05-28 21:23:02 +0200149 struct adc_xec_data *data = dev->data;
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500150 uint32_t reg;
Daniel Leungf3361f42019-09-13 10:16:04 -0700151
Andrei Emeltchenko2f6371c2019-09-30 15:35:39 +0300152 if (sequence->channels & ~BIT_MASK(MCHP_ADC_MAX_CHAN)) {
Andrei Emeltchenko582bb032019-09-30 15:46:13 +0300153 LOG_ERR("Incorrect channels, bitmask 0x%x", sequence->channels);
Daniel Leungf3361f42019-09-13 10:16:04 -0700154 return -EINVAL;
155 }
156
157 if (sequence->channels == 0UL) {
Andrei Emeltchenko582bb032019-09-30 15:46:13 +0300158 LOG_ERR("No channel selected");
Daniel Leungf3361f42019-09-13 10:16:04 -0700159 return -EINVAL;
160 }
161
162 if (!adc_xec_validate_buffer_size(sequence)) {
Andrei Emeltchenko582bb032019-09-30 15:46:13 +0300163 LOG_ERR("Incorrect buffer size");
Daniel Leungf3361f42019-09-13 10:16:04 -0700164 return -ENOMEM;
165 }
166
167 /* Setup ADC resolution */
168 reg = adc_regs->sar_control_reg;
Daniel Leungc3701f52020-03-02 11:06:55 -0800169 reg &= ~(MCHP_ADC_SAR_CTRL_RES_MASK |
170 (1 << MCHP_ADC_SAR_CTRL_SHIFTD_POS));
Daniel Leungf3361f42019-09-13 10:16:04 -0700171
172 if (sequence->resolution == 12) {
173 reg |= MCHP_ADC_SAR_CTRL_RES_12_BITS;
174 } else if (sequence->resolution == 10) {
175 reg |= MCHP_ADC_SAR_CTRL_RES_10_BITS;
Daniel Leungc3701f52020-03-02 11:06:55 -0800176 reg |= MCHP_ADC_SAR_CTRL_SHIFTD_EN;
Daniel Leungf3361f42019-09-13 10:16:04 -0700177 } else {
178 return -EINVAL;
179 }
180
181 adc_regs->sar_control_reg = reg;
182
183 data->buffer = sequence->buffer;
184
185 adc_context_start_read(&data->ctx, sequence);
186
187 return adc_context_wait_for_completion(&data->ctx);
188}
189
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +0200190static int adc_xec_read(const struct device *dev,
Daniel Leungf3361f42019-09-13 10:16:04 -0700191 const struct adc_sequence *sequence)
192{
Tomasz Bursztyka98d9b012020-05-28 21:23:02 +0200193 struct adc_xec_data *data = dev->data;
Daniel Leungf3361f42019-09-13 10:16:04 -0700194 int error;
195
196 adc_context_lock(&data->ctx, false, NULL);
197 error = adc_xec_start_read(dev, sequence);
198 adc_context_release(&data->ctx, error);
199
200 return error;
201}
202
203#if defined(CONFIG_ADC_ASYNC)
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +0200204static int adc_xec_read_async(const struct device *dev,
Daniel Leungf3361f42019-09-13 10:16:04 -0700205 const struct adc_sequence *sequence,
206 struct k_poll_signal *async)
207{
Tomasz Bursztyka98d9b012020-05-28 21:23:02 +0200208 struct adc_xec_data *data = dev->data;
Daniel Leungf3361f42019-09-13 10:16:04 -0700209 int error;
210
211 adc_context_lock(&data->ctx, true, async);
212 error = adc_xec_start_read(dev, sequence);
213 adc_context_release(&data->ctx, error);
214
215 return error;
216}
217#endif /* CONFIG_ADC_ASYNC */
218
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +0200219static void xec_adc_get_sample(const struct device *dev)
Daniel Leungf3361f42019-09-13 10:16:04 -0700220{
221 struct adc_xec_regs *adc_regs = ADC_XEC_REG_BASE;
Tomasz Bursztyka98d9b012020-05-28 21:23:02 +0200222 struct adc_xec_data *data = dev->data;
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500223 uint32_t idx;
224 uint32_t channels = adc_regs->status_reg;
225 uint32_t ch_status = channels;
226 uint32_t bit;
Daniel Leungf3361f42019-09-13 10:16:04 -0700227
228 /*
229 * Using the enabled channel bit set, from
230 * lowest channel number to highest, find out
231 * which channel is enabled and copy the ADC
232 * values from hardware registers to the data
233 * buffer.
234 */
235 bit = find_lsb_set(channels);
236 while (bit != 0) {
237 idx = bit - 1;
238
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500239 *data->buffer = (uint16_t)adc_regs->channel_read_reg[idx];
Daniel Leungf3361f42019-09-13 10:16:04 -0700240 data->buffer++;
241
242 channels &= ~BIT(idx);
243 bit = find_lsb_set(channels);
244 }
245
246 /* Clear the status register */
247 adc_regs->status_reg = ch_status;
248}
249
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +0200250static void adc_xec_isr(const struct device *dev)
Daniel Leungf3361f42019-09-13 10:16:04 -0700251{
252 struct adc_xec_regs *adc_regs = ADC_XEC_REG_BASE;
Tomasz Bursztyka98d9b012020-05-28 21:23:02 +0200253 struct adc_xec_data *data = dev->data;
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500254 uint32_t reg;
Daniel Leungf3361f42019-09-13 10:16:04 -0700255
256 /* Clear START_SINGLE bit and clear SINGLE_DONE_STATUS */
257 reg = adc_regs->control_reg;
258 reg &= ~XEC_ADC_CTRL_START_SINGLE;
259 reg |= XEC_ADC_CTRL_SINGLE_DONE_STATUS;
260 adc_regs->control_reg = reg;
261
262 /* Also clear GIRQ source status bit */
263 MCHP_GIRQ_SRC(MCHP_ADC_GIRQ) = MCHP_ADC_SNG_DONE_GIRQ_VAL;
264
265 xec_adc_get_sample(dev);
266
267 adc_context_on_sampling_done(&data->ctx, dev);
268
269 LOG_DBG("ADC ISR triggered.");
270}
271
272struct adc_driver_api adc_xec_api = {
273 .channel_setup = adc_xec_channel_setup,
274 .read = adc_xec_read,
275#if defined(CONFIG_ADC_ASYNC)
276 .read_async = adc_xec_read_async,
277#endif
278 .ref_internal = XEC_ADC_VREF_ANALOG,
279};
280
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +0200281static int adc_xec_init(const struct device *dev)
Daniel Leungf3361f42019-09-13 10:16:04 -0700282{
283 struct adc_xec_regs *adc_regs = ADC_XEC_REG_BASE;
Tomasz Bursztyka98d9b012020-05-28 21:23:02 +0200284 struct adc_xec_data *data = dev->data;
Daniel Leungf3361f42019-09-13 10:16:04 -0700285
286 adc_regs->control_reg = XEC_ADC_CTRL_ACTIVATE
287 | XEC_ADC_CTRL_POWER_SAVER_DIS
288 | XEC_ADC_CTRL_SINGLE_DONE_STATUS
289 | XEC_ADC_CTRL_REPEAT_DONE_STATUS;
290
291 MCHP_GIRQ_SRC(MCHP_ADC_GIRQ) = MCHP_ADC_SNG_DONE_GIRQ_VAL;
292 MCHP_GIRQ_ENSET(MCHP_ADC_GIRQ) = MCHP_ADC_SNG_DONE_GIRQ_VAL;
293
Kumar Gala7a81cd92020-03-24 15:45:46 -0500294 IRQ_CONNECT(DT_INST_IRQN(0),
295 DT_INST_IRQ(0, priority),
Kumar Gala2f957342020-12-09 12:49:32 -0600296 adc_xec_isr, DEVICE_DT_INST_GET(0), 0);
Kumar Gala7a81cd92020-03-24 15:45:46 -0500297 irq_enable(DT_INST_IRQN(0));
Daniel Leungf3361f42019-09-13 10:16:04 -0700298
299 adc_context_unlock_unconditionally(&data->ctx);
300
301 return 0;
302}
303
304static struct adc_xec_data adc_xec_dev_data_0 = {
305 ADC_CONTEXT_INIT_TIMER(adc_xec_dev_data_0, ctx),
306 ADC_CONTEXT_INIT_LOCK(adc_xec_dev_data_0, ctx),
307 ADC_CONTEXT_INIT_SYNC(adc_xec_dev_data_0, ctx),
308};
309
Gerard Marull-Paretas510aacc2021-04-28 10:02:28 +0200310DEVICE_DT_INST_DEFINE(0, adc_xec_init, NULL,
Kumar Gala2f957342020-12-09 12:49:32 -0600311 &adc_xec_dev_data_0, NULL,
Maureen Helmb0cdef32021-10-19 14:45:49 -0500312 PRE_KERNEL_1, CONFIG_ADC_INIT_PRIORITY,
Daniel Leungf3361f42019-09-13 10:16:04 -0700313 &adc_xec_api);