|
16 | 16 | #include <linux/delay.h>
|
17 | 17 | #include <linux/device.h>
|
18 | 18 | #include <linux/err.h>
|
| 19 | +#include <linux/iio/buffer.h> |
19 | 20 | #include <linux/iio/iio.h>
|
| 21 | +#include <linux/iio/trigger.h> |
| 22 | +#include <linux/iio/trigger_consumer.h> |
| 23 | +#include <linux/iio/triggered_buffer.h> |
20 | 24 | #include <linux/interrupt.h>
|
21 | 25 | #include <linux/irq.h>
|
22 | 26 | #include <linux/math64.h>
|
|
61 | 65 | #define AD4170_FILTER_FS_REG(x) (0xC7 + 14 * (x))
|
62 | 66 | #define AD4170_OFFSET_REG(x) (0xCA + 14 * (x))
|
63 | 67 | #define AD4170_GAIN_REG(x) (0xCD + 14 * (x))
|
| 68 | +#define AD4170_ADC_CTRL_CONT_READ_EXIT_REG 0x200 /* virtual reg */ |
64 | 69 |
|
65 | 70 | #define AD4170_REG_READ_MASK BIT(14)
|
66 | 71 |
|
|
72 | 77 |
|
73 | 78 | /* AD4170_ADC_CTRL_REG */
|
74 | 79 | #define AD4170_ADC_CTRL_MULTI_DATA_REG_SEL_MSK BIT(7)
|
| 80 | +#define AD4170_ADC_CTRL_CONT_READ_MSK GENMASK(5, 4) |
75 | 81 | #define AD4170_ADC_CTRL_MODE_MSK GENMASK(3, 0)
|
76 | 82 |
|
77 | 83 | /* AD4170_CHAN_EN_REG */
|
|
116 | 122 | #define AD4170_PIN_MUXING_DIG_AUX1_RDY 0x1
|
117 | 123 |
|
118 | 124 | /* AD4170_ADC_CTRL_REG constants */
|
| 125 | +#define AD4170_ADC_CTRL_MODE_CONT 0x0 |
119 | 126 | #define AD4170_ADC_CTRL_MODE_SINGLE 0x4
|
120 | 127 | #define AD4170_ADC_CTRL_MODE_IDLE 0x7
|
121 | 128 |
|
| 129 | +#define AD4170_ADC_CTRL_CONT_READ_DISABLE 0x0 |
| 130 | +#define AD4170_ADC_CTRL_CONT_READ_ENABLE 0x1 |
| 131 | + |
122 | 132 | /* AD4170_FILTER_REG constants */
|
123 | 133 | #define AD4170_FILTER_FILTER_TYPE_SINC5_AVG 0x0
|
124 | 134 | #define AD4170_FILTER_FILTER_TYPE_SINC5 0x4
|
|
150 | 160 |
|
151 | 161 | #define AD4170_GAIN_REG_DEFAULT 0x555555
|
152 | 162 |
|
| 163 | +#define AD4170_ADC_CTRL_CONT_READ_EXIT 0xA5 |
| 164 | + |
153 | 165 | static const unsigned int ad4170_reg_size[] = {
|
154 | 166 | [AD4170_CONFIG_A_REG] = 1,
|
155 | 167 | [AD4170_DATA_24B_REG] = 3,
|
@@ -186,6 +198,7 @@ static const unsigned int ad4170_reg_size[] = {
|
186 | 198 | [AD4170_OFFSET_REG(5) ... AD4170_GAIN_REG(5)] = 3,
|
187 | 199 | [AD4170_OFFSET_REG(6) ... AD4170_GAIN_REG(6)] = 3,
|
188 | 200 | [AD4170_OFFSET_REG(7) ... AD4170_GAIN_REG(7)] = 3,
|
| 201 | + [AD4170_ADC_CTRL_CONT_READ_EXIT_REG] = 0, |
189 | 202 | };
|
190 | 203 |
|
191 | 204 | enum ad4170_ref_buf {
|
@@ -325,6 +338,10 @@ struct ad4170_state {
|
325 | 338 | struct spi_device *spi;
|
326 | 339 | struct regmap *regmap;
|
327 | 340 | int sps_tbl[ARRAY_SIZE(ad4170_filt_names)][AD4170_MAX_FS_TBL_SIZE][2];
|
| 341 | + __be32 bounce_buffer[AD4170_MAX_ADC_CHANNELS]; |
| 342 | + struct spi_message msg; |
| 343 | + struct spi_transfer xfer; |
| 344 | + struct iio_trigger *trig; |
328 | 345 | unsigned int pins_fn[AD4170_NUM_ANALOG_PINS];
|
329 | 346 | /*
|
330 | 347 | * DMA (thus cache coherency maintenance) requires the transfer buffers
|
@@ -410,6 +427,10 @@ static int ad4170_reg_write(void *context, unsigned int reg, unsigned int val)
|
410 | 427 | case 1:
|
411 | 428 | tx_buf[AD4170_SPI_INST_PHASE_LEN] = val;
|
412 | 429 | break;
|
| 430 | + case 0: |
| 431 | + /* Write continuous read exit code */ |
| 432 | + tx_buf[0] = AD4170_ADC_CTRL_CONT_READ_EXIT; |
| 433 | + return spi_write_then_read(st->spi, tx_buf, 1, NULL, 0); |
413 | 434 | default:
|
414 | 435 | return -EINVAL;
|
415 | 436 | }
|
@@ -803,6 +824,7 @@ static const struct iio_chan_spec ad4170_channel_template = {
|
803 | 824 | .scan_type = {
|
804 | 825 | .realbits = 24,
|
805 | 826 | .storagebits = 32,
|
| 827 | + .shift = 8, |
806 | 828 | .endianness = IIO_BE,
|
807 | 829 | },
|
808 | 830 | };
|
@@ -1392,11 +1414,27 @@ static int ad4170_write_raw_get_fmt(struct iio_dev *indio_dev,
|
1392 | 1414 | }
|
1393 | 1415 | }
|
1394 | 1416 |
|
| 1417 | +static int ad4170_update_scan_mode(struct iio_dev *indio_dev, |
| 1418 | + const unsigned long *active_scan_mask) |
| 1419 | +{ |
| 1420 | + struct ad4170_state *st = iio_priv(indio_dev); |
| 1421 | + unsigned int chan_index; |
| 1422 | + int ret; |
| 1423 | + |
| 1424 | + iio_for_each_active_channel(indio_dev, chan_index) { |
| 1425 | + ret = ad4170_set_channel_enable(st, chan_index, true); |
| 1426 | + if (ret) |
| 1427 | + return ret; |
| 1428 | + } |
| 1429 | + return 0; |
| 1430 | +} |
| 1431 | + |
1395 | 1432 | static const struct iio_info ad4170_info = {
|
1396 | 1433 | .read_raw = ad4170_read_raw,
|
1397 | 1434 | .read_avail = ad4170_read_avail,
|
1398 | 1435 | .write_raw = ad4170_write_raw,
|
1399 | 1436 | .write_raw_get_fmt = ad4170_write_raw_get_fmt,
|
| 1437 | + .update_scan_mode = ad4170_update_scan_mode, |
1400 | 1438 | .debugfs_reg_access = ad4170_debugfs_reg_access,
|
1401 | 1439 | };
|
1402 | 1440 |
|
@@ -1700,16 +1738,178 @@ static int ad4170_initial_config(struct iio_dev *indio_dev)
|
1700 | 1738 | AD4170_ADC_CTRL_MULTI_DATA_REG_SEL_MSK);
|
1701 | 1739 | }
|
1702 | 1740 |
|
| 1741 | +static int ad4170_prepare_spi_message(struct ad4170_state *st) |
| 1742 | +{ |
| 1743 | + /* |
| 1744 | + * Continuous data register read is enabled on buffer postenable so |
| 1745 | + * no instruction phase is needed meaning we don't need to send the |
| 1746 | + * register address to read data. Transfer only needs the read buffer. |
| 1747 | + */ |
| 1748 | + st->xfer.rx_buf = &st->rx_buf; |
| 1749 | + st->xfer.len = BITS_TO_BYTES(ad4170_channel_template.scan_type.realbits); |
| 1750 | + |
| 1751 | + spi_message_init_with_transfers(&st->msg, &st->xfer, 1); |
| 1752 | + |
| 1753 | + return devm_spi_optimize_message(&st->spi->dev, st->spi, &st->msg); |
| 1754 | +} |
| 1755 | + |
| 1756 | +static int ad4170_buffer_postenable(struct iio_dev *indio_dev) |
| 1757 | +{ |
| 1758 | + struct ad4170_state *st = iio_priv(indio_dev); |
| 1759 | + int ret; |
| 1760 | + |
| 1761 | + ret = regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG, |
| 1762 | + AD4170_ADC_CTRL_MODE_MSK, |
| 1763 | + FIELD_PREP(AD4170_ADC_CTRL_MODE_MSK, |
| 1764 | + AD4170_ADC_CTRL_MODE_CONT)); |
| 1765 | + if (ret) |
| 1766 | + return ret; |
| 1767 | + |
| 1768 | + /* |
| 1769 | + * This enables continuous read of the ADC data register. The ADC must |
| 1770 | + * be in continuous conversion mode. |
| 1771 | + */ |
| 1772 | + return regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG, |
| 1773 | + AD4170_ADC_CTRL_CONT_READ_MSK, |
| 1774 | + FIELD_PREP(AD4170_ADC_CTRL_CONT_READ_MSK, |
| 1775 | + AD4170_ADC_CTRL_CONT_READ_ENABLE)); |
| 1776 | +} |
| 1777 | + |
| 1778 | +static int ad4170_buffer_predisable(struct iio_dev *indio_dev) |
| 1779 | +{ |
| 1780 | + struct ad4170_state *st = iio_priv(indio_dev); |
| 1781 | + unsigned int i; |
| 1782 | + int ret; |
| 1783 | + |
| 1784 | + /* |
| 1785 | + * Use a high register address (virtual register) to request a write of |
| 1786 | + * 0xA5 to the ADC during the first 8 SCLKs of the ADC data read cycle, |
| 1787 | + * thus exiting continuous read. |
| 1788 | + */ |
| 1789 | + ret = regmap_write(st->regmap, AD4170_ADC_CTRL_CONT_READ_EXIT_REG, 0); |
| 1790 | + if (ret) |
| 1791 | + return ret; |
| 1792 | + |
| 1793 | + ret = regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG, |
| 1794 | + AD4170_ADC_CTRL_CONT_READ_MSK, |
| 1795 | + FIELD_PREP(AD4170_ADC_CTRL_CONT_READ_MSK, |
| 1796 | + AD4170_ADC_CTRL_CONT_READ_DISABLE)); |
| 1797 | + if (ret) |
| 1798 | + return ret; |
| 1799 | + |
| 1800 | + ret = regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG, |
| 1801 | + AD4170_ADC_CTRL_MODE_MSK, |
| 1802 | + FIELD_PREP(AD4170_ADC_CTRL_MODE_MSK, |
| 1803 | + AD4170_ADC_CTRL_MODE_IDLE)); |
| 1804 | + if (ret) |
| 1805 | + return ret; |
| 1806 | + |
| 1807 | + /* |
| 1808 | + * The ADC sequences through all the enabled channels (see datasheet |
| 1809 | + * page 95). That can lead to incorrect channel being read if a |
| 1810 | + * single-shot read (or buffered read with different active_scan_mask) |
| 1811 | + * is done after buffer disable. Disable all channels so only requested |
| 1812 | + * channels will be read. |
| 1813 | + */ |
| 1814 | + for (i = 0; i < indio_dev->num_channels; i++) { |
| 1815 | + ret = ad4170_set_channel_enable(st, i, false); |
| 1816 | + if (ret) |
| 1817 | + return ret; |
| 1818 | + } |
| 1819 | + |
| 1820 | + return 0; |
| 1821 | +} |
| 1822 | + |
| 1823 | +static bool ad4170_validate_scan_mask(struct iio_dev *indio_dev, |
| 1824 | + const unsigned long *scan_mask) |
| 1825 | +{ |
| 1826 | + unsigned int masklength = iio_get_masklength(indio_dev); |
| 1827 | + unsigned int enabled; |
| 1828 | + |
| 1829 | + /* |
| 1830 | + * The channel sequencer cycles through the enabled channels in |
| 1831 | + * sequential order, from channel 0 to channel 15, bypassing disabled |
| 1832 | + * channels. When more than one channel is enabled, channel 0 must |
| 1833 | + * always be enabled. See datasheet channel_en register description at |
| 1834 | + * page 95. |
| 1835 | + */ |
| 1836 | + enabled = bitmap_weight(scan_mask, masklength); |
| 1837 | + if (enabled > 1) |
| 1838 | + return test_bit(0, scan_mask); |
| 1839 | + |
| 1840 | + return enabled == 1; |
| 1841 | +} |
| 1842 | + |
| 1843 | +static const struct iio_buffer_setup_ops ad4170_buffer_ops = { |
| 1844 | + .postenable = ad4170_buffer_postenable, |
| 1845 | + .predisable = ad4170_buffer_predisable, |
| 1846 | + .validate_scan_mask = ad4170_validate_scan_mask, |
| 1847 | +}; |
| 1848 | + |
| 1849 | +static irqreturn_t ad4170_trigger_handler(int irq, void *p) |
| 1850 | +{ |
| 1851 | + struct iio_poll_func *pf = p; |
| 1852 | + struct iio_dev *indio_dev = pf->indio_dev; |
| 1853 | + struct ad4170_state *st = iio_priv(indio_dev); |
| 1854 | + unsigned int chan_index; |
| 1855 | + unsigned int i = 0; |
| 1856 | + int ret; |
| 1857 | + |
| 1858 | + iio_for_each_active_channel(indio_dev, chan_index) { |
| 1859 | + ret = spi_sync(st->spi, &st->msg); |
| 1860 | + if (ret) |
| 1861 | + goto err_out; |
| 1862 | + |
| 1863 | + memcpy(&st->bounce_buffer[i++], st->rx_buf, ARRAY_SIZE(st->rx_buf)); |
| 1864 | + } |
| 1865 | + |
| 1866 | + iio_push_to_buffers(indio_dev, st->bounce_buffer); |
| 1867 | +err_out: |
| 1868 | + iio_trigger_notify_done(indio_dev->trig); |
| 1869 | + return IRQ_HANDLED; |
| 1870 | +} |
| 1871 | + |
| 1872 | +static const struct iio_trigger_ops ad4170_trigger_ops = { |
| 1873 | + .validate_device = iio_trigger_validate_own_device, |
| 1874 | +}; |
| 1875 | + |
1703 | 1876 | static irqreturn_t ad4170_irq_handler(int irq, void *dev_id)
|
1704 | 1877 | {
|
1705 | 1878 | struct iio_dev *indio_dev = dev_id;
|
1706 | 1879 | struct ad4170_state *st = iio_priv(indio_dev);
|
1707 | 1880 |
|
1708 |
| - complete(&st->completion); |
| 1881 | + if (iio_buffer_enabled(indio_dev)) |
| 1882 | + iio_trigger_poll(st->trig); |
| 1883 | + else |
| 1884 | + complete(&st->completion); |
1709 | 1885 |
|
1710 | 1886 | return IRQ_HANDLED;
|
1711 | 1887 | };
|
1712 | 1888 |
|
| 1889 | +static int ad4170_trigger_setup(struct iio_dev *indio_dev) |
| 1890 | +{ |
| 1891 | + struct ad4170_state *st = iio_priv(indio_dev); |
| 1892 | + struct device *dev = &st->spi->dev; |
| 1893 | + int ret; |
| 1894 | + |
| 1895 | + st->trig = devm_iio_trigger_alloc(dev, "%s-trig%d", |
| 1896 | + indio_dev->name, |
| 1897 | + iio_device_id(indio_dev)); |
| 1898 | + if (!st->trig) |
| 1899 | + return -ENOMEM; |
| 1900 | + |
| 1901 | + st->trig->ops = &ad4170_trigger_ops; |
| 1902 | + |
| 1903 | + iio_trigger_set_drvdata(st->trig, indio_dev); |
| 1904 | + ret = devm_iio_trigger_register(dev, st->trig); |
| 1905 | + if (ret) |
| 1906 | + return dev_err_probe(dev, ret, "Failed to register trigger\n"); |
| 1907 | + |
| 1908 | + indio_dev->trig = iio_trigger_get(st->trig); |
| 1909 | + |
| 1910 | + return 0; |
| 1911 | +} |
| 1912 | + |
1713 | 1913 | static int ad4170_regulator_setup(struct ad4170_state *st)
|
1714 | 1914 | {
|
1715 | 1915 | struct device *dev = &st->spi->dev;
|
@@ -1834,8 +2034,22 @@ static int ad4170_probe(struct spi_device *spi)
|
1834 | 2034 | IRQF_ONESHOT, indio_dev->name, indio_dev);
|
1835 | 2035 | if (ret)
|
1836 | 2036 | return ret;
|
| 2037 | + |
| 2038 | + ret = ad4170_trigger_setup(indio_dev); |
| 2039 | + if (ret) |
| 2040 | + return ret; |
1837 | 2041 | }
|
1838 | 2042 |
|
| 2043 | + ret = ad4170_prepare_spi_message(st); |
| 2044 | + if (ret) |
| 2045 | + return dev_err_probe(dev, ret, "Failed to prepare SPI message\n"); |
| 2046 | + |
| 2047 | + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, |
| 2048 | + &ad4170_trigger_handler, |
| 2049 | + &ad4170_buffer_ops); |
| 2050 | + if (ret) |
| 2051 | + return dev_err_probe(dev, ret, "Failed to setup read buffer\n"); |
| 2052 | + |
1839 | 2053 | return devm_iio_device_register(dev, indio_dev);
|
1840 | 2054 | }
|
1841 | 2055 |
|
|
0 commit comments