Lines Matching +full:adc +full:- +full:channels

1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI ADC MFD driver
5 * Copyright (C) 2012 Texas Instruments Incorporated - https://www.ti.com/
26 #include <linux/dma-mapping.h>
45 int channels; member
54 static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg) in tiadc_readl() argument
56 return readl(adc->mfd_tscadc->tscadc_base + reg); in tiadc_readl()
59 static void tiadc_writel(struct tiadc_device *adc, unsigned int reg, in tiadc_writel() argument
62 writel(val, adc->mfd_tscadc->tscadc_base + reg); in tiadc_writel()
69 step_en = ((1 << adc_dev->channels) - 1); in get_adc_step_mask()
70 step_en <<= TOTAL_STEPS - adc_dev->channels + 1; in get_adc_step_mask()
79 for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) { in get_adc_chan_step_mask()
80 if (chan->channel == adc_dev->channel_line[i]) { in get_adc_chan_step_mask()
83 step = adc_dev->channel_step[i]; in get_adc_chan_step_mask()
94 return 1 << adc_dev->channel_step[chan]; in get_adc_step_bit()
101 return readl_poll_timeout(adc_dev->mfd_tscadc->tscadc_base + REG_ADCFSM, in tiadc_wait_idle()
103 IDLE_TIMEOUT_MS * 1000 * adc_dev->channels); in tiadc_wait_idle()
114 * lines available which are shared between Touchscreen and ADC. in tiadc_step_config()
116 * Steps forwards i.e. from 0 towards 16 are used by ADC in tiadc_step_config()
119 * needs to be given to ADC to digitalize data. in tiadc_step_config()
121 for (i = 0; i < adc_dev->channels; i++) { in tiadc_step_config()
124 chan = adc_dev->channel_line[i]; in tiadc_step_config()
126 if (adc_dev->step_avg[i]) in tiadc_step_config()
127 stepconfig = STEPCONFIG_AVG(ffs(adc_dev->step_avg[i]) - 1) | in tiadc_step_config()
141 STEPDELAY_OPEN(adc_dev->open_delay[i]) | in tiadc_step_config()
142 STEPDELAY_SAMPLE(adc_dev->sample_delay[i])); in tiadc_step_config()
144 adc_dev->channel_step[i] = steps; in tiadc_step_config()
159 * ADC and touchscreen share the IRQ line. in tiadc_irq_h()
163 /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */ in tiadc_irq_h()
173 * ADC needs to finish the current conversion in tiadc_irq_h()
196 u16 *data = adc_dev->data; in tiadc_worker_h()
200 for (i = 0; i < indio_dev->scan_bytes / 2; i++) { in tiadc_worker_h()
217 struct tiadc_dma *dma = &adc_dev->dma; in tiadc_dma_rx_complete()
221 data = dma->buf + dma->current_period * dma->period_size; in tiadc_dma_rx_complete()
222 dma->current_period = 1 - dma->current_period; /* swap the buffer ID */ in tiadc_dma_rx_complete()
224 for (i = 0; i < dma->period_size; i += indio_dev->scan_bytes) { in tiadc_dma_rx_complete()
226 data += indio_dev->scan_bytes; in tiadc_dma_rx_complete()
233 struct tiadc_dma *dma = &adc_dev->dma; in tiadc_start_dma()
236 dma->current_period = 0; /* We start to fill period 0 */ in tiadc_start_dma()
240 * channels enabled, so make sure that cyclic DMA period in tiadc_start_dma()
241 * length is also a multiple of total number of channels in tiadc_start_dma()
245 dma->fifo_thresh = rounddown(FIFO1_THRESHOLD + 1, in tiadc_start_dma()
246 adc_dev->total_ch_enabled) - 1; in tiadc_start_dma()
249 dma->period_size = rounddown(DMA_BUFFER_SIZE / 2, in tiadc_start_dma()
250 (dma->fifo_thresh + 1) * sizeof(u16)); in tiadc_start_dma()
252 dma->conf.src_maxburst = dma->fifo_thresh + 1; in tiadc_start_dma()
253 dmaengine_slave_config(dma->chan, &dma->conf); in tiadc_start_dma()
255 desc = dmaengine_prep_dma_cyclic(dma->chan, dma->addr, in tiadc_start_dma()
256 dma->period_size * 2, in tiadc_start_dma()
257 dma->period_size, DMA_DEV_TO_MEM, in tiadc_start_dma()
260 return -EBUSY; in tiadc_start_dma()
262 desc->callback = tiadc_dma_rx_complete; in tiadc_start_dma()
263 desc->callback_param = indio_dev; in tiadc_start_dma()
265 dma->cookie = dmaengine_submit(desc); in tiadc_start_dma()
267 dma_async_issue_pending(dma->chan); in tiadc_start_dma()
269 tiadc_writel(adc_dev, REG_FIFO1THR, dma->fifo_thresh); in tiadc_start_dma()
270 tiadc_writel(adc_dev, REG_DMA1REQ, dma->fifo_thresh); in tiadc_start_dma()
290 /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */ in tiadc_buffer_preenable()
301 struct tiadc_dma *dma = &adc_dev->dma; in tiadc_buffer_postenable()
307 for_each_set_bit(bit, indio_dev->active_scan_mask, adc_dev->channels) { in tiadc_buffer_postenable()
309 adc_dev->total_ch_enabled++; in tiadc_buffer_postenable()
311 adc_dev->buffer_en_ch_steps = enb; in tiadc_buffer_postenable()
313 if (dma->chan) in tiadc_buffer_postenable()
316 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb); in tiadc_buffer_postenable()
323 if (!dma->chan) in tiadc_buffer_postenable()
333 struct tiadc_dma *dma = &adc_dev->dma; in tiadc_buffer_predisable()
339 am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps); in tiadc_buffer_predisable()
340 adc_dev->buffer_en_ch_steps = 0; in tiadc_buffer_predisable()
341 adc_dev->total_ch_enabled = 0; in tiadc_buffer_predisable()
342 if (dma->chan) { in tiadc_buffer_predisable()
344 dmaengine_terminate_async(dma->chan); in tiadc_buffer_predisable()
347 /* Flush FIFO of leftover data in the time it takes to disable adc */ in tiadc_buffer_predisable()
383 flags, indio_dev->name, indio_dev); in tiadc_iio_buffered_hardware_setup()
398 int channels) in tiadc_channel_init() argument
405 indio_dev->num_channels = channels; in tiadc_channel_init()
406 chan_array = devm_kcalloc(dev, channels, sizeof(*chan_array), in tiadc_channel_init()
409 return -ENOMEM; in tiadc_channel_init()
412 for (i = 0; i < channels; i++, chan++) { in tiadc_channel_init()
413 chan->type = IIO_VOLTAGE; in tiadc_channel_init()
414 chan->indexed = 1; in tiadc_channel_init()
415 chan->channel = adc_dev->channel_line[i]; in tiadc_channel_init()
416 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); in tiadc_channel_init()
417 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE); in tiadc_channel_init()
418 chan->datasheet_name = chan_name_ain[chan->channel]; in tiadc_channel_init()
419 chan->scan_index = i; in tiadc_channel_init()
420 chan->scan_type.sign = 'u'; in tiadc_channel_init()
421 chan->scan_type.realbits = 12; in tiadc_channel_init()
422 chan->scan_type.storagebits = 16; in tiadc_channel_init()
425 indio_dev->channels = chan_array; in tiadc_channel_init()
446 switch (chan->type) { in tiadc_read_raw()
449 *val2 = chan->scan_type.realbits; in tiadc_read_raw()
452 return -EINVAL; in tiadc_read_raw()
456 return -EINVAL; in tiadc_read_raw()
460 return -EBUSY; in tiadc_read_raw()
464 return -EINVAL; in tiadc_read_raw()
466 mutex_lock(&adc_dev->fifo1_lock); in tiadc_read_raw()
473 while (fifo1count--) in tiadc_read_raw()
476 am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en); in tiadc_read_raw()
479 timeout = jiffies + msecs_to_jiffies(IDLE_TIMEOUT_MS * adc_dev->channels); in tiadc_read_raw()
486 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc); in tiadc_read_raw()
487 ret = -EAGAIN; in tiadc_read_raw()
492 map_val = adc_dev->channel_step[chan->scan_index]; in tiadc_read_raw()
496 * something went wrong we left empty handed (-EAGAIN previously) and in tiadc_read_raw()
513 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc); in tiadc_read_raw()
516 ret = -EBUSY; in tiadc_read_raw()
519 mutex_unlock(&adc_dev->fifo1_lock); in tiadc_read_raw()
530 struct tiadc_dma *dma = &adc_dev->dma; in tiadc_request_dma()
534 dma->conf.direction = DMA_DEV_TO_MEM; in tiadc_request_dma()
535 dma->conf.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; in tiadc_request_dma()
536 dma->conf.src_addr = adc_dev->mfd_tscadc->tscadc_phys_base + REG_FIFO1; in tiadc_request_dma()
542 dma->chan = dma_request_chan(adc_dev->mfd_tscadc->dev, "fifo1"); in tiadc_request_dma()
543 if (IS_ERR(dma->chan)) { in tiadc_request_dma()
544 int ret = PTR_ERR(dma->chan); in tiadc_request_dma()
546 dma->chan = NULL; in tiadc_request_dma()
551 dma->buf = dma_alloc_coherent(dma->chan->device->dev, DMA_BUFFER_SIZE, in tiadc_request_dma()
552 &dma->addr, GFP_KERNEL); in tiadc_request_dma()
553 if (!dma->buf) in tiadc_request_dma()
559 dma_release_channel(dma->chan); in tiadc_request_dma()
560 return -ENOMEM; in tiadc_request_dma()
566 struct device_node *node = pdev->dev.of_node; in tiadc_parse_dt()
567 int channels = 0; in tiadc_parse_dt() local
571 of_property_for_each_u32(node, "ti,adc-channels", val) { in tiadc_parse_dt()
572 adc_dev->channel_line[channels] = val; in tiadc_parse_dt()
575 adc_dev->open_delay[channels] = STEPCONFIG_OPENDLY; in tiadc_parse_dt()
576 adc_dev->sample_delay[channels] = STEPCONFIG_SAMPLEDLY; in tiadc_parse_dt()
577 adc_dev->step_avg[channels] = 16; in tiadc_parse_dt()
579 channels++; in tiadc_parse_dt()
582 adc_dev->channels = channels; in tiadc_parse_dt()
584 of_property_read_u32_array(node, "ti,chan-step-avg", in tiadc_parse_dt()
585 adc_dev->step_avg, channels); in tiadc_parse_dt()
586 of_property_read_u32_array(node, "ti,chan-step-opendelay", in tiadc_parse_dt()
587 adc_dev->open_delay, channels); in tiadc_parse_dt()
588 of_property_read_u32_array(node, "ti,chan-step-sampledelay", in tiadc_parse_dt()
589 adc_dev->sample_delay, channels); in tiadc_parse_dt()
591 for (i = 0; i < adc_dev->channels; i++) { in tiadc_parse_dt()
594 chan = adc_dev->channel_line[i]; in tiadc_parse_dt()
596 if (adc_dev->step_avg[i] > STEPCONFIG_AVG_16) { in tiadc_parse_dt()
597 dev_warn(&pdev->dev, in tiadc_parse_dt()
600 adc_dev->step_avg[i] = STEPCONFIG_AVG_16; in tiadc_parse_dt()
603 if (adc_dev->open_delay[i] > STEPCONFIG_MAX_OPENDLY) { in tiadc_parse_dt()
604 dev_warn(&pdev->dev, in tiadc_parse_dt()
607 adc_dev->open_delay[i] = STEPCONFIG_MAX_OPENDLY; in tiadc_parse_dt()
610 if (adc_dev->sample_delay[i] > STEPCONFIG_MAX_SAMPLE) { in tiadc_parse_dt()
611 dev_warn(&pdev->dev, in tiadc_parse_dt()
614 adc_dev->sample_delay[i] = STEPCONFIG_MAX_SAMPLE; in tiadc_parse_dt()
625 struct device_node *node = pdev->dev.of_node; in tiadc_probe()
629 dev_err(&pdev->dev, "Could not find valid DT data.\n"); in tiadc_probe()
630 return -EINVAL; in tiadc_probe()
633 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc_dev)); in tiadc_probe()
635 dev_err(&pdev->dev, "failed to allocate iio device\n"); in tiadc_probe()
636 return -ENOMEM; in tiadc_probe()
640 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev); in tiadc_probe()
643 indio_dev->name = dev_name(&pdev->dev); in tiadc_probe()
644 indio_dev->modes = INDIO_DIRECT_MODE; in tiadc_probe()
645 indio_dev->info = &tiadc_info; in tiadc_probe()
649 mutex_init(&adc_dev->fifo1_lock); in tiadc_probe()
651 err = tiadc_channel_init(&pdev->dev, indio_dev, adc_dev->channels); in tiadc_probe()
655 err = tiadc_iio_buffered_hardware_setup(&pdev->dev, indio_dev, in tiadc_probe()
658 adc_dev->mfd_tscadc->irq, in tiadc_probe()
671 if (err && err != -ENODEV) { in tiadc_probe()
672 dev_err_probe(&pdev->dev, err, "DMA request failed\n"); in tiadc_probe()
688 struct tiadc_dma *dma = &adc_dev->dma; in tiadc_remove()
691 if (dma->chan) { in tiadc_remove()
692 dma_free_coherent(dma->chan->device->dev, DMA_BUFFER_SIZE, in tiadc_remove()
693 dma->buf, dma->addr); in tiadc_remove()
694 dma_release_channel(dma->chan); in tiadc_remove()
699 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en); in tiadc_remove()
721 /* Make sure ADC is powered up */ in tiadc_resume()
727 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, in tiadc_resume()
728 adc_dev->buffer_en_ch_steps); in tiadc_resume()
735 { .compatible = "ti,am3359-adc", },
736 { .compatible = "ti,am4372-adc", },
743 .name = "TI-am335x-adc",
752 MODULE_DESCRIPTION("TI ADC controller driver");