2 * AD5593R Digital <-> Analog converters driver
4 * Copyright 2015-2016 Analog Devices Inc.
5 * Author: Paul Cercueil <paul.cercueil@analog.com>
7 * Licensed under the GPL-2.
10 #include "ad5592r-base.h"
12 #include <linux/bitops.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
16 #include <linux/acpi.h>
18 #include <asm/unaligned.h>
20 #define AD5593R_MODE_CONF (0 << 4)
21 #define AD5593R_MODE_DAC_WRITE (1 << 4)
22 #define AD5593R_MODE_ADC_READBACK (4 << 4)
23 #define AD5593R_MODE_DAC_READBACK (5 << 4)
24 #define AD5593R_MODE_GPIO_READBACK (6 << 4)
25 #define AD5593R_MODE_REG_READBACK (7 << 4)
27 static int ad5593r_read_word(struct i2c_client *i2c, u8 reg, u16 *value)
32 ret = i2c_smbus_write_byte(i2c, reg);
36 ret = i2c_master_recv(i2c, buf, sizeof(buf));
40 *value = get_unaligned_be16(buf);
45 static int ad5593r_write_dac(struct ad5592r_state *st, unsigned chan, u16 value)
47 struct i2c_client *i2c = to_i2c_client(st->dev);
49 return i2c_smbus_write_word_swapped(i2c,
50 AD5593R_MODE_DAC_WRITE | chan, value);
53 static int ad5593r_read_adc(struct ad5592r_state *st, unsigned chan, u16 *value)
55 struct i2c_client *i2c = to_i2c_client(st->dev);
58 val = i2c_smbus_write_word_swapped(i2c,
59 AD5593R_MODE_CONF | AD5592R_REG_ADC_SEQ, BIT(chan));
63 return ad5593r_read_word(i2c, AD5593R_MODE_ADC_READBACK, value);
66 static int ad5593r_reg_write(struct ad5592r_state *st, u8 reg, u16 value)
68 struct i2c_client *i2c = to_i2c_client(st->dev);
70 return i2c_smbus_write_word_swapped(i2c,
71 AD5593R_MODE_CONF | reg, value);
74 static int ad5593r_reg_read(struct ad5592r_state *st, u8 reg, u16 *value)
76 struct i2c_client *i2c = to_i2c_client(st->dev);
78 return ad5593r_read_word(i2c, AD5593R_MODE_REG_READBACK | reg, value);
81 static int ad5593r_gpio_read(struct ad5592r_state *st, u8 *value)
83 struct i2c_client *i2c = to_i2c_client(st->dev);
87 ret = ad5593r_read_word(i2c, AD5593R_MODE_GPIO_READBACK, &val);
96 static const struct ad5592r_rw_ops ad5593r_rw_ops = {
97 .write_dac = ad5593r_write_dac,
98 .read_adc = ad5593r_read_adc,
99 .reg_write = ad5593r_reg_write,
100 .reg_read = ad5593r_reg_read,
101 .gpio_read = ad5593r_gpio_read,
104 static int ad5593r_i2c_probe(struct i2c_client *i2c,
105 const struct i2c_device_id *id)
107 return ad5592r_probe(&i2c->dev, id->name, &ad5593r_rw_ops);
110 static int ad5593r_i2c_remove(struct i2c_client *i2c)
112 return ad5592r_remove(&i2c->dev);
115 static const struct i2c_device_id ad5593r_i2c_ids[] = {
116 { .name = "ad5593r", },
119 MODULE_DEVICE_TABLE(i2c, ad5593r_i2c_ids);
121 static const struct of_device_id ad5593r_of_match[] = {
122 { .compatible = "adi,ad5593r", },
125 MODULE_DEVICE_TABLE(of, ad5593r_of_match);
127 static const struct acpi_device_id ad5593r_acpi_match[] = {
131 MODULE_DEVICE_TABLE(acpi, ad5593r_acpi_match);
133 static struct i2c_driver ad5593r_driver = {
136 .of_match_table = of_match_ptr(ad5593r_of_match),
137 .acpi_match_table = ACPI_PTR(ad5593r_acpi_match),
139 .probe = ad5593r_i2c_probe,
140 .remove = ad5593r_i2c_remove,
141 .id_table = ad5593r_i2c_ids,
143 module_i2c_driver(ad5593r_driver);
145 MODULE_AUTHOR("Paul Cercueil <paul.cercueil@analog.com>");
146 MODULE_DESCRIPTION("Analog Devices AD5592R multi-channel converters");
147 MODULE_LICENSE("GPL v2");