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 | /* |
Mircea Caprioru | 192778f | 2019-01-09 11:14:16 +0200 | [diff] [blame] | 3 | * AD5672R, AD5674R, AD5676, AD5676R, AD5679R, |
| 4 | * AD5681R, AD5682R, AD5683, AD5683R, AD5684, |
| 5 | * AD5684R, AD5685R, AD5686, AD5686R |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 6 | * Digital to analog converters driver |
| 7 | * |
| 8 | * Copyright 2018 Analog Devices Inc. |
| 9 | */ |
| 10 | |
| 11 | #include "ad5686.h" |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/spi/spi.h> |
| 15 | |
| 16 | static int ad5686_spi_write(struct ad5686_state *st, |
| 17 | u8 cmd, u8 addr, u16 val) |
| 18 | { |
| 19 | struct spi_device *spi = to_spi_device(st->dev); |
Stefan Popa | 1dbae4c | 2018-05-18 18:23:34 +0300 | [diff] [blame] | 20 | u8 tx_len, *buf; |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 21 | |
Stefan Popa | 1dbae4c | 2018-05-18 18:23:34 +0300 | [diff] [blame] | 22 | switch (st->chip_info->regmap_type) { |
Stefan Popa | 12d323c | 2018-12-06 15:38:30 +0200 | [diff] [blame] | 23 | case AD5310_REGMAP: |
| 24 | st->data[0].d16 = cpu_to_be16(AD5310_CMD(cmd) | |
| 25 | val); |
| 26 | buf = &st->data[0].d8[0]; |
| 27 | tx_len = 2; |
| 28 | break; |
Stefan Popa | 1dbae4c | 2018-05-18 18:23:34 +0300 | [diff] [blame] | 29 | case AD5683_REGMAP: |
| 30 | st->data[0].d32 = cpu_to_be32(AD5686_CMD(cmd) | |
| 31 | AD5683_DATA(val)); |
| 32 | buf = &st->data[0].d8[1]; |
| 33 | tx_len = 3; |
| 34 | break; |
| 35 | case AD5686_REGMAP: |
| 36 | st->data[0].d32 = cpu_to_be32(AD5686_CMD(cmd) | |
| 37 | AD5686_ADDR(addr) | |
| 38 | val); |
| 39 | buf = &st->data[0].d8[1]; |
| 40 | tx_len = 3; |
| 41 | break; |
| 42 | default: |
| 43 | return -EINVAL; |
| 44 | } |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 45 | |
Stefan Popa | 1dbae4c | 2018-05-18 18:23:34 +0300 | [diff] [blame] | 46 | return spi_write(spi, buf, tx_len); |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static int ad5686_spi_read(struct ad5686_state *st, u8 addr) |
| 50 | { |
| 51 | struct spi_transfer t[] = { |
| 52 | { |
| 53 | .tx_buf = &st->data[0].d8[1], |
| 54 | .len = 3, |
| 55 | .cs_change = 1, |
| 56 | }, { |
| 57 | .tx_buf = &st->data[1].d8[1], |
| 58 | .rx_buf = &st->data[2].d8[1], |
| 59 | .len = 3, |
| 60 | }, |
| 61 | }; |
| 62 | struct spi_device *spi = to_spi_device(st->dev); |
Stefan Popa | 1dbae4c | 2018-05-18 18:23:34 +0300 | [diff] [blame] | 63 | u8 cmd = 0; |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 64 | int ret; |
| 65 | |
Stefan Popa | 12d323c | 2018-12-06 15:38:30 +0200 | [diff] [blame] | 66 | switch (st->chip_info->regmap_type) { |
| 67 | case AD5310_REGMAP: |
| 68 | return -ENOTSUPP; |
| 69 | case AD5683_REGMAP: |
Stefan Popa | 1dbae4c | 2018-05-18 18:23:34 +0300 | [diff] [blame] | 70 | cmd = AD5686_CMD_READBACK_ENABLE_V2; |
Stefan Popa | 12d323c | 2018-12-06 15:38:30 +0200 | [diff] [blame] | 71 | break; |
| 72 | case AD5686_REGMAP: |
| 73 | cmd = AD5686_CMD_READBACK_ENABLE; |
| 74 | break; |
| 75 | default: |
| 76 | return -EINVAL; |
| 77 | } |
Stefan Popa | 1dbae4c | 2018-05-18 18:23:34 +0300 | [diff] [blame] | 78 | |
| 79 | st->data[0].d32 = cpu_to_be32(AD5686_CMD(cmd) | |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 80 | AD5686_ADDR(addr)); |
| 81 | st->data[1].d32 = cpu_to_be32(AD5686_CMD(AD5686_CMD_NOOP)); |
| 82 | |
| 83 | ret = spi_sync_transfer(spi, t, ARRAY_SIZE(t)); |
| 84 | if (ret < 0) |
| 85 | return ret; |
| 86 | |
| 87 | return be32_to_cpu(st->data[2].d32); |
| 88 | } |
| 89 | |
| 90 | static int ad5686_spi_probe(struct spi_device *spi) |
| 91 | { |
| 92 | const struct spi_device_id *id = spi_get_device_id(spi); |
| 93 | |
| 94 | return ad5686_probe(&spi->dev, id->driver_data, id->name, |
| 95 | ad5686_spi_write, ad5686_spi_read); |
| 96 | } |
| 97 | |
| 98 | static int ad5686_spi_remove(struct spi_device *spi) |
| 99 | { |
| 100 | return ad5686_remove(&spi->dev); |
| 101 | } |
| 102 | |
| 103 | static const struct spi_device_id ad5686_spi_id[] = { |
Stefan Popa | 12d323c | 2018-12-06 15:38:30 +0200 | [diff] [blame] | 104 | {"ad5310r", ID_AD5310R}, |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 105 | {"ad5672r", ID_AD5672R}, |
Mircea Caprioru | 192778f | 2019-01-09 11:14:16 +0200 | [diff] [blame] | 106 | {"ad5674r", ID_AD5674R}, |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 107 | {"ad5676", ID_AD5676}, |
| 108 | {"ad5676r", ID_AD5676R}, |
Mircea Caprioru | 192778f | 2019-01-09 11:14:16 +0200 | [diff] [blame] | 109 | {"ad5679r", ID_AD5679R}, |
Stefan Popa | 1dbae4c | 2018-05-18 18:23:34 +0300 | [diff] [blame] | 110 | {"ad5681r", ID_AD5681R}, |
| 111 | {"ad5682r", ID_AD5682R}, |
| 112 | {"ad5683", ID_AD5683}, |
| 113 | {"ad5683r", ID_AD5683R}, |
Stefan Popa | 0357e48 | 2018-04-11 14:53:17 +0300 | [diff] [blame] | 114 | {"ad5684", ID_AD5684}, |
| 115 | {"ad5684r", ID_AD5684R}, |
| 116 | {"ad5685", ID_AD5685R}, /* Does not exist */ |
| 117 | {"ad5685r", ID_AD5685R}, |
| 118 | {"ad5686", ID_AD5686}, |
| 119 | {"ad5686r", ID_AD5686R}, |
| 120 | {} |
| 121 | }; |
| 122 | MODULE_DEVICE_TABLE(spi, ad5686_spi_id); |
| 123 | |
| 124 | static struct spi_driver ad5686_spi_driver = { |
| 125 | .driver = { |
| 126 | .name = "ad5686", |
| 127 | }, |
| 128 | .probe = ad5686_spi_probe, |
| 129 | .remove = ad5686_spi_remove, |
| 130 | .id_table = ad5686_spi_id, |
| 131 | }; |
| 132 | |
| 133 | module_spi_driver(ad5686_spi_driver); |
| 134 | |
| 135 | MODULE_AUTHOR("Stefan Popa <stefan.popa@analog.com>"); |
| 136 | MODULE_DESCRIPTION("Analog Devices AD5686 and similar multi-channel DACs"); |
| 137 | MODULE_LICENSE("GPL v2"); |