blob: 3edeb677996dd681ba148246c4f43b0b4413ea76 [file] [log] [blame]
Paul Cercueil56ca9db2016-04-05 09:46:19 +02001/*
2 * AD5593R 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/i2c.h>
14#include <linux/module.h>
15#include <linux/of.h>
16
Michael Hennerich805df042022-09-13 09:34:12 +020017#include <asm/unaligned.h>
18
Paul Cercueil56ca9db2016-04-05 09:46:19 +020019#define AD5593R_MODE_CONF (0 << 4)
20#define AD5593R_MODE_DAC_WRITE (1 << 4)
21#define AD5593R_MODE_ADC_READBACK (4 << 4)
22#define AD5593R_MODE_DAC_READBACK (5 << 4)
23#define AD5593R_MODE_GPIO_READBACK (6 << 4)
24#define AD5593R_MODE_REG_READBACK (7 << 4)
25
Michael Hennerich805df042022-09-13 09:34:12 +020026static int ad5593r_read_word(struct i2c_client *i2c, u8 reg, u16 *value)
27{
28 int ret;
29 u8 buf[2];
30
31 ret = i2c_smbus_write_byte(i2c, reg);
32 if (ret < 0)
33 return ret;
34
35 ret = i2c_master_recv(i2c, buf, sizeof(buf));
36 if (ret < 0)
37 return ret;
38
39 *value = get_unaligned_be16(buf);
40
41 return 0;
42}
43
Paul Cercueil56ca9db2016-04-05 09:46:19 +020044static int ad5593r_write_dac(struct ad5592r_state *st, unsigned chan, u16 value)
45{
46 struct i2c_client *i2c = to_i2c_client(st->dev);
47
48 return i2c_smbus_write_word_swapped(i2c,
49 AD5593R_MODE_DAC_WRITE | chan, value);
50}
51
52static int ad5593r_read_adc(struct ad5592r_state *st, unsigned chan, u16 *value)
53{
54 struct i2c_client *i2c = to_i2c_client(st->dev);
55 s32 val;
56
57 val = i2c_smbus_write_word_swapped(i2c,
58 AD5593R_MODE_CONF | AD5592R_REG_ADC_SEQ, BIT(chan));
59 if (val < 0)
60 return (int) val;
61
Michael Hennerich805df042022-09-13 09:34:12 +020062 return ad5593r_read_word(i2c, AD5593R_MODE_ADC_READBACK, value);
Paul Cercueil56ca9db2016-04-05 09:46:19 +020063}
64
65static int ad5593r_reg_write(struct ad5592r_state *st, u8 reg, u16 value)
66{
67 struct i2c_client *i2c = to_i2c_client(st->dev);
68
69 return i2c_smbus_write_word_swapped(i2c,
70 AD5593R_MODE_CONF | reg, value);
71}
72
73static int ad5593r_reg_read(struct ad5592r_state *st, u8 reg, u16 *value)
74{
75 struct i2c_client *i2c = to_i2c_client(st->dev);
Paul Cercueil56ca9db2016-04-05 09:46:19 +020076
Michael Hennerich805df042022-09-13 09:34:12 +020077 return ad5593r_read_word(i2c, AD5593R_MODE_REG_READBACK | reg, value);
Paul Cercueil56ca9db2016-04-05 09:46:19 +020078}
79
80static int ad5593r_gpio_read(struct ad5592r_state *st, u8 *value)
81{
82 struct i2c_client *i2c = to_i2c_client(st->dev);
Michael Hennerich805df042022-09-13 09:34:12 +020083 u16 val;
84 int ret;
Paul Cercueil56ca9db2016-04-05 09:46:19 +020085
Michael Hennerich805df042022-09-13 09:34:12 +020086 ret = ad5593r_read_word(i2c, AD5593R_MODE_GPIO_READBACK, &val);
87 if (ret)
88 return ret;
Paul Cercueil56ca9db2016-04-05 09:46:19 +020089
90 *value = (u8) val;
91
92 return 0;
93}
94
95static const struct ad5592r_rw_ops ad5593r_rw_ops = {
96 .write_dac = ad5593r_write_dac,
97 .read_adc = ad5593r_read_adc,
98 .reg_write = ad5593r_reg_write,
99 .reg_read = ad5593r_reg_read,
100 .gpio_read = ad5593r_gpio_read,
101};
102
103static int ad5593r_i2c_probe(struct i2c_client *i2c,
104 const struct i2c_device_id *id)
105{
106 return ad5592r_probe(&i2c->dev, id->name, &ad5593r_rw_ops);
107}
108
109static int ad5593r_i2c_remove(struct i2c_client *i2c)
110{
111 return ad5592r_remove(&i2c->dev);
112}
113
114static const struct i2c_device_id ad5593r_i2c_ids[] = {
115 { .name = "ad5593r", },
116 {},
117};
118MODULE_DEVICE_TABLE(i2c, ad5593r_i2c_ids);
119
120static const struct of_device_id ad5593r_of_match[] = {
121 { .compatible = "adi,ad5593r", },
122 {},
123};
124MODULE_DEVICE_TABLE(of, ad5593r_of_match);
125
126static struct i2c_driver ad5593r_driver = {
127 .driver = {
128 .name = "ad5593r",
129 .of_match_table = of_match_ptr(ad5593r_of_match),
130 },
131 .probe = ad5593r_i2c_probe,
132 .remove = ad5593r_i2c_remove,
133 .id_table = ad5593r_i2c_ids,
134};
135module_i2c_driver(ad5593r_driver);
136
137MODULE_AUTHOR("Paul Cercueil <paul.cercueil@analog.com>");
138MODULE_DESCRIPTION("Analog Devices AD5592R multi-channel converters");
139MODULE_LICENSE("GPL v2");