Lines Matching +full:am3359 +full:- +full:adc

1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI ADC MFD driver
5 * Copyright (C) 2012 Texas Instruments Incorporated - https://www.ti.com/
27 #include <linux/dma-mapping.h>
55 static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg) in tiadc_readl() argument
57 return readl(adc->mfd_tscadc->tscadc_base + reg); in tiadc_readl()
60 static void tiadc_writel(struct tiadc_device *adc, unsigned int reg, in tiadc_writel() argument
63 writel(val, adc->mfd_tscadc->tscadc_base + reg); in tiadc_writel()
70 step_en = ((1 << adc_dev->channels) - 1); in get_adc_step_mask()
71 step_en <<= TOTAL_STEPS - adc_dev->channels + 1; in get_adc_step_mask()
80 for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) { in get_adc_chan_step_mask()
81 if (chan->channel == adc_dev->channel_line[i]) { in get_adc_chan_step_mask()
84 step = adc_dev->channel_step[i]; in get_adc_chan_step_mask()
95 return 1 << adc_dev->channel_step[chan]; in get_adc_step_bit()
102 return readl_poll_timeout(adc_dev->mfd_tscadc->tscadc_base + REG_ADCFSM, in tiadc_wait_idle()
104 IDLE_TIMEOUT_MS * 1000 * adc_dev->channels); in tiadc_wait_idle()
115 * lines available which are shared between Touchscreen and ADC. in tiadc_step_config()
117 * Steps forwards i.e. from 0 towards 16 are used by ADC in tiadc_step_config()
120 * needs to be given to ADC to digitalize data. in tiadc_step_config()
122 for (i = 0; i < adc_dev->channels; i++) { in tiadc_step_config()
125 chan = adc_dev->channel_line[i]; in tiadc_step_config()
127 if (adc_dev->step_avg[i]) in tiadc_step_config()
128 stepconfig = STEPCONFIG_AVG(ffs(adc_dev->step_avg[i]) - 1) | in tiadc_step_config()
142 STEPDELAY_OPEN(adc_dev->open_delay[i]) | in tiadc_step_config()
143 STEPDELAY_SAMPLE(adc_dev->sample_delay[i])); in tiadc_step_config()
145 adc_dev->channel_step[i] = steps; in tiadc_step_config()
160 * ADC and touchscreen share the IRQ line. in tiadc_irq_h()
164 /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */ in tiadc_irq_h()
174 * ADC needs to finish the current conversion in tiadc_irq_h()
197 u16 *data = adc_dev->data; in tiadc_worker_h()
201 for (i = 0; i < indio_dev->scan_bytes / 2; i++) { in tiadc_worker_h()
218 struct tiadc_dma *dma = &adc_dev->dma; in tiadc_dma_rx_complete()
222 data = dma->buf + dma->current_period * dma->period_size; in tiadc_dma_rx_complete()
223 dma->current_period = 1 - dma->current_period; /* swap the buffer ID */ in tiadc_dma_rx_complete()
225 for (i = 0; i < dma->period_size; i += indio_dev->scan_bytes) { in tiadc_dma_rx_complete()
227 data += indio_dev->scan_bytes; in tiadc_dma_rx_complete()
234 struct tiadc_dma *dma = &adc_dev->dma; in tiadc_start_dma()
237 dma->current_period = 0; /* We start to fill period 0 */ in tiadc_start_dma()
246 dma->fifo_thresh = rounddown(FIFO1_THRESHOLD + 1, in tiadc_start_dma()
247 adc_dev->total_ch_enabled) - 1; in tiadc_start_dma()
250 dma->period_size = rounddown(DMA_BUFFER_SIZE / 2, in tiadc_start_dma()
251 (dma->fifo_thresh + 1) * sizeof(u16)); in tiadc_start_dma()
253 dma->conf.src_maxburst = dma->fifo_thresh + 1; in tiadc_start_dma()
254 dmaengine_slave_config(dma->chan, &dma->conf); in tiadc_start_dma()
256 desc = dmaengine_prep_dma_cyclic(dma->chan, dma->addr, in tiadc_start_dma()
257 dma->period_size * 2, in tiadc_start_dma()
258 dma->period_size, DMA_DEV_TO_MEM, in tiadc_start_dma()
261 return -EBUSY; in tiadc_start_dma()
263 desc->callback = tiadc_dma_rx_complete; in tiadc_start_dma()
264 desc->callback_param = indio_dev; in tiadc_start_dma()
266 dma->cookie = dmaengine_submit(desc); in tiadc_start_dma()
268 dma_async_issue_pending(dma->chan); in tiadc_start_dma()
270 tiadc_writel(adc_dev, REG_FIFO1THR, dma->fifo_thresh); in tiadc_start_dma()
271 tiadc_writel(adc_dev, REG_DMA1REQ, dma->fifo_thresh); in tiadc_start_dma()
291 /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */ in tiadc_buffer_preenable()
302 struct tiadc_dma *dma = &adc_dev->dma; in tiadc_buffer_postenable()
308 for_each_set_bit(bit, indio_dev->active_scan_mask, adc_dev->channels) { in tiadc_buffer_postenable()
310 adc_dev->total_ch_enabled++; in tiadc_buffer_postenable()
312 adc_dev->buffer_en_ch_steps = enb; in tiadc_buffer_postenable()
314 if (dma->chan) in tiadc_buffer_postenable()
317 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb); in tiadc_buffer_postenable()
324 if (!dma->chan) in tiadc_buffer_postenable()
334 struct tiadc_dma *dma = &adc_dev->dma; in tiadc_buffer_predisable()
340 am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps); in tiadc_buffer_predisable()
341 adc_dev->buffer_en_ch_steps = 0; in tiadc_buffer_predisable()
342 adc_dev->total_ch_enabled = 0; in tiadc_buffer_predisable()
343 if (dma->chan) { in tiadc_buffer_predisable()
345 dmaengine_terminate_async(dma->chan); in tiadc_buffer_predisable()
348 /* Flush FIFO of leftover data in the time it takes to disable adc */ in tiadc_buffer_predisable()
384 flags, indio_dev->name, indio_dev); in tiadc_iio_buffered_hardware_setup()
406 indio_dev->num_channels = channels; in tiadc_channel_init()
410 return -ENOMEM; in tiadc_channel_init()
414 chan->type = IIO_VOLTAGE; in tiadc_channel_init()
415 chan->indexed = 1; in tiadc_channel_init()
416 chan->channel = adc_dev->channel_line[i]; in tiadc_channel_init()
417 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); in tiadc_channel_init()
418 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE); in tiadc_channel_init()
419 chan->datasheet_name = chan_name_ain[chan->channel]; in tiadc_channel_init()
420 chan->scan_index = i; in tiadc_channel_init()
421 chan->scan_type.sign = 'u'; in tiadc_channel_init()
422 chan->scan_type.realbits = 12; in tiadc_channel_init()
423 chan->scan_type.storagebits = 16; in tiadc_channel_init()
426 indio_dev->channels = chan_array; in tiadc_channel_init()
447 switch (chan->type) { in tiadc_read_raw()
450 *val2 = chan->scan_type.realbits; in tiadc_read_raw()
453 return -EINVAL; in tiadc_read_raw()
457 return -EINVAL; in tiadc_read_raw()
461 return -EBUSY; in tiadc_read_raw()
465 return -EINVAL; in tiadc_read_raw()
467 mutex_lock(&adc_dev->fifo1_lock); in tiadc_read_raw()
474 while (fifo1count--) in tiadc_read_raw()
477 am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en); in tiadc_read_raw()
480 timeout = jiffies + msecs_to_jiffies(IDLE_TIMEOUT_MS * adc_dev->channels); in tiadc_read_raw()
487 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc); in tiadc_read_raw()
488 ret = -EAGAIN; in tiadc_read_raw()
493 map_val = adc_dev->channel_step[chan->scan_index]; in tiadc_read_raw()
497 * something went wrong we left empty handed (-EAGAIN previously) and in tiadc_read_raw()
514 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc); in tiadc_read_raw()
517 ret = -EBUSY; in tiadc_read_raw()
520 mutex_unlock(&adc_dev->fifo1_lock); in tiadc_read_raw()
531 struct tiadc_dma *dma = &adc_dev->dma; in tiadc_request_dma()
535 dma->conf.direction = DMA_DEV_TO_MEM; in tiadc_request_dma()
536 dma->conf.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; in tiadc_request_dma()
537 dma->conf.src_addr = adc_dev->mfd_tscadc->tscadc_phys_base + REG_FIFO1; in tiadc_request_dma()
543 dma->chan = dma_request_chan(adc_dev->mfd_tscadc->dev, "fifo1"); in tiadc_request_dma()
544 if (IS_ERR(dma->chan)) { in tiadc_request_dma()
545 int ret = PTR_ERR(dma->chan); in tiadc_request_dma()
547 dma->chan = NULL; in tiadc_request_dma()
552 dma->buf = dma_alloc_coherent(dma->chan->device->dev, DMA_BUFFER_SIZE, in tiadc_request_dma()
553 &dma->addr, GFP_KERNEL); in tiadc_request_dma()
554 if (!dma->buf) in tiadc_request_dma()
560 dma_release_channel(dma->chan); in tiadc_request_dma()
561 return -ENOMEM; in tiadc_request_dma()
567 struct device_node *node = pdev->dev.of_node; in tiadc_parse_dt()
574 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) { in tiadc_parse_dt()
575 adc_dev->channel_line[channels] = val; in tiadc_parse_dt()
578 adc_dev->open_delay[channels] = STEPCONFIG_OPENDLY; in tiadc_parse_dt()
579 adc_dev->sample_delay[channels] = STEPCONFIG_SAMPLEDLY; in tiadc_parse_dt()
580 adc_dev->step_avg[channels] = 16; in tiadc_parse_dt()
585 adc_dev->channels = channels; in tiadc_parse_dt()
587 of_property_read_u32_array(node, "ti,chan-step-avg", in tiadc_parse_dt()
588 adc_dev->step_avg, channels); in tiadc_parse_dt()
589 of_property_read_u32_array(node, "ti,chan-step-opendelay", in tiadc_parse_dt()
590 adc_dev->open_delay, channels); in tiadc_parse_dt()
591 of_property_read_u32_array(node, "ti,chan-step-sampledelay", in tiadc_parse_dt()
592 adc_dev->sample_delay, channels); in tiadc_parse_dt()
594 for (i = 0; i < adc_dev->channels; i++) { in tiadc_parse_dt()
597 chan = adc_dev->channel_line[i]; in tiadc_parse_dt()
599 if (adc_dev->step_avg[i] > STEPCONFIG_AVG_16) { in tiadc_parse_dt()
600 dev_warn(&pdev->dev, in tiadc_parse_dt()
603 adc_dev->step_avg[i] = STEPCONFIG_AVG_16; in tiadc_parse_dt()
606 if (adc_dev->open_delay[i] > STEPCONFIG_MAX_OPENDLY) { in tiadc_parse_dt()
607 dev_warn(&pdev->dev, in tiadc_parse_dt()
610 adc_dev->open_delay[i] = STEPCONFIG_MAX_OPENDLY; in tiadc_parse_dt()
613 if (adc_dev->sample_delay[i] > STEPCONFIG_MAX_SAMPLE) { in tiadc_parse_dt()
614 dev_warn(&pdev->dev, in tiadc_parse_dt()
617 adc_dev->sample_delay[i] = STEPCONFIG_MAX_SAMPLE; in tiadc_parse_dt()
628 struct device_node *node = pdev->dev.of_node; in tiadc_probe()
632 dev_err(&pdev->dev, "Could not find valid DT data.\n"); in tiadc_probe()
633 return -EINVAL; in tiadc_probe()
636 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc_dev)); in tiadc_probe()
638 dev_err(&pdev->dev, "failed to allocate iio device\n"); in tiadc_probe()
639 return -ENOMEM; in tiadc_probe()
643 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev); in tiadc_probe()
646 indio_dev->name = dev_name(&pdev->dev); in tiadc_probe()
647 indio_dev->modes = INDIO_DIRECT_MODE; in tiadc_probe()
648 indio_dev->info = &tiadc_info; in tiadc_probe()
652 mutex_init(&adc_dev->fifo1_lock); in tiadc_probe()
654 err = tiadc_channel_init(&pdev->dev, indio_dev, adc_dev->channels); in tiadc_probe()
658 err = tiadc_iio_buffered_hardware_setup(&pdev->dev, indio_dev, in tiadc_probe()
661 adc_dev->mfd_tscadc->irq, in tiadc_probe()
674 if (err && err == -EPROBE_DEFER) in tiadc_probe()
689 struct tiadc_dma *dma = &adc_dev->dma; in tiadc_remove()
692 if (dma->chan) { in tiadc_remove()
693 dma_free_coherent(dma->chan->device->dev, DMA_BUFFER_SIZE, in tiadc_remove()
694 dma->buf, dma->addr); in tiadc_remove()
695 dma_release_channel(dma->chan); in tiadc_remove()
700 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en); in tiadc_remove()
724 /* Make sure ADC is powered up */ in tiadc_resume()
730 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, in tiadc_resume()
731 adc_dev->buffer_en_ch_steps); in tiadc_resume()
738 { .compatible = "ti,am3359-adc", },
739 { .compatible = "ti,am4372-adc", },
746 .name = "TI-am335x-adc",
755 MODULE_DESCRIPTION("TI ADC controller driver");