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