Stefan Popa | cbd5dd3 | 2019-02-04 11:19:35 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 2 | /* |
| 3 | * This file is part of AD5686 DAC driver |
| 4 | * |
| 5 | * Copyright 2018 Analog Devices Inc. |
| 6 | */ |
| 7 | |
| 8 | #ifndef __DRIVERS_IIO_DAC_AD5686_H__ |
| 9 | #define __DRIVERS_IIO_DAC_AD5686_H__ |
| 10 | |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/cache.h> |
| 13 | #include <linux/mutex.h> |
| 14 | #include <linux/kernel.h> |
| 15 | |
Stefan Popa | 12d323c | 2018-12-06 15:38:30 +0200 | [diff] [blame] | 16 | #define AD5310_CMD(x) ((x) << 12) |
| 17 | |
Stefan Popa | 1dbae4c | 2018-05-18 18:23:34 +0300 | [diff] [blame] | 18 | #define AD5683_DATA(x) ((x) << 4) |
Stefan Popa | 12d323c | 2018-12-06 15:38:30 +0200 | [diff] [blame] | 19 | |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 20 | #define AD5686_ADDR(x) ((x) << 16) |
| 21 | #define AD5686_CMD(x) ((x) << 20) |
| 22 | |
| 23 | #define AD5686_ADDR_DAC(chan) (0x1 << (chan)) |
| 24 | #define AD5686_ADDR_ALL_DAC 0xF |
| 25 | |
| 26 | #define AD5686_CMD_NOOP 0x0 |
| 27 | #define AD5686_CMD_WRITE_INPUT_N 0x1 |
| 28 | #define AD5686_CMD_UPDATE_DAC_N 0x2 |
| 29 | #define AD5686_CMD_WRITE_INPUT_N_UPDATE_N 0x3 |
| 30 | #define AD5686_CMD_POWERDOWN_DAC 0x4 |
| 31 | #define AD5686_CMD_LDAC_MASK 0x5 |
| 32 | #define AD5686_CMD_RESET 0x6 |
| 33 | #define AD5686_CMD_INTERNAL_REFER_SETUP 0x7 |
| 34 | #define AD5686_CMD_DAISY_CHAIN_ENABLE 0x8 |
| 35 | #define AD5686_CMD_READBACK_ENABLE 0x9 |
| 36 | |
| 37 | #define AD5686_LDAC_PWRDN_NONE 0x0 |
| 38 | #define AD5686_LDAC_PWRDN_1K 0x1 |
| 39 | #define AD5686_LDAC_PWRDN_100K 0x2 |
| 40 | #define AD5686_LDAC_PWRDN_3STATE 0x3 |
| 41 | |
Stefan Popa | be1b24d | 2018-05-18 18:22:50 +0300 | [diff] [blame] | 42 | #define AD5686_CMD_CONTROL_REG 0x4 |
Stefan Popa | 1dbae4c | 2018-05-18 18:23:34 +0300 | [diff] [blame] | 43 | #define AD5686_CMD_READBACK_ENABLE_V2 0x5 |
Stefan Popa | 12d323c | 2018-12-06 15:38:30 +0200 | [diff] [blame] | 44 | |
| 45 | #define AD5310_REF_BIT_MSK BIT(8) |
Stefan Popa | 1dbae4c | 2018-05-18 18:23:34 +0300 | [diff] [blame] | 46 | #define AD5683_REF_BIT_MSK BIT(12) |
Stefan Popa | be1b24d | 2018-05-18 18:22:50 +0300 | [diff] [blame] | 47 | #define AD5693_REF_BIT_MSK BIT(12) |
| 48 | |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 49 | /** |
| 50 | * ad5686_supported_device_ids: |
| 51 | */ |
| 52 | enum ad5686_supported_device_ids { |
Stefan Popa | 12d323c | 2018-12-06 15:38:30 +0200 | [diff] [blame] | 53 | ID_AD5310R, |
Stefan Popa | d8084a0 | 2018-06-04 17:22:07 +0300 | [diff] [blame] | 54 | ID_AD5311R, |
Michael Auchter | 7dc8fae | 2020-09-24 14:52:12 -0500 | [diff] [blame] | 55 | ID_AD5338R, |
Stefan Popa | 4177381 | 2018-04-11 14:53:39 +0300 | [diff] [blame] | 56 | ID_AD5671R, |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 57 | ID_AD5672R, |
Mircea Caprioru | 477bd01 | 2021-02-17 09:41:02 +0200 | [diff] [blame] | 58 | ID_AD5673R, |
Mircea Caprioru | 192778f | 2019-01-09 11:14:16 +0200 | [diff] [blame] | 59 | ID_AD5674R, |
Stefan Popa | 4177381 | 2018-04-11 14:53:39 +0300 | [diff] [blame] | 60 | ID_AD5675R, |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 61 | ID_AD5676, |
| 62 | ID_AD5676R, |
Mircea Caprioru | 477bd01 | 2021-02-17 09:41:02 +0200 | [diff] [blame] | 63 | ID_AD5677R, |
Mircea Caprioru | 192778f | 2019-01-09 11:14:16 +0200 | [diff] [blame] | 64 | ID_AD5679R, |
Stefan Popa | 1dbae4c | 2018-05-18 18:23:34 +0300 | [diff] [blame] | 65 | ID_AD5681R, |
| 66 | ID_AD5682R, |
| 67 | ID_AD5683, |
| 68 | ID_AD5683R, |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 69 | ID_AD5684, |
| 70 | ID_AD5684R, |
| 71 | ID_AD5685R, |
| 72 | ID_AD5686, |
| 73 | ID_AD5686R, |
Stefan Popa | be1b24d | 2018-05-18 18:22:50 +0300 | [diff] [blame] | 74 | ID_AD5691R, |
| 75 | ID_AD5692R, |
| 76 | ID_AD5693, |
| 77 | ID_AD5693R, |
Stefan Popa | 4177381 | 2018-04-11 14:53:39 +0300 | [diff] [blame] | 78 | ID_AD5694, |
| 79 | ID_AD5694R, |
| 80 | ID_AD5695R, |
| 81 | ID_AD5696, |
| 82 | ID_AD5696R, |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 83 | }; |
| 84 | |
Stefan Popa | be1b24d | 2018-05-18 18:22:50 +0300 | [diff] [blame] | 85 | enum ad5686_regmap_type { |
Stefan Popa | 12d323c | 2018-12-06 15:38:30 +0200 | [diff] [blame] | 86 | AD5310_REGMAP, |
Stefan Popa | 1dbae4c | 2018-05-18 18:23:34 +0300 | [diff] [blame] | 87 | AD5683_REGMAP, |
Stefan Popa | be1b24d | 2018-05-18 18:22:50 +0300 | [diff] [blame] | 88 | AD5686_REGMAP, |
| 89 | AD5693_REGMAP |
| 90 | }; |
| 91 | |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 92 | struct ad5686_state; |
| 93 | |
| 94 | typedef int (*ad5686_write_func)(struct ad5686_state *st, |
| 95 | u8 cmd, u8 addr, u16 val); |
| 96 | |
| 97 | typedef int (*ad5686_read_func)(struct ad5686_state *st, u8 addr); |
| 98 | |
| 99 | /** |
| 100 | * struct ad5686_chip_info - chip specific information |
| 101 | * @int_vref_mv: AD5620/40/60: the internal reference voltage |
| 102 | * @num_channels: number of channels |
| 103 | * @channel: channel specification |
Stefan Popa | be1b24d | 2018-05-18 18:22:50 +0300 | [diff] [blame] | 104 | * @regmap_type: register map layout variant |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 105 | */ |
| 106 | |
| 107 | struct ad5686_chip_info { |
| 108 | u16 int_vref_mv; |
| 109 | unsigned int num_channels; |
Rikard Falkeborn | 5971349 | 2020-05-26 23:02:21 +0200 | [diff] [blame] | 110 | const struct iio_chan_spec *channels; |
Stefan Popa | be1b24d | 2018-05-18 18:22:50 +0300 | [diff] [blame] | 111 | enum ad5686_regmap_type regmap_type; |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | /** |
| 115 | * struct ad5446_state - driver instance specific data |
| 116 | * @spi: spi_device |
| 117 | * @chip_info: chip model specific constants, available modes etc |
| 118 | * @reg: supply regulator |
| 119 | * @vref_mv: actual reference voltage used |
| 120 | * @pwr_down_mask: power down mask |
| 121 | * @pwr_down_mode: current power down mode |
Stefan Popa | be1b24d | 2018-05-18 18:22:50 +0300 | [diff] [blame] | 122 | * @use_internal_vref: set to true if the internal reference voltage is used |
Sergiu Cuciurean | 0b2884e | 2020-05-14 12:19:38 +0300 | [diff] [blame] | 123 | * @lock lock to protect the data buffer during regmap ops |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 124 | * @data: spi transfer buffers |
| 125 | */ |
| 126 | |
| 127 | struct ad5686_state { |
| 128 | struct device *dev; |
| 129 | const struct ad5686_chip_info *chip_info; |
| 130 | struct regulator *reg; |
| 131 | unsigned short vref_mv; |
| 132 | unsigned int pwr_down_mask; |
| 133 | unsigned int pwr_down_mode; |
| 134 | ad5686_write_func write; |
| 135 | ad5686_read_func read; |
Stefan Popa | be1b24d | 2018-05-18 18:22:50 +0300 | [diff] [blame] | 136 | bool use_internal_vref; |
Sergiu Cuciurean | 0b2884e | 2020-05-14 12:19:38 +0300 | [diff] [blame] | 137 | struct mutex lock; |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 138 | |
| 139 | /* |
| 140 | * DMA (thus cache coherency maintenance) requires the |
| 141 | * transfer buffers to live in their own cache lines. |
| 142 | */ |
| 143 | |
| 144 | union { |
| 145 | __be32 d32; |
| 146 | __be16 d16; |
| 147 | u8 d8[4]; |
| 148 | } data[3] ____cacheline_aligned; |
| 149 | }; |
| 150 | |
| 151 | |
| 152 | int ad5686_probe(struct device *dev, |
| 153 | enum ad5686_supported_device_ids chip_type, |
| 154 | const char *name, ad5686_write_func write, |
| 155 | ad5686_read_func read); |
| 156 | |
Uwe Kleine-König | 3ceed02 | 2021-10-13 22:32:18 +0200 | [diff] [blame] | 157 | void ad5686_remove(struct device *dev); |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 158 | |
| 159 | |
| 160 | #endif /* __DRIVERS_IIO_DAC_AD5686_H__ */ |