Lines Matching +full:vref +full:- +full:half
1 // SPDX-License-Identifier: GPL-2.0-only
14 #include <linux/dma-mapping.h>
31 #include <linux/nvmem-consumer.h>
36 #include <dt-bindings/iio/adc/at91-sama5d2_adc.h>
110 /* Interrupt Enable Register - TS X measurement ready */
112 /* Interrupt Enable Register - TS Y measurement ready */
114 /* Interrupt Enable Register - TS pressure measurement ready */
116 /* Interrupt Enable Register - Data ready */
118 /* Interrupt Enable Register - general overrun error */
120 /* Interrupt Enable Register - Pen detect */
122 /* Interrupt Enable Register - No pen detect */
139 /* Interrupt Status Register - Pen touching sense status */
149 /* Extended Mode Register - Oversampling rate */
157 /* Extended Mode Register - TRACKX */
164 /* Extended Mode Register - Averaging on single trigger event */
173 /* Channel Offset Register differential offset - constant, not a register */
177 /* Analog Control Register - Pen detect sensitivity mask */
179 /* Analog Control Register - Source last channel */
184 /* Touchscreen Mode Register - No touch mode */
186 /* Touchscreen Mode Register - 4 wire screen, no pressure measurement */
188 /* Touchscreen Mode Register - 4 wire screen, pressure measurement */
190 /* Touchscreen Mode Register - 5 wire screen */
192 /* Touchscreen Mode Register - Average samples mask */
194 /* Touchscreen Mode Register - Average samples */
196 /* Touchscreen Mode Register - Touch/trigger frequency ratio mask */
198 /* Touchscreen Mode Register - Touch/trigger frequency ratio */
200 /* Touchscreen Mode Register - Pen Debounce Time mask */
202 /* Touchscreen Mode Register - Pen Debounce Time */
204 /* Touchscreen Mode Register - No DMA for touch measurements */
206 /* Touchscreen Mode Register - Disable pen detection */
208 /* Touchscreen Mode Register - Enable pen detection */
231 /* Trigger Mode - trigger period mask */
233 /* Trigger Mode - trigger period */
250 /* Temperature Sensor Mode - Temperature sensor on */
378 .datasheet_name = "CH"#num"-CH"#num2, \
441 readl_relaxed((st)->base + (st)->soc_info.platform->layout->reg)
443 readl_relaxed((st)->base + reg)
445 writel_relaxed(val, (st)->base + (st)->soc_info.platform->layout->reg)
448 * struct at91_adc_platform - at91-sama5d2 platform information struct
486 * struct at91_adc_temp_sensor_clb - at91-sama5d2 temperature sensor
499 * enum at91_adc_ts_clb_idx - calibration indexes in NVMEM buffer
512 /* Temperature sensor calibration - Vtemp voltage sensitivity to temperature. */
516 * struct at91_adc_soc_info - at91-sama5d2 soc information struct
539 * struct at91_adc_dma - at91-sama5d2 dma information struct
561 * struct at91_adc_touch - at91-sama5d2 touchscreen information struct
577 * struct at91_adc_temp - at91-sama5d2 temperature information structure
591 * Divided by 2 because we need half words.
602 struct regulator *vref; member
762 for (i = 0; i < indio_dev->num_channels; i++) { in at91_adc_chan_xlate()
763 if (indio_dev->channels[i].scan_index == chan) in at91_adc_chan_xlate()
766 return -EINVAL; in at91_adc_chan_xlate()
776 return indio_dev->channels + index; in at91_adc_chan_get()
782 return at91_adc_chan_xlate(indio_dev, iiospec->args[0]); in at91_adc_fwnode_xlate()
791 for_each_set_bit(bit, indio_dev->active_scan_mask, in at91_adc_active_scan_mask_to_reg()
792 indio_dev->num_channels) { in at91_adc_active_scan_mask_to_reg()
795 mask |= BIT(chan->channel); in at91_adc_active_scan_mask_to_reg()
798 return mask & GENMASK(st->soc_info.platform->nr_channels, 0); in at91_adc_active_scan_mask_to_reg()
806 cor = BIT(chan->channel) | BIT(chan->channel2); in at91_adc_cor()
809 cor <<= st->soc_info.platform->layout->COR_diff_offset; in at91_adc_cor()
810 if (chan->differential) in at91_adc_cor()
820 if (st->soc_info.platform->layout->EOC_ISR) in at91_adc_irq_status()
829 if (st->soc_info.platform->layout->EOC_IMR) in at91_adc_irq_mask()
842 if (!st->soc_info.platform->layout->EOC_IDR) in at91_adc_eoc_dis()
848 if (!st->soc_info.platform->layout->EOC_IDR) in at91_adc_eoc_ena()
859 unsigned int osr_mask = st->soc_info.platform->osr_mask; in at91_adc_config_emr()
863 for (i = 0; i < st->soc_info.platform->oversampling_avail_no; i++) { in at91_adc_config_emr()
864 if (oversampling_ratio == st->soc_info.platform->oversampling_avail[i]) in at91_adc_config_emr()
867 if (i == st->soc_info.platform->oversampling_avail_no) in at91_adc_config_emr()
868 return -EINVAL; in at91_adc_config_emr()
894 ret = pm_runtime_resume_and_get(st->dev); in at91_adc_config_emr()
907 pm_runtime_mark_last_busy(st->dev); in at91_adc_config_emr()
908 pm_runtime_put_autosuspend(st->dev); in at91_adc_config_emr()
910 st->oversampling_ratio = oversampling_ratio; in at91_adc_config_emr()
919 if (st->oversampling_ratio == 1) in at91_adc_adjust_val_osr()
921 else if (st->oversampling_ratio == 4) in at91_adc_adjust_val_osr()
923 else if (st->oversampling_ratio == 16) in at91_adc_adjust_val_osr()
925 else if (st->oversampling_ratio == 64) in at91_adc_adjust_val_osr()
927 else if (st->oversampling_ratio == 256) in at91_adc_adjust_val_osr()
931 return -EINVAL; in at91_adc_adjust_val_osr()
935 * st->soc_info.platform->chan_realbits, so shift left diff bits. in at91_adc_adjust_val_osr()
937 diff = st->soc_info.platform->chan_realbits - nbits; in at91_adc_adjust_val_osr()
967 u32 clk_khz = st->current_sample_rate / 1000; in at91_adc_configure_touch()
973 ret = pm_runtime_resume_and_get(st->dev); in at91_adc_configure_touch()
982 pm_runtime_mark_last_busy(st->dev); in at91_adc_configure_touch()
983 pm_runtime_put_autosuspend(st->dev); in at91_adc_configure_touch()
1017 st->touch_st.sample_period_val = in at91_adc_configure_touch()
1019 clk_khz / 1000) - 1, 1); in at91_adc_configure_touch()
1034 * max = 2^AT91_SAMA5D2_MAX_POS_BITS - 1 in at91_adc_touch_pos()
1036 /* first half of register is the x or y, second half is the scale */ in at91_adc_touch_pos()
1037 if (reg == st->soc_info.platform->layout->XPOSR) in at91_adc_touch_pos()
1039 else if (reg == st->soc_info.platform->layout->YPOSR) in at91_adc_touch_pos()
1043 dev_dbg(&st->indio_dev->dev, "pos is 0\n"); in at91_adc_touch_pos()
1046 result = (pos << AT91_SAMA5D2_MAX_POS_BITS) - pos; in at91_adc_touch_pos()
1049 dev_err(&st->indio_dev->dev, "scale is 0\n"); in at91_adc_touch_pos()
1059 st->touch_st.x_pos = at91_adc_touch_pos(st, st->soc_info.platform->layout->XPOSR); in at91_adc_touch_x_pos()
1060 return st->touch_st.x_pos; in at91_adc_touch_x_pos()
1065 return at91_adc_touch_pos(st, st->soc_info.platform->layout->YPOSR); in at91_adc_touch_y_pos()
1082 pres = rxp * (st->touch_st.x_pos * factor / 1024) * in at91_adc_touch_pressure()
1083 (z2 * factor / z1 - factor) / in at91_adc_touch_pressure()
1093 return 0xFFFF - pres; in at91_adc_touch_pressure()
1099 if (!st->touch_st.touching) in at91_adc_read_position()
1100 return -ENODATA; in at91_adc_read_position()
1101 if (chan == st->soc_info.platform->touch_chan_x) in at91_adc_read_position()
1103 else if (chan == st->soc_info.platform->touch_chan_y) in at91_adc_read_position()
1106 return -ENODATA; in at91_adc_read_position()
1114 if (!st->touch_st.touching) in at91_adc_read_pressure()
1115 return -ENODATA; in at91_adc_read_pressure()
1116 if (chan == st->soc_info.platform->touch_chan_p) in at91_adc_read_pressure()
1119 return -ENODATA; in at91_adc_read_pressure()
1133 status |= st->selected_trig->trgmod_value; in at91_adc_configure_trigger_registers()
1146 ret = pm_runtime_resume_and_get(st->dev); in at91_adc_configure_trigger()
1154 pm_runtime_mark_last_busy(st->dev); in at91_adc_configure_trigger()
1155 pm_runtime_put_autosuspend(st->dev); in at91_adc_configure_trigger()
1167 if (st->dma_st.dma_chan) in at91_adc_reenable_trigger()
1170 enable_irq(st->irq); in at91_adc_reenable_trigger()
1188 status = dmaengine_tx_status(st->dma_st.dma_chan, in at91_adc_dma_size_done()
1189 st->dma_st.dma_chan->cookie, in at91_adc_dma_size_done()
1195 i = st->dma_st.rx_buf_sz - state.residue; in at91_adc_dma_size_done()
1198 if (i >= st->dma_st.buf_idx) in at91_adc_dma_size_done()
1199 size = i - st->dma_st.buf_idx; in at91_adc_dma_size_done()
1201 size = st->dma_st.rx_buf_sz + i - st->dma_st.buf_idx; in at91_adc_dma_size_done()
1209 iio_trigger_poll_nested(indio_dev->trig); in at91_dma_buffer_done()
1220 if (!st->dma_st.dma_chan) in at91_adc_dma_start()
1224 st->dma_st.buf_idx = 0; in at91_adc_dma_start()
1230 st->dma_st.rx_buf_sz = 0; in at91_adc_dma_start()
1232 for_each_set_bit(bit, indio_dev->active_scan_mask, in at91_adc_dma_start()
1233 indio_dev->num_channels) { in at91_adc_dma_start()
1240 st->dma_st.rx_buf_sz += chan->scan_type.storagebits / 8; in at91_adc_dma_start()
1242 st->dma_st.rx_buf_sz *= st->dma_st.watermark; in at91_adc_dma_start()
1245 desc = dmaengine_prep_dma_cyclic(st->dma_st.dma_chan, in at91_adc_dma_start()
1246 st->dma_st.rx_dma_buf, in at91_adc_dma_start()
1247 st->dma_st.rx_buf_sz, in at91_adc_dma_start()
1248 st->dma_st.rx_buf_sz / 2, in at91_adc_dma_start()
1252 dev_err(&indio_dev->dev, "cannot prepare DMA cyclic\n"); in at91_adc_dma_start()
1253 return -EBUSY; in at91_adc_dma_start()
1256 desc->callback = at91_dma_buffer_done; in at91_adc_dma_start()
1257 desc->callback_param = indio_dev; in at91_adc_dma_start()
1262 dev_err(&indio_dev->dev, "cannot submit DMA cyclic\n"); in at91_adc_dma_start()
1263 dmaengine_terminate_async(st->dma_st.dma_chan); in at91_adc_dma_start()
1270 dma_async_issue_pending(st->dma_st.dma_chan); in at91_adc_dma_start()
1273 st->dma_st.dma_ts = iio_get_time_ns(indio_dev); in at91_adc_dma_start()
1275 dev_dbg(&indio_dev->dev, "DMA cyclic started\n"); in at91_adc_dma_start()
1283 /* if using DMA, we do not use our own IRQ (we use DMA-controller) */ in at91_adc_buffer_check_use_irq()
1284 if (st->dma_st.dma_chan) in at91_adc_buffer_check_use_irq()
1287 if (iio_trigger_validate_own_device(indio->trig, indio)) in at91_adc_buffer_check_use_irq()
1296 return !!bitmap_subset(indio_dev->active_scan_mask, in at91_adc_current_chan_is_touch()
1297 &st->touch_st.channels_bitmask, in at91_adc_current_chan_is_touch()
1298 st->soc_info.platform->max_index + 1); in at91_adc_current_chan_is_touch()
1313 return -EINVAL; in at91_adc_buffer_prepare()
1315 ret = pm_runtime_resume_and_get(st->dev); in at91_adc_buffer_prepare()
1322 dev_err(&indio_dev->dev, "buffer prepare failed\n"); in at91_adc_buffer_prepare()
1326 for_each_set_bit(bit, indio_dev->active_scan_mask, in at91_adc_buffer_prepare()
1327 indio_dev->num_channels) { in at91_adc_buffer_prepare()
1333 if (chan->type == IIO_POSITIONRELATIVE || in at91_adc_buffer_prepare()
1334 chan->type == IIO_PRESSURE || in at91_adc_buffer_prepare()
1335 chan->type == IIO_TEMP) in at91_adc_buffer_prepare()
1340 at91_adc_writel(st, CHER, BIT(chan->channel)); in at91_adc_buffer_prepare()
1347 pm_runtime_mark_last_busy(st->dev); in at91_adc_buffer_prepare()
1348 pm_runtime_put_autosuspend(st->dev); in at91_adc_buffer_prepare()
1364 return -EINVAL; in at91_adc_buffer_postdisable()
1366 ret = pm_runtime_resume_and_get(st->dev); in at91_adc_buffer_postdisable()
1376 for_each_set_bit(bit, indio_dev->active_scan_mask, in at91_adc_buffer_postdisable()
1377 indio_dev->num_channels) { in at91_adc_buffer_postdisable()
1384 if (chan->type == IIO_POSITIONRELATIVE || in at91_adc_buffer_postdisable()
1385 chan->type == IIO_PRESSURE || in at91_adc_buffer_postdisable()
1386 chan->type == IIO_TEMP) in at91_adc_buffer_postdisable()
1389 at91_adc_writel(st, CHDR, BIT(chan->channel)); in at91_adc_buffer_postdisable()
1391 if (st->dma_st.dma_chan) in at91_adc_buffer_postdisable()
1392 at91_adc_read_chan(st, chan->address); in at91_adc_buffer_postdisable()
1402 if (st->dma_st.dma_chan) in at91_adc_buffer_postdisable()
1403 dmaengine_terminate_sync(st->dma_st.dma_chan); in at91_adc_buffer_postdisable()
1405 pm_runtime_mark_last_busy(st->dev); in at91_adc_buffer_postdisable()
1406 pm_runtime_put_autosuspend(st->dev); in at91_adc_buffer_postdisable()
1421 trig = devm_iio_trigger_alloc(&indio->dev, "%s-dev%d-%s", indio->name, in at91_adc_allocate_trigger()
1424 return ERR_PTR(-ENOMEM); in at91_adc_allocate_trigger()
1426 trig->dev.parent = indio->dev.parent; in at91_adc_allocate_trigger()
1428 trig->ops = &at91_adc_trigger_ops; in at91_adc_allocate_trigger()
1430 ret = devm_iio_trigger_register(&indio->dev, trig); in at91_adc_allocate_trigger()
1456 timeout--; in at91_adc_trigger_handler_nodma()
1463 for_each_set_bit(bit, indio_dev->active_scan_mask, in at91_adc_trigger_handler_nodma()
1464 indio_dev->num_channels) { in at91_adc_trigger_handler_nodma()
1480 if (chan->type == IIO_VOLTAGE) { in at91_adc_trigger_handler_nodma()
1481 val = at91_adc_read_chan(st, chan->address); in at91_adc_trigger_handler_nodma()
1483 st->buffer[i] = val; in at91_adc_trigger_handler_nodma()
1485 st->buffer[i] = 0; in at91_adc_trigger_handler_nodma()
1490 iio_push_to_buffers_with_timestamp(indio_dev, st->buffer, in at91_adc_trigger_handler_nodma()
1491 pf->timestamp); in at91_adc_trigger_handler_nodma()
1506 indio_dev->name); in at91_adc_trigger_handler_dma()
1508 sample_size = div_s64(st->dma_st.rx_buf_sz, st->dma_st.watermark); in at91_adc_trigger_handler_dma()
1516 interval = div_s64((ns - st->dma_st.dma_ts), sample_count); in at91_adc_trigger_handler_dma()
1524 &st->dma_st.rx_buf[st->dma_st.buf_idx], in at91_adc_trigger_handler_dma()
1528 (st->dma_st.rx_buf + st->dma_st.buf_idx), in at91_adc_trigger_handler_dma()
1529 (st->dma_st.dma_ts + interval * sample_index)); in at91_adc_trigger_handler_dma()
1531 transferred_len -= sample_size; in at91_adc_trigger_handler_dma()
1533 st->dma_st.buf_idx += sample_size; in at91_adc_trigger_handler_dma()
1535 if (st->dma_st.buf_idx >= st->dma_st.rx_buf_sz) in at91_adc_trigger_handler_dma()
1536 st->dma_st.buf_idx = 0; in at91_adc_trigger_handler_dma()
1540 st->dma_st.dma_ts = iio_get_time_ns(indio_dev); in at91_adc_trigger_handler_dma()
1546 struct iio_dev *indio_dev = pf->indio_dev; in at91_adc_trigger_handler()
1553 if (iio_trigger_validate_own_device(indio_dev->trig, indio_dev)) in at91_adc_trigger_handler()
1556 if (st->dma_st.dma_chan) in at91_adc_trigger_handler()
1561 iio_trigger_notify_done(indio_dev->trig); in at91_adc_trigger_handler()
1598 f_per = clk_get_rate(st->per_clk); in at91_adc_setup_samp_freq()
1599 prescal = (f_per / (2 * freq)) - 1; in at91_adc_setup_samp_freq()
1603 ret = pm_runtime_resume_and_get(st->dev); in at91_adc_setup_samp_freq()
1614 pm_runtime_mark_last_busy(st->dev); in at91_adc_setup_samp_freq()
1615 pm_runtime_put_autosuspend(st->dev); in at91_adc_setup_samp_freq()
1617 dev_dbg(&indio_dev->dev, "freq: %u, startup: %u, prescal: %u, tracktim=%u\n", in at91_adc_setup_samp_freq()
1619 st->current_sample_rate = freq; in at91_adc_setup_samp_freq()
1624 return st->current_sample_rate; in at91_adc_get_sample_freq()
1634 for_each_set_bit(bit, indio_dev->active_scan_mask, in at91_adc_touch_data_handler()
1635 st->soc_info.platform->max_index + 1) { in at91_adc_touch_data_handler()
1639 if (chan->type == IIO_POSITIONRELATIVE) in at91_adc_touch_data_handler()
1640 at91_adc_read_position(st, chan->channel, &val); in at91_adc_touch_data_handler()
1641 else if (chan->type == IIO_PRESSURE) in at91_adc_touch_data_handler()
1642 at91_adc_read_pressure(st, chan->channel, &val); in at91_adc_touch_data_handler()
1645 st->buffer[i] = val; in at91_adc_touch_data_handler()
1656 schedule_work(&st->touch_st.workq); in at91_adc_touch_data_handler()
1666 AT91_SAMA5D2_TRGR_TRGPER(st->touch_st.sample_period_val)); in at91_adc_pen_detect_interrupt()
1667 st->touch_st.touching = true; in at91_adc_pen_detect_interrupt()
1678 st->touch_st.touching = false; in at91_adc_no_pen_detect_interrupt()
1691 struct iio_dev *indio_dev = st->indio_dev; in at91_adc_workq_handler()
1693 iio_push_to_buffers(indio_dev, st->buffer); in at91_adc_workq_handler()
1717 /* periodic trigger IRQ - during pen sense */ in at91_adc_interrupt()
1731 iio_trigger_poll(indio->trig); in at91_adc_interrupt()
1732 } else if (iio_buffer_enabled(indio) && st->dma_st.dma_chan) { in at91_adc_interrupt()
1733 /* triggered buffer with DMA - should not happen */ in at91_adc_interrupt()
1738 st->conversion_value = at91_adc_read_chan(st, st->chan->address); in at91_adc_interrupt()
1739 st->conversion_done = true; in at91_adc_interrupt()
1740 wake_up_interruptible(&st->wq_data_available); in at91_adc_interrupt()
1745 /* This needs to be called with direct mode claimed and st->lock locked. */
1753 ret = pm_runtime_resume_and_get(st->dev); in at91_adc_read_info_raw()
1761 if (chan->type == IIO_POSITIONRELATIVE) { in at91_adc_read_info_raw()
1762 ret = at91_adc_read_position(st, chan->channel, in at91_adc_read_info_raw()
1770 if (chan->type == IIO_PRESSURE) { in at91_adc_read_info_raw()
1771 ret = at91_adc_read_pressure(st, chan->channel, in at91_adc_read_info_raw()
1782 st->chan = chan; in at91_adc_read_info_raw()
1785 at91_adc_writel(st, CHER, BIT(chan->channel)); in at91_adc_read_info_raw()
1791 if (chan->type == IIO_TEMP) in at91_adc_read_info_raw()
1793 at91_adc_eoc_ena(st, chan->channel); in at91_adc_read_info_raw()
1796 ret = wait_event_interruptible_timeout(st->wq_data_available, in at91_adc_read_info_raw()
1797 st->conversion_done, in at91_adc_read_info_raw()
1800 ret = -ETIMEDOUT; in at91_adc_read_info_raw()
1803 *val = st->conversion_value; in at91_adc_read_info_raw()
1805 if (chan->scan_type.sign == 's') in at91_adc_read_info_raw()
1807 chan->scan_type.realbits - 1); in at91_adc_read_info_raw()
1808 st->conversion_done = false; in at91_adc_read_info_raw()
1811 at91_adc_eoc_dis(st, st->chan->channel); in at91_adc_read_info_raw()
1812 if (chan->type == IIO_TEMP) in at91_adc_read_info_raw()
1814 at91_adc_writel(st, CHDR, BIT(chan->channel)); in at91_adc_read_info_raw()
1820 pm_runtime_mark_last_busy(st->dev); in at91_adc_read_info_raw()
1821 pm_runtime_put_autosuspend(st->dev); in at91_adc_read_info_raw()
1835 mutex_lock(&st->lock); in at91_adc_read_info_locked()
1837 mutex_unlock(&st->lock); in at91_adc_read_info_locked()
1861 st->temp_st.saved_sample_rate = st->current_sample_rate; in at91_adc_temp_sensor_configure()
1862 st->temp_st.saved_oversampling = st->oversampling_ratio; in at91_adc_temp_sensor_configure()
1865 sample_rate = st->temp_st.saved_sample_rate; in at91_adc_temp_sensor_configure()
1866 oversampling_ratio = st->temp_st.saved_oversampling; in at91_adc_temp_sensor_configure()
1867 startup_time = st->soc_info.startup_time; in at91_adc_temp_sensor_configure()
1872 at91_adc_setup_samp_freq(st->indio_dev, sample_rate, startup_time, in at91_adc_temp_sensor_configure()
1881 struct at91_adc_temp_sensor_clb *clb = &st->soc_info.temp_sensor_clb; in at91_adc_read_temp()
1889 mutex_lock(&st->lock); in at91_adc_read_temp()
1891 ret = pm_runtime_resume_and_get(st->dev); in at91_adc_read_temp()
1913 pm_runtime_mark_last_busy(st->dev); in at91_adc_read_temp()
1914 pm_runtime_put_autosuspend(st->dev); in at91_adc_read_temp()
1916 mutex_unlock(&st->lock); in at91_adc_read_temp()
1922 * Temp[milli] = p1[milli] + (vtemp * clb->p6 - clb->p4 * vbg)/ in at91_adc_read_temp()
1925 div1 = DIV_ROUND_CLOSEST_ULL(((u64)vtemp * clb->p6), vbg); in at91_adc_read_temp()
1927 div2 = DIV_ROUND_CLOSEST_ULL((u64)clb->p4, AT91_ADC_TS_VTEMP_DT); in at91_adc_read_temp()
1929 *val = clb->p1 + (int)div1 - (int)div2; in at91_adc_read_temp()
1945 *val = st->vref_uv / 1000; in at91_adc_read_raw()
1946 if (chan->differential) in at91_adc_read_raw()
1948 *val2 = chan->scan_type.realbits; in at91_adc_read_raw()
1952 if (chan->type != IIO_TEMP) in at91_adc_read_raw()
1953 return -EINVAL; in at91_adc_read_raw()
1961 *val = st->oversampling_ratio; in at91_adc_read_raw()
1965 return -EINVAL; in at91_adc_read_raw()
1979 if (val == st->oversampling_ratio) in at91_adc_write_raw()
1985 mutex_lock(&st->lock); in at91_adc_write_raw()
1988 mutex_unlock(&st->lock); in at91_adc_write_raw()
1992 if (val < st->soc_info.min_sample_rate || in at91_adc_write_raw()
1993 val > st->soc_info.max_sample_rate) in at91_adc_write_raw()
1994 return -EINVAL; in at91_adc_write_raw()
1999 mutex_lock(&st->lock); in at91_adc_write_raw()
2001 st->soc_info.startup_time, 0); in at91_adc_write_raw()
2002 mutex_unlock(&st->lock); in at91_adc_write_raw()
2006 return -EINVAL; in at91_adc_write_raw()
2019 *vals = (int *)st->soc_info.platform->oversampling_avail; in at91_adc_read_avail()
2021 *length = st->soc_info.platform->oversampling_avail_no; in at91_adc_read_avail()
2024 return -EINVAL; in at91_adc_read_avail()
2030 struct device *dev = &st->indio_dev->dev; in at91_adc_dma_init()
2033 unsigned int sample_size = st->soc_info.platform->nr_channels * 2; in at91_adc_dma_init()
2036 * such that DMA uses one half of the buffer (full fifo size) in at91_adc_dma_init()
2037 * and the software uses the other half to read/write. in at91_adc_dma_init()
2042 if (st->dma_st.dma_chan) in at91_adc_dma_init()
2045 st->dma_st.dma_chan = dma_request_chan(dev, "rx"); in at91_adc_dma_init()
2046 if (IS_ERR(st->dma_st.dma_chan)) { in at91_adc_dma_init()
2048 st->dma_st.dma_chan = NULL; in at91_adc_dma_init()
2052 st->dma_st.rx_buf = dma_alloc_coherent(st->dma_st.dma_chan->device->dev, in at91_adc_dma_init()
2054 &st->dma_st.rx_dma_buf, in at91_adc_dma_init()
2056 if (!st->dma_st.rx_buf) { in at91_adc_dma_init()
2063 config.src_addr = (phys_addr_t)(st->dma_st.phys_addr in at91_adc_dma_init()
2064 + st->soc_info.platform->layout->LCDR); in at91_adc_dma_init()
2069 if (dmaengine_slave_config(st->dma_st.dma_chan, &config)) { in at91_adc_dma_init()
2075 dma_chan_name(st->dma_st.dma_chan)); in at91_adc_dma_init()
2080 dma_free_coherent(st->dma_st.dma_chan->device->dev, pages * PAGE_SIZE, in at91_adc_dma_init()
2081 st->dma_st.rx_buf, st->dma_st.rx_dma_buf); in at91_adc_dma_init()
2083 dma_release_channel(st->dma_st.dma_chan); in at91_adc_dma_init()
2084 st->dma_st.dma_chan = NULL; in at91_adc_dma_init()
2091 struct device *dev = &st->indio_dev->dev; in at91_adc_dma_disable()
2093 unsigned int sample_size = st->soc_info.platform->nr_channels * 2; in at91_adc_dma_disable()
2098 if (!st->dma_st.dma_chan) in at91_adc_dma_disable()
2102 dmaengine_terminate_sync(st->dma_st.dma_chan); in at91_adc_dma_disable()
2104 dma_free_coherent(st->dma_st.dma_chan->device->dev, pages * PAGE_SIZE, in at91_adc_dma_disable()
2105 st->dma_st.rx_buf, st->dma_st.rx_dma_buf); in at91_adc_dma_disable()
2106 dma_release_channel(st->dma_st.dma_chan); in at91_adc_dma_disable()
2107 st->dma_st.dma_chan = NULL; in at91_adc_dma_disable()
2120 if (!st->selected_trig->hw_trig) { in at91_adc_set_watermark()
2121 dev_dbg(&indio_dev->dev, "we need hw trigger for DMA\n"); in at91_adc_set_watermark()
2125 dev_dbg(&indio_dev->dev, "new watermark is %u\n", val); in at91_adc_set_watermark()
2126 st->dma_st.watermark = val; in at91_adc_set_watermark()
2155 if (bitmap_subset(scan_mask, &st->touch_st.channels_bitmask, in at91_adc_update_scan_mode()
2156 st->soc_info.platform->max_index + 1)) in at91_adc_update_scan_mode()
2162 if (bitmap_intersects(&st->touch_st.channels_bitmask, scan_mask, in at91_adc_update_scan_mode()
2163 st->soc_info.platform->max_index + 1)) in at91_adc_update_scan_mode()
2164 return -EINVAL; in at91_adc_update_scan_mode()
2173 if (st->soc_info.platform->layout->EOC_IDR) in at91_adc_hw_init()
2183 at91_adc_setup_samp_freq(indio_dev, st->soc_info.min_sample_rate, in at91_adc_hw_init()
2184 st->soc_info.startup_time, 0); in at91_adc_hw_init()
2187 at91_adc_config_emr(st, st->oversampling_ratio, 0); in at91_adc_hw_init()
2196 return sysfs_emit(buf, "%d\n", !!st->dma_st.dma_chan); in at91_adc_get_fifo_state()
2205 return sysfs_emit(buf, "%d\n", st->dma_st.watermark); in at91_adc_get_watermark()
2240 if (st->selected_trig->hw_trig) in at91_adc_buffer_and_trigger_init()
2245 ret = devm_iio_triggered_buffer_setup_ext(&indio->dev, indio, in at91_adc_buffer_and_trigger_init()
2253 if (!st->selected_trig->hw_trig) in at91_adc_buffer_and_trigger_init()
2256 st->trig = at91_adc_allocate_trigger(indio, st->selected_trig->name); in at91_adc_buffer_and_trigger_init()
2257 if (IS_ERR(st->trig)) { in at91_adc_buffer_and_trigger_init()
2259 return PTR_ERR(st->trig); in at91_adc_buffer_and_trigger_init()
2266 st->dma_st.watermark = 1; in at91_adc_buffer_and_trigger_init()
2274 struct at91_adc_temp_sensor_clb *clb = &st->soc_info.temp_sensor_clb; in at91_adc_temp_sensor_init()
2280 if (!st->soc_info.platform->temp_sensor) in at91_adc_temp_sensor_init()
2287 if (ret != -ENOENT) in at91_adc_temp_sensor_init()
2299 ret = -EINVAL; in at91_adc_temp_sensor_init()
2304 clb->p1 = buf[AT91_ADC_TS_CLB_IDX_P1]; in at91_adc_temp_sensor_init()
2305 clb->p4 = buf[AT91_ADC_TS_CLB_IDX_P4]; in at91_adc_temp_sensor_init()
2306 clb->p6 = buf[AT91_ADC_TS_CLB_IDX_P6]; in at91_adc_temp_sensor_init()
2311 clb->p1 = clb->p1 * 1000; in at91_adc_temp_sensor_init()
2320 struct device *dev = &pdev->dev; in at91_adc_probe()
2327 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*st)); in at91_adc_probe()
2329 return -ENOMEM; in at91_adc_probe()
2332 st->indio_dev = indio_dev; in at91_adc_probe()
2334 st->soc_info.platform = device_get_match_data(dev); in at91_adc_probe()
2336 ret = at91_adc_temp_sensor_init(st, &pdev->dev); in at91_adc_probe()
2339 num_channels = st->soc_info.platform->max_channels - 1; in at91_adc_probe()
2341 num_channels = st->soc_info.platform->max_channels; in at91_adc_probe()
2343 indio_dev->name = dev_name(&pdev->dev); in at91_adc_probe()
2344 indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE; in at91_adc_probe()
2345 indio_dev->info = &at91_adc_info; in at91_adc_probe()
2346 indio_dev->channels = *st->soc_info.platform->adc_channels; in at91_adc_probe()
2347 indio_dev->num_channels = num_channels; in at91_adc_probe()
2349 bitmap_set(&st->touch_st.channels_bitmask, in at91_adc_probe()
2350 st->soc_info.platform->touch_chan_x, 1); in at91_adc_probe()
2351 bitmap_set(&st->touch_st.channels_bitmask, in at91_adc_probe()
2352 st->soc_info.platform->touch_chan_y, 1); in at91_adc_probe()
2353 bitmap_set(&st->touch_st.channels_bitmask, in at91_adc_probe()
2354 st->soc_info.platform->touch_chan_p, 1); in at91_adc_probe()
2356 st->oversampling_ratio = 1; in at91_adc_probe()
2358 ret = device_property_read_u32(dev, "atmel,min-sample-rate-hz", in at91_adc_probe()
2359 &st->soc_info.min_sample_rate); in at91_adc_probe()
2361 dev_err(&pdev->dev, in at91_adc_probe()
2362 "invalid or missing value for atmel,min-sample-rate-hz\n"); in at91_adc_probe()
2366 ret = device_property_read_u32(dev, "atmel,max-sample-rate-hz", in at91_adc_probe()
2367 &st->soc_info.max_sample_rate); in at91_adc_probe()
2369 dev_err(&pdev->dev, in at91_adc_probe()
2370 "invalid or missing value for atmel,max-sample-rate-hz\n"); in at91_adc_probe()
2374 ret = device_property_read_u32(dev, "atmel,startup-time-ms", in at91_adc_probe()
2375 &st->soc_info.startup_time); in at91_adc_probe()
2377 dev_err(&pdev->dev, in at91_adc_probe()
2378 "invalid or missing value for atmel,startup-time-ms\n"); in at91_adc_probe()
2382 ret = device_property_read_u32(dev, "atmel,trigger-edge-type", in at91_adc_probe()
2385 dev_dbg(&pdev->dev, in at91_adc_probe()
2386 "atmel,trigger-edge-type not specified, only software trigger available\n"); in at91_adc_probe()
2389 st->selected_trig = NULL; in at91_adc_probe()
2392 for (i = 0; i < st->soc_info.platform->hw_trig_cnt + 1; i++) in at91_adc_probe()
2394 st->selected_trig = &at91_adc_trigger_list[i]; in at91_adc_probe()
2398 if (!st->selected_trig) { in at91_adc_probe()
2399 dev_err(&pdev->dev, "invalid external trigger edge value\n"); in at91_adc_probe()
2400 return -EINVAL; in at91_adc_probe()
2403 init_waitqueue_head(&st->wq_data_available); in at91_adc_probe()
2404 mutex_init(&st->lock); in at91_adc_probe()
2405 INIT_WORK(&st->touch_st.workq, at91_adc_workq_handler); in at91_adc_probe()
2407 st->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); in at91_adc_probe()
2408 if (IS_ERR(st->base)) in at91_adc_probe()
2409 return PTR_ERR(st->base); in at91_adc_probe()
2412 st->dma_st.phys_addr = res->start; in at91_adc_probe()
2414 st->irq = platform_get_irq(pdev, 0); in at91_adc_probe()
2415 if (st->irq < 0) in at91_adc_probe()
2416 return st->irq; in at91_adc_probe()
2418 st->per_clk = devm_clk_get(&pdev->dev, "adc_clk"); in at91_adc_probe()
2419 if (IS_ERR(st->per_clk)) in at91_adc_probe()
2420 return PTR_ERR(st->per_clk); in at91_adc_probe()
2422 st->reg = devm_regulator_get(&pdev->dev, "vddana"); in at91_adc_probe()
2423 if (IS_ERR(st->reg)) in at91_adc_probe()
2424 return PTR_ERR(st->reg); in at91_adc_probe()
2426 st->vref = devm_regulator_get(&pdev->dev, "vref"); in at91_adc_probe()
2427 if (IS_ERR(st->vref)) in at91_adc_probe()
2428 return PTR_ERR(st->vref); in at91_adc_probe()
2430 ret = devm_request_irq(&pdev->dev, st->irq, at91_adc_interrupt, 0, in at91_adc_probe()
2431 pdev->dev.driver->name, indio_dev); in at91_adc_probe()
2435 ret = regulator_enable(st->reg); in at91_adc_probe()
2439 ret = regulator_enable(st->vref); in at91_adc_probe()
2443 st->vref_uv = regulator_get_voltage(st->vref); in at91_adc_probe()
2444 if (st->vref_uv <= 0) { in at91_adc_probe()
2445 ret = -EINVAL; in at91_adc_probe()
2449 ret = clk_prepare_enable(st->per_clk); in at91_adc_probe()
2454 st->dev = &pdev->dev; in at91_adc_probe()
2455 pm_runtime_set_autosuspend_delay(st->dev, 500); in at91_adc_probe()
2456 pm_runtime_use_autosuspend(st->dev); in at91_adc_probe()
2457 pm_runtime_set_active(st->dev); in at91_adc_probe()
2458 pm_runtime_enable(st->dev); in at91_adc_probe()
2459 pm_runtime_get_noresume(st->dev); in at91_adc_probe()
2463 ret = at91_adc_buffer_and_trigger_init(&pdev->dev, indio_dev); in at91_adc_probe()
2467 if (dma_coerce_mask_and_coherent(&indio_dev->dev, DMA_BIT_MASK(32))) in at91_adc_probe()
2468 dev_info(&pdev->dev, "cannot set DMA mask to 32-bit\n"); in at91_adc_probe()
2474 if (st->selected_trig->hw_trig) in at91_adc_probe()
2475 dev_info(&pdev->dev, "setting up trigger as %s\n", in at91_adc_probe()
2476 st->selected_trig->name); in at91_adc_probe()
2478 dev_info(&pdev->dev, "version: %x\n", in at91_adc_probe()
2479 readl_relaxed(st->base + st->soc_info.platform->layout->VERSION)); in at91_adc_probe()
2481 pm_runtime_mark_last_busy(st->dev); in at91_adc_probe()
2482 pm_runtime_put_autosuspend(st->dev); in at91_adc_probe()
2489 pm_runtime_put_noidle(st->dev); in at91_adc_probe()
2490 pm_runtime_disable(st->dev); in at91_adc_probe()
2491 pm_runtime_set_suspended(st->dev); in at91_adc_probe()
2492 pm_runtime_dont_use_autosuspend(st->dev); in at91_adc_probe()
2493 clk_disable_unprepare(st->per_clk); in at91_adc_probe()
2495 regulator_disable(st->vref); in at91_adc_probe()
2497 regulator_disable(st->reg); in at91_adc_probe()
2510 pm_runtime_disable(st->dev); in at91_adc_remove()
2511 pm_runtime_set_suspended(st->dev); in at91_adc_remove()
2512 clk_disable_unprepare(st->per_clk); in at91_adc_remove()
2514 regulator_disable(st->vref); in at91_adc_remove()
2515 regulator_disable(st->reg); in at91_adc_remove()
2526 ret = pm_runtime_resume_and_get(st->dev); in at91_adc_suspend()
2541 pm_runtime_mark_last_busy(st->dev); in at91_adc_suspend()
2542 pm_runtime_put_noidle(st->dev); in at91_adc_suspend()
2543 clk_disable_unprepare(st->per_clk); in at91_adc_suspend()
2544 regulator_disable(st->vref); in at91_adc_suspend()
2545 regulator_disable(st->reg); in at91_adc_suspend()
2560 ret = regulator_enable(st->reg); in at91_adc_resume()
2564 ret = regulator_enable(st->vref); in at91_adc_resume()
2568 ret = clk_prepare_enable(st->per_clk); in at91_adc_resume()
2572 pm_runtime_get_noresume(st->dev); in at91_adc_resume()
2585 pm_runtime_mark_last_busy(st->dev); in at91_adc_resume()
2586 pm_runtime_put_autosuspend(st->dev); in at91_adc_resume()
2591 pm_runtime_mark_last_busy(st->dev); in at91_adc_resume()
2592 pm_runtime_put_noidle(st->dev); in at91_adc_resume()
2593 clk_disable_unprepare(st->per_clk); in at91_adc_resume()
2595 regulator_disable(st->vref); in at91_adc_resume()
2597 regulator_disable(st->reg); in at91_adc_resume()
2599 dev_err(&indio_dev->dev, "failed to resume\n"); in at91_adc_resume()
2608 clk_disable(st->per_clk); in at91_adc_runtime_suspend()
2618 return clk_enable(st->per_clk); in at91_adc_runtime_resume()
2629 .compatible = "atmel,sama5d2-adc",
2632 .compatible = "microchip,sama7g5-adc",
2644 .name = "at91-sama5d2_adc",