Stefan Popa | ca5b463 | 2018-12-17 14:23:36 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 2 | /* |
| 3 | * AD7606 ADC driver |
| 4 | * |
| 5 | * Copyright 2011 Analog Devices Inc. |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef IIO_ADC_AD7606_H_ |
| 9 | #define IIO_ADC_AD7606_H_ |
| 10 | |
Beniamin Bia | f2a22e1 | 2019-07-18 09:27:33 +0300 | [diff] [blame^] | 11 | #define AD760X_CHANNEL(num, mask_sep, mask_type, mask_all) { \ |
Beniamin Bia | 7677f73 | 2019-07-18 09:27:30 +0300 | [diff] [blame] | 12 | .type = IIO_VOLTAGE, \ |
| 13 | .indexed = 1, \ |
| 14 | .channel = num, \ |
| 15 | .address = num, \ |
Beniamin Bia | f2a22e1 | 2019-07-18 09:27:33 +0300 | [diff] [blame^] | 16 | .info_mask_separate = mask_sep, \ |
| 17 | .info_mask_shared_by_type = mask_type, \ |
| 18 | .info_mask_shared_by_all = mask_all, \ |
Beniamin Bia | 7677f73 | 2019-07-18 09:27:30 +0300 | [diff] [blame] | 19 | .scan_index = num, \ |
| 20 | .scan_type = { \ |
| 21 | .sign = 's', \ |
| 22 | .realbits = 16, \ |
| 23 | .storagebits = 16, \ |
| 24 | .endianness = IIO_CPU, \ |
| 25 | }, \ |
| 26 | } |
| 27 | |
Beniamin Bia | f2a22e1 | 2019-07-18 09:27:33 +0300 | [diff] [blame^] | 28 | #define AD7605_CHANNEL(num) \ |
| 29 | AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_RAW), \ |
| 30 | BIT(IIO_CHAN_INFO_SCALE), 0) |
Beniamin Bia | 7677f73 | 2019-07-18 09:27:30 +0300 | [diff] [blame] | 31 | |
Beniamin Bia | f2a22e1 | 2019-07-18 09:27:33 +0300 | [diff] [blame^] | 32 | #define AD7606_CHANNEL(num) \ |
| 33 | AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_RAW), \ |
| 34 | BIT(IIO_CHAN_INFO_SCALE), \ |
| 35 | BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO)) |
| 36 | |
| 37 | #define AD7616_CHANNEL(num) \ |
| 38 | AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),\ |
| 39 | 0, BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO)) |
Beniamin Bia | 7677f73 | 2019-07-18 09:27:30 +0300 | [diff] [blame] | 40 | |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 41 | /** |
Masanari Iida | e3c5be2 | 2014-04-22 12:23:00 +0100 | [diff] [blame] | 42 | * struct ad7606_chip_info - chip specific information |
Michael Hennerich | 1caf7cb | 2011-05-18 14:42:00 +0100 | [diff] [blame] | 43 | * @channels: channel specification |
| 44 | * @num_channels: number of channels |
Stefan Popa | 6bf229a | 2019-04-02 16:18:39 +0300 | [diff] [blame] | 45 | * @oversampling_avail pointer to the array which stores the available |
| 46 | * oversampling ratios. |
| 47 | * @oversampling_num number of elements stored in oversampling_avail array |
Beniamin Bia | 7989b4b | 2019-04-02 16:18:40 +0300 | [diff] [blame] | 48 | * @os_req_reset some devices require a reset to update oversampling |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 49 | */ |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 50 | struct ad7606_chip_info { |
Lars-Peter Clausen | f4e4b95 | 2012-08-09 08:51:00 +0100 | [diff] [blame] | 51 | const struct iio_chan_spec *channels; |
Alison Schofield | 51fadb9 | 2016-03-26 12:50:24 -0700 | [diff] [blame] | 52 | unsigned int num_channels; |
Stefan Popa | 6bf229a | 2019-04-02 16:18:39 +0300 | [diff] [blame] | 53 | const unsigned int *oversampling_avail; |
| 54 | unsigned int oversampling_num; |
Beniamin Bia | 7989b4b | 2019-04-02 16:18:40 +0300 | [diff] [blame] | 55 | bool os_req_reset; |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | /** |
| 59 | * struct ad7606_state - driver instance specific data |
Alexandru Ardelean | 7c2d537 | 2018-09-18 15:15:03 +0300 | [diff] [blame] | 60 | * @dev pointer to kernel device |
| 61 | * @chip_info entry in the table of chips that describes this device |
| 62 | * @reg regulator info for the the power supply of the device |
Alexandru Ardelean | 7c2d537 | 2018-09-18 15:15:03 +0300 | [diff] [blame] | 63 | * @bops bus operations (SPI or parallel) |
| 64 | * @range voltage range selection, selects which scale to apply |
| 65 | * @oversampling oversampling selection |
Alexandru Ardelean | 7c2d537 | 2018-09-18 15:15:03 +0300 | [diff] [blame] | 66 | * @base_address address from where to read data in parallel operation |
Beniamin Bia | 3c23e9e | 2019-05-27 15:56:48 +0300 | [diff] [blame] | 67 | * @sw_mode_en software mode enabled |
Stefan Popa | 6bf229a | 2019-04-02 16:18:39 +0300 | [diff] [blame] | 68 | * @scale_avail pointer to the array which stores the available scales |
| 69 | * @num_scales number of elements stored in the scale_avail array |
| 70 | * @oversampling_avail pointer to the array which stores the available |
| 71 | * oversampling ratios. |
| 72 | * @num_os_ratios number of elements stored in oversampling_avail array |
Beniamin Bia | 88dd031 | 2019-05-27 15:56:47 +0300 | [diff] [blame] | 73 | * @write_scale pointer to the function which writes the scale |
| 74 | * @write_os pointer to the function which writes the os |
Alexandru Ardelean | 7c2d537 | 2018-09-18 15:15:03 +0300 | [diff] [blame] | 75 | * @lock protect sensor state from concurrent accesses to GPIOs |
| 76 | * @gpio_convst GPIO descriptor for conversion start signal (CONVST) |
| 77 | * @gpio_reset GPIO descriptor for device hard-reset |
| 78 | * @gpio_range GPIO descriptor for range selection |
| 79 | * @gpio_standby GPIO descriptor for stand-by signal (STBY), |
| 80 | * controls power-down mode of device |
| 81 | * @gpio_frstdata GPIO descriptor for reading from device when data |
| 82 | * is being read on the first channel |
| 83 | * @gpio_os GPIO descriptors to control oversampling on the device |
Stefan Popa | 557e585 | 2018-12-13 14:46:15 +0200 | [diff] [blame] | 84 | * @complete completion to indicate end of conversion |
Stefan Popa | cc49bd1 | 2018-12-17 14:23:37 +0200 | [diff] [blame] | 85 | * @trig The IIO trigger associated with the device. |
Alexandru Ardelean | 7c2d537 | 2018-09-18 15:15:03 +0300 | [diff] [blame] | 86 | * @data buffer for reading data from the device |
Beniamin Bia | f2a22e1 | 2019-07-18 09:27:33 +0300 | [diff] [blame^] | 87 | * @d16 be16 buffer for reading data from the device |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 88 | */ |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 89 | struct ad7606_state { |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 90 | struct device *dev; |
| 91 | const struct ad7606_chip_info *chip_info; |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 92 | struct regulator *reg; |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 93 | const struct ad7606_bus_ops *bops; |
Beniamin Bia | 88dd031 | 2019-05-27 15:56:47 +0300 | [diff] [blame] | 94 | unsigned int range[16]; |
Alison Schofield | 51fadb9 | 2016-03-26 12:50:24 -0700 | [diff] [blame] | 95 | unsigned int oversampling; |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 96 | void __iomem *base_address; |
Beniamin Bia | 3c23e9e | 2019-05-27 15:56:48 +0300 | [diff] [blame] | 97 | bool sw_mode_en; |
Stefan Popa | 6bf229a | 2019-04-02 16:18:39 +0300 | [diff] [blame] | 98 | const unsigned int *scale_avail; |
| 99 | unsigned int num_scales; |
| 100 | const unsigned int *oversampling_avail; |
| 101 | unsigned int num_os_ratios; |
Beniamin Bia | 88dd031 | 2019-05-27 15:56:47 +0300 | [diff] [blame] | 102 | int (*write_scale)(struct iio_dev *indio_dev, int ch, int val); |
| 103 | int (*write_os)(struct iio_dev *indio_dev, int val); |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 104 | |
Arushi Singhal | 755d0da | 2017-03-21 01:21:34 +0530 | [diff] [blame] | 105 | struct mutex lock; /* protect sensor state */ |
Lars-Peter Clausen | 722407a | 2016-10-19 19:07:07 +0200 | [diff] [blame] | 106 | struct gpio_desc *gpio_convst; |
| 107 | struct gpio_desc *gpio_reset; |
| 108 | struct gpio_desc *gpio_range; |
| 109 | struct gpio_desc *gpio_standby; |
| 110 | struct gpio_desc *gpio_frstdata; |
| 111 | struct gpio_descs *gpio_os; |
Stefan Popa | cc49bd1 | 2018-12-17 14:23:37 +0200 | [diff] [blame] | 112 | struct iio_trigger *trig; |
Stefan Popa | 557e585 | 2018-12-13 14:46:15 +0200 | [diff] [blame] | 113 | struct completion completion; |
Lars-Peter Clausen | 722407a | 2016-10-19 19:07:07 +0200 | [diff] [blame] | 114 | |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 115 | /* |
| 116 | * DMA (thus cache coherency maintenance) requires the |
| 117 | * transfer buffers to live in their own cache lines. |
Beniamin Bia | 7989b4b | 2019-04-02 16:18:40 +0300 | [diff] [blame] | 118 | * 16 * 16-bit samples + 64-bit timestamp |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 119 | */ |
Beniamin Bia | 7989b4b | 2019-04-02 16:18:40 +0300 | [diff] [blame] | 120 | unsigned short data[20] ____cacheline_aligned; |
Beniamin Bia | f2a22e1 | 2019-07-18 09:27:33 +0300 | [diff] [blame^] | 121 | __be16 d16[2]; |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 122 | }; |
| 123 | |
Alexandru Ardelean | 7c2d537 | 2018-09-18 15:15:03 +0300 | [diff] [blame] | 124 | /** |
| 125 | * struct ad7606_bus_ops - driver bus operations |
| 126 | * @read_block function pointer for reading blocks of data |
Beniamin Bia | a0c648c | 2019-07-18 09:27:31 +0300 | [diff] [blame] | 127 | * @sw_mode_config: pointer to a function which configured the device |
| 128 | * for software mode |
Beniamin Bia | f2a22e1 | 2019-07-18 09:27:33 +0300 | [diff] [blame^] | 129 | * @reg_read function pointer for reading spi register |
| 130 | * @reg_write function pointer for writing spi register |
| 131 | * @write_mask function pointer for write spi register with mask |
| 132 | * @rd_wr_cmd pointer to the function which calculates the spi address |
Alexandru Ardelean | 7c2d537 | 2018-09-18 15:15:03 +0300 | [diff] [blame] | 133 | */ |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 134 | struct ad7606_bus_ops { |
| 135 | /* more methods added in future? */ |
Giulio Benetti | 727198f | 2018-06-09 00:13:31 +0200 | [diff] [blame] | 136 | int (*read_block)(struct device *dev, int num, void *data); |
Beniamin Bia | a0c648c | 2019-07-18 09:27:31 +0300 | [diff] [blame] | 137 | int (*sw_mode_config)(struct iio_dev *indio_dev); |
Beniamin Bia | f2a22e1 | 2019-07-18 09:27:33 +0300 | [diff] [blame^] | 138 | int (*reg_read)(struct ad7606_state *st, unsigned int addr); |
| 139 | int (*reg_write)(struct ad7606_state *st, |
| 140 | unsigned int addr, |
| 141 | unsigned int val); |
| 142 | int (*write_mask)(struct ad7606_state *st, |
| 143 | unsigned int addr, |
| 144 | unsigned long mask, |
| 145 | unsigned int val); |
| 146 | u16 (*rd_wr_cmd)(int addr, char isWriteOp); |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 147 | }; |
| 148 | |
Lars-Peter Clausen | 71faca7 | 2016-10-19 19:07:04 +0200 | [diff] [blame] | 149 | int ad7606_probe(struct device *dev, int irq, void __iomem *base_address, |
| 150 | const char *name, unsigned int id, |
| 151 | const struct ad7606_bus_ops *bops); |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 152 | |
| 153 | enum ad7606_supported_device_ids { |
Alexandru Ardelean | bb9fc6a | 2018-09-13 14:02:12 +0300 | [diff] [blame] | 154 | ID_AD7605_4, |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 155 | ID_AD7606_8, |
| 156 | ID_AD7606_6, |
Beniamin Bia | 7989b4b | 2019-04-02 16:18:40 +0300 | [diff] [blame] | 157 | ID_AD7606_4, |
| 158 | ID_AD7616, |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 159 | }; |
| 160 | |
Lars-Peter Clausen | 2c3a523 | 2016-02-08 11:13:29 +0100 | [diff] [blame] | 161 | #ifdef CONFIG_PM_SLEEP |
| 162 | extern const struct dev_pm_ops ad7606_pm_ops; |
| 163 | #define AD7606_PM_OPS (&ad7606_pm_ops) |
| 164 | #else |
| 165 | #define AD7606_PM_OPS NULL |
| 166 | #endif |
| 167 | |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 168 | #endif /* IIO_ADC_AD7606_H_ */ |