1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * AD5593R Digital <-> Analog converters driver
4 *
5 * Copyright 2015-2016 Analog Devices Inc.
6 * Author: Paul Cercueil <paul.cercueil@analog.com>
7 */
8
9 #include "ad5592r-base.h"
10
11 #include <linux/bitops.h>
12 #include <linux/i2c.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/acpi.h>
16
17 #include <asm/unaligned.h>
18
19 #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
ad5593r_read_word(struct i2c_client * i2c,u8 reg,u16 * value)26 static 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
ad5593r_write_dac(struct ad5592r_state * st,unsigned chan,u16 value)44 static 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
ad5593r_read_adc(struct ad5592r_state * st,unsigned chan,u16 * value)52 static 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
62 return ad5593r_read_word(i2c, AD5593R_MODE_ADC_READBACK, value);
63 }
64
ad5593r_reg_write(struct ad5592r_state * st,u8 reg,u16 value)65 static 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
ad5593r_reg_read(struct ad5592r_state * st,u8 reg,u16 * value)73 static int ad5593r_reg_read(struct ad5592r_state *st, u8 reg, u16 *value)
74 {
75 struct i2c_client *i2c = to_i2c_client(st->dev);
76
77 return ad5593r_read_word(i2c, AD5593R_MODE_REG_READBACK | reg, value);
78 }
79
ad5593r_gpio_read(struct ad5592r_state * st,u8 * value)80 static int ad5593r_gpio_read(struct ad5592r_state *st, u8 *value)
81 {
82 struct i2c_client *i2c = to_i2c_client(st->dev);
83 u16 val;
84 int ret;
85
86 ret = ad5593r_read_word(i2c, AD5593R_MODE_GPIO_READBACK, &val);
87 if (ret)
88 return ret;
89
90 *value = (u8) val;
91
92 return 0;
93 }
94
95 static 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
ad5593r_i2c_probe(struct i2c_client * i2c,const struct i2c_device_id * id)103 static 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
ad5593r_i2c_remove(struct i2c_client * i2c)109 static int ad5593r_i2c_remove(struct i2c_client *i2c)
110 {
111 return ad5592r_remove(&i2c->dev);
112 }
113
114 static const struct i2c_device_id ad5593r_i2c_ids[] = {
115 { .name = "ad5593r", },
116 {},
117 };
118 MODULE_DEVICE_TABLE(i2c, ad5593r_i2c_ids);
119
120 static const struct of_device_id ad5593r_of_match[] = {
121 { .compatible = "adi,ad5593r", },
122 {},
123 };
124 MODULE_DEVICE_TABLE(of, ad5593r_of_match);
125
126 static const struct acpi_device_id ad5593r_acpi_match[] = {
127 {"ADS5593", },
128 { },
129 };
130 MODULE_DEVICE_TABLE(acpi, ad5593r_acpi_match);
131
132 static struct i2c_driver ad5593r_driver = {
133 .driver = {
134 .name = "ad5593r",
135 .of_match_table = of_match_ptr(ad5593r_of_match),
136 .acpi_match_table = ACPI_PTR(ad5593r_acpi_match),
137 },
138 .probe = ad5593r_i2c_probe,
139 .remove = ad5593r_i2c_remove,
140 .id_table = ad5593r_i2c_ids,
141 };
142 module_i2c_driver(ad5593r_driver);
143
144 MODULE_AUTHOR("Paul Cercueil <paul.cercueil@analog.com>");
145 MODULE_DESCRIPTION("Analog Devices AD5592R multi-channel converters");
146 MODULE_LICENSE("GPL v2");
147