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