1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * AD5338R, AD5671R, AD5673R, AD5675R, AD5677R, AD5691R, AD5692R, AD5693,
4 * AD5693R, AD5694, AD5694R, AD5695R, AD5696, AD5696R
5 * Digital to analog converters driver
6 *
7 * Copyright 2018 Analog Devices Inc.
8 */
9
10 #include "ad5686.h"
11
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14
ad5686_i2c_read(struct ad5686_state * st,u8 addr)15 static int ad5686_i2c_read(struct ad5686_state *st, u8 addr)
16 {
17 struct i2c_client *i2c = to_i2c_client(st->dev);
18 struct i2c_msg msg[2] = {
19 {
20 .addr = i2c->addr,
21 .flags = i2c->flags,
22 .len = 3,
23 .buf = &st->data[0].d8[1],
24 },
25 {
26 .addr = i2c->addr,
27 .flags = i2c->flags | I2C_M_RD,
28 .len = 2,
29 .buf = (char *)&st->data[0].d16,
30 },
31 };
32 int ret;
33
34 st->data[0].d32 = cpu_to_be32(AD5686_CMD(AD5686_CMD_NOOP) |
35 AD5686_ADDR(addr) |
36 0x00);
37
38 ret = i2c_transfer(i2c->adapter, msg, 2);
39 if (ret < 0)
40 return ret;
41
42 return be16_to_cpu(st->data[0].d16);
43 }
44
ad5686_i2c_write(struct ad5686_state * st,u8 cmd,u8 addr,u16 val)45 static int ad5686_i2c_write(struct ad5686_state *st,
46 u8 cmd, u8 addr, u16 val)
47 {
48 struct i2c_client *i2c = to_i2c_client(st->dev);
49 int ret;
50
51 st->data[0].d32 = cpu_to_be32(AD5686_CMD(cmd) | AD5686_ADDR(addr)
52 | val);
53
54 ret = i2c_master_send(i2c, &st->data[0].d8[1], 3);
55 if (ret < 0)
56 return ret;
57
58 return (ret != 3) ? -EIO : 0;
59 }
60
ad5686_i2c_probe(struct i2c_client * i2c)61 static int ad5686_i2c_probe(struct i2c_client *i2c)
62 {
63 const struct i2c_device_id *id = i2c_client_get_device_id(i2c);
64 return ad5686_probe(&i2c->dev, id->driver_data, id->name,
65 ad5686_i2c_write, ad5686_i2c_read);
66 }
67
ad5686_i2c_remove(struct i2c_client * i2c)68 static void ad5686_i2c_remove(struct i2c_client *i2c)
69 {
70 ad5686_remove(&i2c->dev);
71 }
72
73 static const struct i2c_device_id ad5686_i2c_id[] = {
74 {"ad5311r", ID_AD5311R},
75 {"ad5337r", ID_AD5337R},
76 {"ad5338r", ID_AD5338R},
77 {"ad5671r", ID_AD5671R},
78 {"ad5673r", ID_AD5673R},
79 {"ad5675r", ID_AD5675R},
80 {"ad5677r", ID_AD5677R},
81 {"ad5691r", ID_AD5691R},
82 {"ad5692r", ID_AD5692R},
83 {"ad5693", ID_AD5693},
84 {"ad5693r", ID_AD5693R},
85 {"ad5694", ID_AD5694},
86 {"ad5694r", ID_AD5694R},
87 {"ad5695r", ID_AD5695R},
88 {"ad5696", ID_AD5696},
89 {"ad5696r", ID_AD5696R},
90 {}
91 };
92 MODULE_DEVICE_TABLE(i2c, ad5686_i2c_id);
93
94 static const struct of_device_id ad5686_of_match[] = {
95 { .compatible = "adi,ad5311r" },
96 { .compatible = "adi,ad5337r" },
97 { .compatible = "adi,ad5338r" },
98 { .compatible = "adi,ad5671r" },
99 { .compatible = "adi,ad5675r" },
100 { .compatible = "adi,ad5691r" },
101 { .compatible = "adi,ad5692r" },
102 { .compatible = "adi,ad5693" },
103 { .compatible = "adi,ad5693r" },
104 { .compatible = "adi,ad5694" },
105 { .compatible = "adi,ad5694r" },
106 { .compatible = "adi,ad5695r" },
107 { .compatible = "adi,ad5696" },
108 { .compatible = "adi,ad5696r" },
109 {}
110 };
111 MODULE_DEVICE_TABLE(of, ad5686_of_match);
112
113 static struct i2c_driver ad5686_i2c_driver = {
114 .driver = {
115 .name = "ad5696",
116 .of_match_table = ad5686_of_match,
117 },
118 .probe = ad5686_i2c_probe,
119 .remove = ad5686_i2c_remove,
120 .id_table = ad5686_i2c_id,
121 };
122
123 module_i2c_driver(ad5686_i2c_driver);
124
125 MODULE_AUTHOR("Stefan Popa <stefan.popa@analog.com>");
126 MODULE_DESCRIPTION("Analog Devices AD5686 and similar multi-channel DACs");
127 MODULE_LICENSE("GPL v2");
128 MODULE_IMPORT_NS(IIO_AD5686);
129