blob: 6a12a3c9d94f9d923640181a37d64e291e1f0c8d [file] [log] [blame]
Paul Cercueil56ca9db2016-04-05 09:46:19 +02001/*
2 * AD5592R Digital <-> Analog converters driver
3 *
4 * Copyright 2015-2016 Analog Devices Inc.
5 * Author: Paul Cercueil <paul.cercueil@analog.com>
6 *
7 * Licensed under the GPL-2.
8 */
9
10#include "ad5592r-base.h"
11
12#include <linux/bitops.h>
13#include <linux/module.h>
14#include <linux/of.h>
15#include <linux/spi/spi.h>
Michael Hennerichb0570bc2016-12-19 13:10:38 +010016#include <linux/acpi.h>
Paul Cercueil56ca9db2016-04-05 09:46:19 +020017
18#define AD5592R_GPIO_READBACK_EN BIT(10)
19#define AD5592R_LDAC_READBACK_EN BIT(6)
20
Sandhya Bankar38e442f2016-09-24 00:50:22 +053021static int ad5592r_spi_wnop_r16(struct ad5592r_state *st, __be16 *buf)
Paul Cercueil56ca9db2016-04-05 09:46:19 +020022{
23 struct spi_device *spi = container_of(st->dev, struct spi_device, dev);
24 struct spi_transfer t = {
25 .tx_buf = &st->spi_msg_nop,
26 .rx_buf = buf,
27 .len = 2
28 };
29
30 st->spi_msg_nop = 0; /* NOP */
31
32 return spi_sync_transfer(spi, &t, 1);
33}
34
35static int ad5592r_write_dac(struct ad5592r_state *st, unsigned chan, u16 value)
36{
37 struct spi_device *spi = container_of(st->dev, struct spi_device, dev);
38
39 st->spi_msg = cpu_to_be16(BIT(15) | (chan << 12) | value);
40
41 return spi_write(spi, &st->spi_msg, sizeof(st->spi_msg));
42}
43
44static int ad5592r_read_adc(struct ad5592r_state *st, unsigned chan, u16 *value)
45{
46 struct spi_device *spi = container_of(st->dev, struct spi_device, dev);
47 int ret;
48
49 st->spi_msg = cpu_to_be16((AD5592R_REG_ADC_SEQ << 11) | BIT(chan));
50
51 ret = spi_write(spi, &st->spi_msg, sizeof(st->spi_msg));
52 if (ret)
53 return ret;
54
55 /*
56 * Invalid data:
57 * See Figure 40. Single-Channel ADC Conversion Sequence
58 */
59 ret = ad5592r_spi_wnop_r16(st, &st->spi_msg);
60 if (ret)
61 return ret;
62
63 ret = ad5592r_spi_wnop_r16(st, &st->spi_msg);
64 if (ret)
65 return ret;
66
67 *value = be16_to_cpu(st->spi_msg);
68
69 return 0;
70}
71
72static int ad5592r_reg_write(struct ad5592r_state *st, u8 reg, u16 value)
73{
74 struct spi_device *spi = container_of(st->dev, struct spi_device, dev);
75
76 st->spi_msg = cpu_to_be16((reg << 11) | value);
77
78 return spi_write(spi, &st->spi_msg, sizeof(st->spi_msg));
79}
80
81static int ad5592r_reg_read(struct ad5592r_state *st, u8 reg, u16 *value)
82{
83 struct spi_device *spi = container_of(st->dev, struct spi_device, dev);
84 int ret;
85
86 st->spi_msg = cpu_to_be16((AD5592R_REG_LDAC << 11) |
87 AD5592R_LDAC_READBACK_EN | (reg << 2));
88
89 ret = spi_write(spi, &st->spi_msg, sizeof(st->spi_msg));
90 if (ret)
91 return ret;
92
93 ret = ad5592r_spi_wnop_r16(st, &st->spi_msg);
94 if (ret)
95 return ret;
96
97 *value = be16_to_cpu(st->spi_msg);
98
99 return 0;
100}
101
102static int ad5593r_gpio_read(struct ad5592r_state *st, u8 *value)
103{
104 int ret;
105
106 ret = ad5592r_reg_write(st, AD5592R_REG_GPIO_IN_EN,
107 AD5592R_GPIO_READBACK_EN | st->gpio_in);
108 if (ret)
109 return ret;
110
111 ret = ad5592r_spi_wnop_r16(st, &st->spi_msg);
112 if (ret)
113 return ret;
114
115 *value = (u8) be16_to_cpu(st->spi_msg);
116
117 return 0;
118}
119
120static const struct ad5592r_rw_ops ad5592r_rw_ops = {
121 .write_dac = ad5592r_write_dac,
122 .read_adc = ad5592r_read_adc,
123 .reg_write = ad5592r_reg_write,
124 .reg_read = ad5592r_reg_read,
125 .gpio_read = ad5593r_gpio_read,
126};
127
128static int ad5592r_spi_probe(struct spi_device *spi)
129{
130 const struct spi_device_id *id = spi_get_device_id(spi);
131
132 return ad5592r_probe(&spi->dev, id->name, &ad5592r_rw_ops);
133}
134
135static int ad5592r_spi_remove(struct spi_device *spi)
136{
137 return ad5592r_remove(&spi->dev);
138}
139
140static const struct spi_device_id ad5592r_spi_ids[] = {
141 { .name = "ad5592r", },
142 {}
143};
144MODULE_DEVICE_TABLE(spi, ad5592r_spi_ids);
145
146static const struct of_device_id ad5592r_of_match[] = {
147 { .compatible = "adi,ad5592r", },
148 {},
149};
150MODULE_DEVICE_TABLE(of, ad5592r_of_match);
151
Michael Hennerichb0570bc2016-12-19 13:10:38 +0100152static const struct acpi_device_id ad5592r_acpi_match[] = {
153 {"ADS5592", },
154 { },
155};
156MODULE_DEVICE_TABLE(acpi, ad5592r_acpi_match);
157
Paul Cercueil56ca9db2016-04-05 09:46:19 +0200158static struct spi_driver ad5592r_spi_driver = {
159 .driver = {
160 .name = "ad5592r",
161 .of_match_table = of_match_ptr(ad5592r_of_match),
Michael Hennerichb0570bc2016-12-19 13:10:38 +0100162 .acpi_match_table = ACPI_PTR(ad5592r_acpi_match),
Paul Cercueil56ca9db2016-04-05 09:46:19 +0200163 },
164 .probe = ad5592r_spi_probe,
165 .remove = ad5592r_spi_remove,
166 .id_table = ad5592r_spi_ids,
167};
168module_spi_driver(ad5592r_spi_driver);
169
170MODULE_AUTHOR("Paul Cercueil <paul.cercueil@analog.com>");
171MODULE_DESCRIPTION("Analog Devices AD5592R multi-channel converters");
172MODULE_LICENSE("GPL v2");