Paul Cercueil | 56ca9db | 2016-04-05 09:46:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * AD5593R Digital <-> Analog converters driver |
| 3 | * |
| 4 | * Copyright 2015-2016 Analog Devices Inc. |
| 5 | * Author: Paul Cercueil <paul.cercueil@analog.com> |
| 6 | * |
| 7 | * Licensed under the GPL-2. |
| 8 | */ |
| 9 | |
| 10 | #include "ad5592r-base.h" |
| 11 | |
| 12 | #include <linux/bitops.h> |
| 13 | #include <linux/i2c.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/of.h> |
| 16 | |
Michael Hennerich | 805df04 | 2022-09-13 09:34:12 +0200 | [diff] [blame] | 17 | #include <asm/unaligned.h> |
| 18 | |
Paul Cercueil | 56ca9db | 2016-04-05 09:46:19 +0200 | [diff] [blame] | 19 | #define AD5593R_MODE_CONF (0 << 4) |
| 20 | #define AD5593R_MODE_DAC_WRITE (1 << 4) |
| 21 | #define AD5593R_MODE_ADC_READBACK (4 << 4) |
| 22 | #define AD5593R_MODE_DAC_READBACK (5 << 4) |
| 23 | #define AD5593R_MODE_GPIO_READBACK (6 << 4) |
| 24 | #define AD5593R_MODE_REG_READBACK (7 << 4) |
| 25 | |
Michael Hennerich | 805df04 | 2022-09-13 09:34:12 +0200 | [diff] [blame] | 26 | static int ad5593r_read_word(struct i2c_client *i2c, u8 reg, u16 *value) |
| 27 | { |
| 28 | int ret; |
| 29 | u8 buf[2]; |
| 30 | |
| 31 | ret = i2c_smbus_write_byte(i2c, reg); |
| 32 | if (ret < 0) |
| 33 | return ret; |
| 34 | |
| 35 | ret = i2c_master_recv(i2c, buf, sizeof(buf)); |
| 36 | if (ret < 0) |
| 37 | return ret; |
| 38 | |
| 39 | *value = get_unaligned_be16(buf); |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
Paul Cercueil | 56ca9db | 2016-04-05 09:46:19 +0200 | [diff] [blame] | 44 | static int ad5593r_write_dac(struct ad5592r_state *st, unsigned chan, u16 value) |
| 45 | { |
| 46 | struct i2c_client *i2c = to_i2c_client(st->dev); |
| 47 | |
| 48 | return i2c_smbus_write_word_swapped(i2c, |
| 49 | AD5593R_MODE_DAC_WRITE | chan, value); |
| 50 | } |
| 51 | |
| 52 | static int ad5593r_read_adc(struct ad5592r_state *st, unsigned chan, u16 *value) |
| 53 | { |
| 54 | struct i2c_client *i2c = to_i2c_client(st->dev); |
| 55 | s32 val; |
| 56 | |
| 57 | val = i2c_smbus_write_word_swapped(i2c, |
| 58 | AD5593R_MODE_CONF | AD5592R_REG_ADC_SEQ, BIT(chan)); |
| 59 | if (val < 0) |
| 60 | return (int) val; |
| 61 | |
Michael Hennerich | 805df04 | 2022-09-13 09:34:12 +0200 | [diff] [blame] | 62 | return ad5593r_read_word(i2c, AD5593R_MODE_ADC_READBACK, value); |
Paul Cercueil | 56ca9db | 2016-04-05 09:46:19 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static int ad5593r_reg_write(struct ad5592r_state *st, u8 reg, u16 value) |
| 66 | { |
| 67 | struct i2c_client *i2c = to_i2c_client(st->dev); |
| 68 | |
| 69 | return i2c_smbus_write_word_swapped(i2c, |
| 70 | AD5593R_MODE_CONF | reg, value); |
| 71 | } |
| 72 | |
| 73 | static int ad5593r_reg_read(struct ad5592r_state *st, u8 reg, u16 *value) |
| 74 | { |
| 75 | struct i2c_client *i2c = to_i2c_client(st->dev); |
Paul Cercueil | 56ca9db | 2016-04-05 09:46:19 +0200 | [diff] [blame] | 76 | |
Michael Hennerich | 805df04 | 2022-09-13 09:34:12 +0200 | [diff] [blame] | 77 | return ad5593r_read_word(i2c, AD5593R_MODE_REG_READBACK | reg, value); |
Paul Cercueil | 56ca9db | 2016-04-05 09:46:19 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static int ad5593r_gpio_read(struct ad5592r_state *st, u8 *value) |
| 81 | { |
| 82 | struct i2c_client *i2c = to_i2c_client(st->dev); |
Michael Hennerich | 805df04 | 2022-09-13 09:34:12 +0200 | [diff] [blame] | 83 | u16 val; |
| 84 | int ret; |
Paul Cercueil | 56ca9db | 2016-04-05 09:46:19 +0200 | [diff] [blame] | 85 | |
Michael Hennerich | 805df04 | 2022-09-13 09:34:12 +0200 | [diff] [blame] | 86 | ret = ad5593r_read_word(i2c, AD5593R_MODE_GPIO_READBACK, &val); |
| 87 | if (ret) |
| 88 | return ret; |
Paul Cercueil | 56ca9db | 2016-04-05 09:46:19 +0200 | [diff] [blame] | 89 | |
| 90 | *value = (u8) val; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static const struct ad5592r_rw_ops ad5593r_rw_ops = { |
| 96 | .write_dac = ad5593r_write_dac, |
| 97 | .read_adc = ad5593r_read_adc, |
| 98 | .reg_write = ad5593r_reg_write, |
| 99 | .reg_read = ad5593r_reg_read, |
| 100 | .gpio_read = ad5593r_gpio_read, |
| 101 | }; |
| 102 | |
| 103 | static int ad5593r_i2c_probe(struct i2c_client *i2c, |
| 104 | const struct i2c_device_id *id) |
| 105 | { |
| 106 | return ad5592r_probe(&i2c->dev, id->name, &ad5593r_rw_ops); |
| 107 | } |
| 108 | |
| 109 | static int ad5593r_i2c_remove(struct i2c_client *i2c) |
| 110 | { |
| 111 | return ad5592r_remove(&i2c->dev); |
| 112 | } |
| 113 | |
| 114 | static const struct i2c_device_id ad5593r_i2c_ids[] = { |
| 115 | { .name = "ad5593r", }, |
| 116 | {}, |
| 117 | }; |
| 118 | MODULE_DEVICE_TABLE(i2c, ad5593r_i2c_ids); |
| 119 | |
| 120 | static const struct of_device_id ad5593r_of_match[] = { |
| 121 | { .compatible = "adi,ad5593r", }, |
| 122 | {}, |
| 123 | }; |
| 124 | MODULE_DEVICE_TABLE(of, ad5593r_of_match); |
| 125 | |
| 126 | static struct i2c_driver ad5593r_driver = { |
| 127 | .driver = { |
| 128 | .name = "ad5593r", |
| 129 | .of_match_table = of_match_ptr(ad5593r_of_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 AD5592R multi-channel converters"); |
| 139 | MODULE_LICENSE("GPL v2"); |