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/mod_devicetable.h>
15 
16 #define AD5593R_MODE_CONF		(0 << 4)
17 #define AD5593R_MODE_DAC_WRITE		(1 << 4)
18 #define AD5593R_MODE_ADC_READBACK	(4 << 4)
19 #define AD5593R_MODE_DAC_READBACK	(5 << 4)
20 #define AD5593R_MODE_GPIO_READBACK	(6 << 4)
21 #define AD5593R_MODE_REG_READBACK	(7 << 4)
22 
ad5593r_write_dac(struct ad5592r_state * st,unsigned chan,u16 value)23 static int ad5593r_write_dac(struct ad5592r_state *st, unsigned chan, u16 value)
24 {
25 	struct i2c_client *i2c = to_i2c_client(st->dev);
26 
27 	return i2c_smbus_write_word_swapped(i2c,
28 			AD5593R_MODE_DAC_WRITE | chan, value);
29 }
30 
ad5593r_read_adc(struct ad5592r_state * st,unsigned chan,u16 * value)31 static int ad5593r_read_adc(struct ad5592r_state *st, unsigned chan, u16 *value)
32 {
33 	struct i2c_client *i2c = to_i2c_client(st->dev);
34 	s32 val;
35 
36 	val = i2c_smbus_write_word_swapped(i2c,
37 			AD5593R_MODE_CONF | AD5592R_REG_ADC_SEQ, BIT(chan));
38 	if (val < 0)
39 		return (int) val;
40 
41 	val = i2c_smbus_read_word_swapped(i2c, AD5593R_MODE_ADC_READBACK);
42 	if (val < 0)
43 		return (int) val;
44 
45 	*value = (u16) val;
46 
47 	return 0;
48 }
49 
ad5593r_reg_write(struct ad5592r_state * st,u8 reg,u16 value)50 static int ad5593r_reg_write(struct ad5592r_state *st, u8 reg, u16 value)
51 {
52 	struct i2c_client *i2c = to_i2c_client(st->dev);
53 
54 	return i2c_smbus_write_word_swapped(i2c,
55 			AD5593R_MODE_CONF | reg, value);
56 }
57 
ad5593r_reg_read(struct ad5592r_state * st,u8 reg,u16 * value)58 static int ad5593r_reg_read(struct ad5592r_state *st, u8 reg, u16 *value)
59 {
60 	struct i2c_client *i2c = to_i2c_client(st->dev);
61 	s32 val;
62 
63 	val = i2c_smbus_read_word_swapped(i2c, AD5593R_MODE_REG_READBACK | reg);
64 	if (val < 0)
65 		return (int) val;
66 
67 	*value = (u16) val;
68 
69 	return 0;
70 }
71 
ad5593r_gpio_read(struct ad5592r_state * st,u8 * value)72 static int ad5593r_gpio_read(struct ad5592r_state *st, u8 *value)
73 {
74 	struct i2c_client *i2c = to_i2c_client(st->dev);
75 	s32 val;
76 
77 	val = i2c_smbus_read_word_swapped(i2c, AD5593R_MODE_GPIO_READBACK);
78 	if (val < 0)
79 		return (int) val;
80 
81 	*value = (u8) val;
82 
83 	return 0;
84 }
85 
86 static const struct ad5592r_rw_ops ad5593r_rw_ops = {
87 	.write_dac = ad5593r_write_dac,
88 	.read_adc = ad5593r_read_adc,
89 	.reg_write = ad5593r_reg_write,
90 	.reg_read = ad5593r_reg_read,
91 	.gpio_read = ad5593r_gpio_read,
92 };
93 
ad5593r_i2c_probe(struct i2c_client * i2c,const struct i2c_device_id * id)94 static int ad5593r_i2c_probe(struct i2c_client *i2c,
95 		const struct i2c_device_id *id)
96 {
97 	return ad5592r_probe(&i2c->dev, id->name, &ad5593r_rw_ops);
98 }
99 
ad5593r_i2c_remove(struct i2c_client * i2c)100 static int ad5593r_i2c_remove(struct i2c_client *i2c)
101 {
102 	ad5592r_remove(&i2c->dev);
103 
104 	return 0;
105 }
106 
107 static const struct i2c_device_id ad5593r_i2c_ids[] = {
108 	{ .name = "ad5593r", },
109 	{},
110 };
111 MODULE_DEVICE_TABLE(i2c, ad5593r_i2c_ids);
112 
113 static const struct of_device_id ad5593r_of_match[] = {
114 	{ .compatible = "adi,ad5593r", },
115 	{},
116 };
117 MODULE_DEVICE_TABLE(of, ad5593r_of_match);
118 
119 static const struct acpi_device_id ad5593r_acpi_match[] = {
120 	{"ADS5593", },
121 	{ },
122 };
123 MODULE_DEVICE_TABLE(acpi, ad5593r_acpi_match);
124 
125 static struct i2c_driver ad5593r_driver = {
126 	.driver = {
127 		.name = "ad5593r",
128 		.of_match_table = ad5593r_of_match,
129 		.acpi_match_table = ad5593r_acpi_match,
130 	},
131 	.probe = ad5593r_i2c_probe,
132 	.remove = ad5593r_i2c_remove,
133 	.id_table = ad5593r_i2c_ids,
134 };
135 module_i2c_driver(ad5593r_driver);
136 
137 MODULE_AUTHOR("Paul Cercueil <paul.cercueil@analog.com>");
138 MODULE_DESCRIPTION("Analog Devices AD5593R multi-channel converters");
139 MODULE_LICENSE("GPL v2");
140 MODULE_IMPORT_NS(IIO_AD5592R);
141