|
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 {
|
@@ -320,6 +333,10 @@ struct ad4170_state {
|
320 | 333 | struct spi_device *spi;
|
321 | 334 | struct regmap *regmap;
|
322 | 335 | int sps_tbl[ARRAY_SIZE(ad4170_filt_names)][AD4170_MAX_FS_TBL_SIZE][2];
|
| 336 | + __be32 bounce_buffer[AD4170_MAX_CHANNELS]; |
| 337 | + struct spi_message msg; |
| 338 | + struct spi_transfer xfer; |
| 339 | + struct iio_trigger *trig; |
323 | 340 | struct completion completion;
|
324 | 341 | unsigned int pins_fn[AD4170_NUM_ANALOG_PINS];
|
325 | 342 | u32 int_pin_sel;
|
@@ -407,6 +424,10 @@ static int ad4170_reg_write(void *context, unsigned int reg, unsigned int val)
|
407 | 424 | case 1:
|
408 | 425 | tx_buf[AD4170_SPI_INST_PHASE_LEN] = val;
|
409 | 426 | break;
|
| 427 | + case 0: |
| 428 | + /* Write continuous read exit code */ |
| 429 | + tx_buf[0] = AD4170_ADC_CTRL_CONT_READ_EXIT; |
| 430 | + return spi_write_then_read(st->spi, tx_buf, 1, NULL, 0); |
410 | 431 | default:
|
411 | 432 | return -EINVAL;
|
412 | 433 | }
|
@@ -800,6 +821,7 @@ static const struct iio_chan_spec ad4170_channel_template = {
|
800 | 821 | .scan_type = {
|
801 | 822 | .realbits = 24,
|
802 | 823 | .storagebits = 32,
|
| 824 | + .shift = 8, |
803 | 825 | .endianness = IIO_BE,
|
804 | 826 | },
|
805 | 827 | };
|
@@ -1384,11 +1406,27 @@ static int ad4170_write_raw_get_fmt(struct iio_dev *indio_dev,
|
1384 | 1406 | }
|
1385 | 1407 | }
|
1386 | 1408 |
|
| 1409 | +static int ad4170_update_scan_mode(struct iio_dev *indio_dev, |
| 1410 | + const unsigned long *active_scan_mask) |
| 1411 | +{ |
| 1412 | + struct ad4170_state *st = iio_priv(indio_dev); |
| 1413 | + unsigned int chan_index; |
| 1414 | + int ret; |
| 1415 | + |
| 1416 | + iio_for_each_active_channel(indio_dev, chan_index) { |
| 1417 | + ret = ad4170_set_channel_enable(st, chan_index, true); |
| 1418 | + if (ret) |
| 1419 | + return ret; |
| 1420 | + } |
| 1421 | + return 0; |
| 1422 | +} |
| 1423 | + |
1387 | 1424 | static const struct iio_info ad4170_info = {
|
1388 | 1425 | .read_raw = ad4170_read_raw,
|
1389 | 1426 | .read_avail = ad4170_read_avail,
|
1390 | 1427 | .write_raw = ad4170_write_raw,
|
1391 | 1428 | .write_raw_get_fmt = ad4170_write_raw_get_fmt,
|
| 1429 | + .update_scan_mode = ad4170_update_scan_mode, |
1392 | 1430 | .debugfs_reg_access = ad4170_debugfs_reg_access,
|
1393 | 1431 | };
|
1394 | 1432 |
|
@@ -1683,16 +1721,178 @@ static int ad4170_initial_config(struct iio_dev *indio_dev)
|
1683 | 1721 | AD4170_ADC_CTRL_MULTI_DATA_REG_SEL_MSK);
|
1684 | 1722 | }
|
1685 | 1723 |
|
| 1724 | +static int ad4170_prepare_spi_message(struct ad4170_state *st) |
| 1725 | +{ |
| 1726 | + /* |
| 1727 | + * Continuous data register read is enabled on buffer postenable so |
| 1728 | + * no instruction phase is needed meaning we don't need to send the |
| 1729 | + * register address to read data. Transfer only needs the read buffer. |
| 1730 | + */ |
| 1731 | + st->xfer.rx_buf = &st->rx_buf; |
| 1732 | + st->xfer.len = BITS_TO_BYTES(ad4170_channel_template.scan_type.realbits); |
| 1733 | + |
| 1734 | + spi_message_init_with_transfers(&st->msg, &st->xfer, 1); |
| 1735 | + |
| 1736 | + return devm_spi_optimize_message(&st->spi->dev, st->spi, &st->msg); |
| 1737 | +} |
| 1738 | + |
| 1739 | +static int ad4170_buffer_postenable(struct iio_dev *indio_dev) |
| 1740 | +{ |
| 1741 | + struct ad4170_state *st = iio_priv(indio_dev); |
| 1742 | + int ret; |
| 1743 | + |
| 1744 | + ret = regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG, |
| 1745 | + AD4170_ADC_CTRL_MODE_MSK, |
| 1746 | + FIELD_PREP(AD4170_ADC_CTRL_MODE_MSK, |
| 1747 | + AD4170_ADC_CTRL_MODE_CONT)); |
| 1748 | + if (ret) |
| 1749 | + return ret; |
| 1750 | + |
| 1751 | + /* |
| 1752 | + * This enables continuous read of the ADC data register. The ADC must |
| 1753 | + * be in continuous conversion mode. |
| 1754 | + */ |
| 1755 | + return regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG, |
| 1756 | + AD4170_ADC_CTRL_CONT_READ_MSK, |
| 1757 | + FIELD_PREP(AD4170_ADC_CTRL_CONT_READ_MSK, |
| 1758 | + AD4170_ADC_CTRL_CONT_READ_ENABLE)); |
| 1759 | +} |
| 1760 | + |
| 1761 | +static int ad4170_buffer_predisable(struct iio_dev *indio_dev) |
| 1762 | +{ |
| 1763 | + struct ad4170_state *st = iio_priv(indio_dev); |
| 1764 | + unsigned int i; |
| 1765 | + int ret; |
| 1766 | + |
| 1767 | + /* |
| 1768 | + * Use a high register address (virtual register) to request a write of |
| 1769 | + * 0xA5 to the ADC during the first 8 SCLKs of the ADC data read cycle, |
| 1770 | + * thus exiting continuous read. |
| 1771 | + */ |
| 1772 | + ret = regmap_write(st->regmap, AD4170_ADC_CTRL_CONT_READ_EXIT_REG, 0); |
| 1773 | + if (ret) |
| 1774 | + return ret; |
| 1775 | + |
| 1776 | + ret = regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG, |
| 1777 | + AD4170_ADC_CTRL_CONT_READ_MSK, |
| 1778 | + FIELD_PREP(AD4170_ADC_CTRL_CONT_READ_MSK, |
| 1779 | + AD4170_ADC_CTRL_CONT_READ_DISABLE)); |
| 1780 | + if (ret) |
| 1781 | + return ret; |
| 1782 | + |
| 1783 | + ret = regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG, |
| 1784 | + AD4170_ADC_CTRL_MODE_MSK, |
| 1785 | + FIELD_PREP(AD4170_ADC_CTRL_MODE_MSK, |
| 1786 | + AD4170_ADC_CTRL_MODE_IDLE)); |
| 1787 | + if (ret) |
| 1788 | + return ret; |
| 1789 | + |
| 1790 | + /* |
| 1791 | + * The ADC sequences through all the enabled channels (see datasheet |
| 1792 | + * page 95). That can lead to incorrect channel being read if a |
| 1793 | + * single-shot read (or buffered read with different active_scan_mask) |
| 1794 | + * is done after buffer disable. Disable all channels so only requested |
| 1795 | + * channels will be read. |
| 1796 | + */ |
| 1797 | + for (i = 0; i < indio_dev->num_channels; i++) { |
| 1798 | + ret = ad4170_set_channel_enable(st, i, false); |
| 1799 | + if (ret) |
| 1800 | + return ret; |
| 1801 | + } |
| 1802 | + |
| 1803 | + return 0; |
| 1804 | +} |
| 1805 | + |
| 1806 | +static bool ad4170_validate_scan_mask(struct iio_dev *indio_dev, |
| 1807 | + const unsigned long *scan_mask) |
| 1808 | +{ |
| 1809 | + unsigned int masklength = iio_get_masklength(indio_dev); |
| 1810 | + unsigned int enabled; |
| 1811 | + |
| 1812 | + /* |
| 1813 | + * The channel sequencer cycles through the enabled channels in |
| 1814 | + * sequential order, from channel 0 to channel 15, bypassing disabled |
| 1815 | + * channels. When more than one channel is enabled, channel 0 must |
| 1816 | + * always be enabled. See datasheet channel_en register description at |
| 1817 | + * page 95. |
| 1818 | + */ |
| 1819 | + enabled = bitmap_weight(scan_mask, masklength); |
| 1820 | + if (enabled > 1) |
| 1821 | + return test_bit(0, scan_mask); |
| 1822 | + |
| 1823 | + return enabled == 1; |
| 1824 | +} |
| 1825 | + |
| 1826 | +static const struct iio_buffer_setup_ops ad4170_buffer_ops = { |
| 1827 | + .postenable = ad4170_buffer_postenable, |
| 1828 | + .predisable = ad4170_buffer_predisable, |
| 1829 | + .validate_scan_mask = ad4170_validate_scan_mask, |
| 1830 | +}; |
| 1831 | + |
| 1832 | +static irqreturn_t ad4170_trigger_handler(int irq, void *p) |
| 1833 | +{ |
| 1834 | + struct iio_poll_func *pf = p; |
| 1835 | + struct iio_dev *indio_dev = pf->indio_dev; |
| 1836 | + struct ad4170_state *st = iio_priv(indio_dev); |
| 1837 | + unsigned int chan_index; |
| 1838 | + unsigned int i = 0; |
| 1839 | + int ret; |
| 1840 | + |
| 1841 | + iio_for_each_active_channel(indio_dev, chan_index) { |
| 1842 | + ret = spi_sync(st->spi, &st->msg); |
| 1843 | + if (ret) |
| 1844 | + goto err_out; |
| 1845 | + |
| 1846 | + memcpy(&st->bounce_buffer[i++], st->rx_buf, ARRAY_SIZE(st->rx_buf)); |
| 1847 | + } |
| 1848 | + |
| 1849 | + iio_push_to_buffers(indio_dev, st->bounce_buffer); |
| 1850 | +err_out: |
| 1851 | + iio_trigger_notify_done(indio_dev->trig); |
| 1852 | + return IRQ_HANDLED; |
| 1853 | +} |
| 1854 | + |
| 1855 | +static const struct iio_trigger_ops ad4170_trigger_ops = { |
| 1856 | + .validate_device = iio_trigger_validate_own_device, |
| 1857 | +}; |
| 1858 | + |
1686 | 1859 | static irqreturn_t ad4170_irq_handler(int irq, void *dev_id)
|
1687 | 1860 | {
|
1688 | 1861 | struct iio_dev *indio_dev = dev_id;
|
1689 | 1862 | struct ad4170_state *st = iio_priv(indio_dev);
|
1690 | 1863 |
|
1691 |
| - complete(&st->completion); |
| 1864 | + if (iio_buffer_enabled(indio_dev)) |
| 1865 | + iio_trigger_poll(st->trig); |
| 1866 | + else |
| 1867 | + complete(&st->completion); |
1692 | 1868 |
|
1693 | 1869 | return IRQ_HANDLED;
|
1694 | 1870 | };
|
1695 | 1871 |
|
| 1872 | +static int ad4170_trigger_setup(struct iio_dev *indio_dev) |
| 1873 | +{ |
| 1874 | + struct ad4170_state *st = iio_priv(indio_dev); |
| 1875 | + struct device *dev = &st->spi->dev; |
| 1876 | + int ret; |
| 1877 | + |
| 1878 | + st->trig = devm_iio_trigger_alloc(dev, "%s-trig%d", |
| 1879 | + indio_dev->name, |
| 1880 | + iio_device_id(indio_dev)); |
| 1881 | + if (!st->trig) |
| 1882 | + return -ENOMEM; |
| 1883 | + |
| 1884 | + st->trig->ops = &ad4170_trigger_ops; |
| 1885 | + |
| 1886 | + iio_trigger_set_drvdata(st->trig, indio_dev); |
| 1887 | + ret = devm_iio_trigger_register(dev, st->trig); |
| 1888 | + if (ret) |
| 1889 | + return dev_err_probe(dev, ret, "Failed to register trigger\n"); |
| 1890 | + |
| 1891 | + indio_dev->trig = iio_trigger_get(st->trig); |
| 1892 | + |
| 1893 | + return 0; |
| 1894 | +} |
| 1895 | + |
1696 | 1896 | static int ad4170_regulator_setup(struct ad4170_state *st)
|
1697 | 1897 | {
|
1698 | 1898 | struct device *dev = &st->spi->dev;
|
@@ -1817,8 +2017,22 @@ static int ad4170_probe(struct spi_device *spi)
|
1817 | 2017 | IRQF_ONESHOT, indio_dev->name, indio_dev);
|
1818 | 2018 | if (ret)
|
1819 | 2019 | return ret;
|
| 2020 | + |
| 2021 | + ret = ad4170_trigger_setup(indio_dev); |
| 2022 | + if (ret) |
| 2023 | + return ret; |
1820 | 2024 | }
|
1821 | 2025 |
|
| 2026 | + ret = ad4170_prepare_spi_message(st); |
| 2027 | + if (ret) |
| 2028 | + return dev_err_probe(dev, ret, "Failed to prepare SPI message\n"); |
| 2029 | + |
| 2030 | + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, |
| 2031 | + &ad4170_trigger_handler, |
| 2032 | + &ad4170_buffer_ops); |
| 2033 | + if (ret) |
| 2034 | + return dev_err_probe(dev, ret, "Failed to setup read buffer\n"); |
| 2035 | + |
1822 | 2036 | return devm_iio_device_register(dev, indio_dev);
|
1823 | 2037 | }
|
1824 | 2038 |
|
|
0 commit comments