blob: 4f82d7c9acfdebf8a79a368a8b180cee50d38b12 [file] [log] [blame]
Stefan Popaca5b4632018-12-17 14:23:36 +02001/* SPDX-License-Identifier: GPL-2.0 */
Michael Hennerichb9618c02011-02-22 21:46:18 +01002/*
3 * AD7606 ADC driver
4 *
5 * Copyright 2011 Analog Devices Inc.
Michael Hennerichb9618c02011-02-22 21:46:18 +01006 */
7
8#ifndef IIO_ADC_AD7606_H_
9#define IIO_ADC_AD7606_H_
10
Beniamin Biaf2a22e12019-07-18 09:27:33 +030011#define AD760X_CHANNEL(num, mask_sep, mask_type, mask_all) { \
Beniamin Bia7677f732019-07-18 09:27:30 +030012 .type = IIO_VOLTAGE, \
13 .indexed = 1, \
14 .channel = num, \
15 .address = num, \
Beniamin Biaf2a22e12019-07-18 09:27:33 +030016 .info_mask_separate = mask_sep, \
17 .info_mask_shared_by_type = mask_type, \
18 .info_mask_shared_by_all = mask_all, \
Beniamin Bia7677f732019-07-18 09:27:30 +030019 .scan_index = num, \
20 .scan_type = { \
21 .sign = 's', \
22 .realbits = 16, \
23 .storagebits = 16, \
24 .endianness = IIO_CPU, \
25 }, \
26}
27
Beniamin Biaf2a22e12019-07-18 09:27:33 +030028#define AD7605_CHANNEL(num) \
29 AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_RAW), \
30 BIT(IIO_CHAN_INFO_SCALE), 0)
Beniamin Bia7677f732019-07-18 09:27:30 +030031
Beniamin Biaf2a22e12019-07-18 09:27:33 +030032#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 Bia7677f732019-07-18 09:27:30 +030040
Michael Hennerichb9618c02011-02-22 21:46:18 +010041/**
Masanari Iidae3c5be22014-04-22 12:23:00 +010042 * struct ad7606_chip_info - chip specific information
Michael Hennerich1caf7cb2011-05-18 14:42:00 +010043 * @channels: channel specification
44 * @num_channels: number of channels
Stefan Popa6bf229a2019-04-02 16:18:39 +030045 * @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 Bia7989b4b2019-04-02 16:18:40 +030048 * @os_req_reset some devices require a reset to update oversampling
Stefan Popad2a415c2019-08-21 17:16:53 +030049 * @init_delay_ms required delay in miliseconds for initialization
50 * after a restart
Michael Hennerichb9618c02011-02-22 21:46:18 +010051 */
Michael Hennerichb9618c02011-02-22 21:46:18 +010052struct ad7606_chip_info {
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +010053 const struct iio_chan_spec *channels;
Alison Schofield51fadb92016-03-26 12:50:24 -070054 unsigned int num_channels;
Stefan Popa6bf229a2019-04-02 16:18:39 +030055 const unsigned int *oversampling_avail;
56 unsigned int oversampling_num;
Beniamin Bia7989b4b2019-04-02 16:18:40 +030057 bool os_req_reset;
Stefan Popad2a415c2019-08-21 17:16:53 +030058 unsigned long init_delay_ms;
Michael Hennerichb9618c02011-02-22 21:46:18 +010059};
60
61/**
62 * struct ad7606_state - driver instance specific data
Alexandru Ardelean7c2d5372018-09-18 15:15:03 +030063 * @dev pointer to kernel device
64 * @chip_info entry in the table of chips that describes this device
Xiang wangx0a52c3f2021-12-12 22:41:18 +080065 * @reg regulator info for the power supply of the device
Alexandru Ardelean7c2d5372018-09-18 15:15:03 +030066 * @bops bus operations (SPI or parallel)
67 * @range voltage range selection, selects which scale to apply
68 * @oversampling oversampling selection
Alexandru Ardelean7c2d5372018-09-18 15:15:03 +030069 * @base_address address from where to read data in parallel operation
Beniamin Bia3c23e9e2019-05-27 15:56:48 +030070 * @sw_mode_en software mode enabled
Stefan Popa6bf229a2019-04-02 16:18:39 +030071 * @scale_avail pointer to the array which stores the available scales
72 * @num_scales number of elements stored in the scale_avail array
73 * @oversampling_avail pointer to the array which stores the available
74 * oversampling ratios.
75 * @num_os_ratios number of elements stored in oversampling_avail array
Beniamin Bia88dd0312019-05-27 15:56:47 +030076 * @write_scale pointer to the function which writes the scale
77 * @write_os pointer to the function which writes the os
Alexandru Ardelean7c2d5372018-09-18 15:15:03 +030078 * @lock protect sensor state from concurrent accesses to GPIOs
79 * @gpio_convst GPIO descriptor for conversion start signal (CONVST)
80 * @gpio_reset GPIO descriptor for device hard-reset
81 * @gpio_range GPIO descriptor for range selection
82 * @gpio_standby GPIO descriptor for stand-by signal (STBY),
83 * controls power-down mode of device
84 * @gpio_frstdata GPIO descriptor for reading from device when data
85 * is being read on the first channel
86 * @gpio_os GPIO descriptors to control oversampling on the device
Stefan Popa557e5852018-12-13 14:46:15 +020087 * @complete completion to indicate end of conversion
Stefan Popacc49bd12018-12-17 14:23:37 +020088 * @trig The IIO trigger associated with the device.
Alexandru Ardelean7c2d5372018-09-18 15:15:03 +030089 * @data buffer for reading data from the device
Beniamin Biaf2a22e12019-07-18 09:27:33 +030090 * @d16 be16 buffer for reading data from the device
Michael Hennerichb9618c02011-02-22 21:46:18 +010091 */
Michael Hennerichb9618c02011-02-22 21:46:18 +010092struct ad7606_state {
Michael Hennerichb9618c02011-02-22 21:46:18 +010093 struct device *dev;
94 const struct ad7606_chip_info *chip_info;
Michael Hennerichb9618c02011-02-22 21:46:18 +010095 struct regulator *reg;
Michael Hennerichb9618c02011-02-22 21:46:18 +010096 const struct ad7606_bus_ops *bops;
Beniamin Bia88dd0312019-05-27 15:56:47 +030097 unsigned int range[16];
Alison Schofield51fadb92016-03-26 12:50:24 -070098 unsigned int oversampling;
Michael Hennerichb9618c02011-02-22 21:46:18 +010099 void __iomem *base_address;
Beniamin Bia3c23e9e2019-05-27 15:56:48 +0300100 bool sw_mode_en;
Stefan Popa6bf229a2019-04-02 16:18:39 +0300101 const unsigned int *scale_avail;
102 unsigned int num_scales;
103 const unsigned int *oversampling_avail;
104 unsigned int num_os_ratios;
Beniamin Bia88dd0312019-05-27 15:56:47 +0300105 int (*write_scale)(struct iio_dev *indio_dev, int ch, int val);
106 int (*write_os)(struct iio_dev *indio_dev, int val);
Michael Hennerichb9618c02011-02-22 21:46:18 +0100107
Arushi Singhal755d0da2017-03-21 01:21:34 +0530108 struct mutex lock; /* protect sensor state */
Lars-Peter Clausen722407a2016-10-19 19:07:07 +0200109 struct gpio_desc *gpio_convst;
110 struct gpio_desc *gpio_reset;
111 struct gpio_desc *gpio_range;
112 struct gpio_desc *gpio_standby;
113 struct gpio_desc *gpio_frstdata;
114 struct gpio_descs *gpio_os;
Stefan Popacc49bd12018-12-17 14:23:37 +0200115 struct iio_trigger *trig;
Stefan Popa557e5852018-12-13 14:46:15 +0200116 struct completion completion;
Lars-Peter Clausen722407a2016-10-19 19:07:07 +0200117
Michael Hennerichb9618c02011-02-22 21:46:18 +0100118 /*
119 * DMA (thus cache coherency maintenance) requires the
120 * transfer buffers to live in their own cache lines.
Beniamin Bia7989b4b2019-04-02 16:18:40 +0300121 * 16 * 16-bit samples + 64-bit timestamp
Michael Hennerichb9618c02011-02-22 21:46:18 +0100122 */
Beniamin Bia7989b4b2019-04-02 16:18:40 +0300123 unsigned short data[20] ____cacheline_aligned;
Beniamin Biaf2a22e12019-07-18 09:27:33 +0300124 __be16 d16[2];
Michael Hennerichb9618c02011-02-22 21:46:18 +0100125};
126
Alexandru Ardelean7c2d5372018-09-18 15:15:03 +0300127/**
128 * struct ad7606_bus_ops - driver bus operations
129 * @read_block function pointer for reading blocks of data
Beniamin Biaa0c648c2019-07-18 09:27:31 +0300130 * @sw_mode_config: pointer to a function which configured the device
131 * for software mode
Beniamin Biaf2a22e12019-07-18 09:27:33 +0300132 * @reg_read function pointer for reading spi register
133 * @reg_write function pointer for writing spi register
134 * @write_mask function pointer for write spi register with mask
135 * @rd_wr_cmd pointer to the function which calculates the spi address
Alexandru Ardelean7c2d5372018-09-18 15:15:03 +0300136 */
Michael Hennerichb9618c02011-02-22 21:46:18 +0100137struct ad7606_bus_ops {
138 /* more methods added in future? */
Giulio Benetti727198f2018-06-09 00:13:31 +0200139 int (*read_block)(struct device *dev, int num, void *data);
Beniamin Biaa0c648c2019-07-18 09:27:31 +0300140 int (*sw_mode_config)(struct iio_dev *indio_dev);
Beniamin Biaf2a22e12019-07-18 09:27:33 +0300141 int (*reg_read)(struct ad7606_state *st, unsigned int addr);
142 int (*reg_write)(struct ad7606_state *st,
143 unsigned int addr,
144 unsigned int val);
145 int (*write_mask)(struct ad7606_state *st,
146 unsigned int addr,
147 unsigned long mask,
148 unsigned int val);
149 u16 (*rd_wr_cmd)(int addr, char isWriteOp);
Michael Hennerichb9618c02011-02-22 21:46:18 +0100150};
151
Lars-Peter Clausen71faca72016-10-19 19:07:04 +0200152int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
153 const char *name, unsigned int id,
154 const struct ad7606_bus_ops *bops);
Michael Hennerichb9618c02011-02-22 21:46:18 +0100155
156enum ad7606_supported_device_ids {
Alexandru Ardeleanbb9fc6a2018-09-13 14:02:12 +0300157 ID_AD7605_4,
Michael Hennerichb9618c02011-02-22 21:46:18 +0100158 ID_AD7606_8,
159 ID_AD7606_6,
Beniamin Bia7989b4b2019-04-02 16:18:40 +0300160 ID_AD7606_4,
Stefan Popad2a415c2019-08-21 17:16:53 +0300161 ID_AD7606B,
Beniamin Bia7989b4b2019-04-02 16:18:40 +0300162 ID_AD7616,
Michael Hennerichb9618c02011-02-22 21:46:18 +0100163};
164
Lars-Peter Clausen2c3a5232016-02-08 11:13:29 +0100165#ifdef CONFIG_PM_SLEEP
166extern const struct dev_pm_ops ad7606_pm_ops;
167#define AD7606_PM_OPS (&ad7606_pm_ops)
168#else
169#define AD7606_PM_OPS NULL
170#endif
171
Michael Hennerichb9618c02011-02-22 21:46:18 +0100172#endif /* IIO_ADC_AD7606_H_ */