Daniel Scheller | a96e5ab | 2017-07-29 07:28:36 -0400 | [diff] [blame] | 1 | /* |
Daniel Scheller | 22e7438 | 2017-08-12 07:55:52 -0400 | [diff] [blame] | 2 | * ddbridge-i2c.c: Digital Devices bridge i2c driver |
Daniel Scheller | a96e5ab | 2017-07-29 07:28:36 -0400 | [diff] [blame] | 3 | * |
Daniel Scheller | 22e7438 | 2017-08-12 07:55:52 -0400 | [diff] [blame] | 4 | * Copyright (C) 2010-2017 Digital Devices GmbH |
| 5 | * Ralph Metzler <rjkm@metzlerbros.de> |
| 6 | * Marcus Metzler <mocm@metzlerbros.de> |
Daniel Scheller | a96e5ab | 2017-07-29 07:28:36 -0400 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * version 2 only, as published by the Free Software Foundation. |
| 11 | * |
Daniel Scheller | a96e5ab | 2017-07-29 07:28:36 -0400 | [diff] [blame] | 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
Daniel Scheller | a96e5ab | 2017-07-29 07:28:36 -0400 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #ifndef __DDBRIDGE_I2C_H__ |
| 20 | #define __DDBRIDGE_I2C_H__ |
| 21 | |
| 22 | #include <linux/i2c.h> |
| 23 | |
| 24 | #include "ddbridge.h" |
| 25 | |
| 26 | /******************************************************************************/ |
| 27 | |
| 28 | void ddb_i2c_release(struct ddb *dev); |
| 29 | int ddb_i2c_init(struct ddb *dev); |
| 30 | |
| 31 | /******************************************************************************/ |
| 32 | |
| 33 | static int __maybe_unused i2c_io(struct i2c_adapter *adapter, u8 adr, |
| 34 | u8 *wbuf, u32 wlen, u8 *rbuf, u32 rlen) |
| 35 | { |
| 36 | struct i2c_msg msgs[2] = { { .addr = adr, .flags = 0, |
| 37 | .buf = wbuf, .len = wlen }, |
| 38 | { .addr = adr, .flags = I2C_M_RD, |
| 39 | .buf = rbuf, .len = rlen } }; |
| 40 | |
| 41 | return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1; |
| 42 | } |
| 43 | |
| 44 | static int __maybe_unused i2c_write(struct i2c_adapter *adap, u8 adr, |
| 45 | u8 *data, int len) |
| 46 | { |
| 47 | struct i2c_msg msg = { .addr = adr, .flags = 0, |
| 48 | .buf = data, .len = len }; |
| 49 | |
| 50 | return (i2c_transfer(adap, &msg, 1) == 1) ? 0 : -1; |
| 51 | } |
| 52 | |
| 53 | static int __maybe_unused i2c_read(struct i2c_adapter *adapter, u8 adr, u8 *val) |
| 54 | { |
| 55 | struct i2c_msg msgs[1] = { { .addr = adr, .flags = I2C_M_RD, |
| 56 | .buf = val, .len = 1 } }; |
| 57 | |
| 58 | return (i2c_transfer(adapter, msgs, 1) == 1) ? 0 : -1; |
| 59 | } |
| 60 | |
| 61 | static int __maybe_unused i2c_read_regs(struct i2c_adapter *adapter, |
| 62 | u8 adr, u8 reg, u8 *val, u8 len) |
| 63 | { |
| 64 | struct i2c_msg msgs[2] = { { .addr = adr, .flags = 0, |
| 65 | .buf = ®, .len = 1 }, |
| 66 | { .addr = adr, .flags = I2C_M_RD, |
| 67 | .buf = val, .len = len } }; |
| 68 | |
| 69 | return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1; |
| 70 | } |
| 71 | |
Daniel Scheller | 22e7438 | 2017-08-12 07:55:52 -0400 | [diff] [blame] | 72 | static int __maybe_unused i2c_read_regs16(struct i2c_adapter *adapter, |
| 73 | u8 adr, u16 reg, u8 *val, u8 len) |
Daniel Scheller | a96e5ab | 2017-07-29 07:28:36 -0400 | [diff] [blame] | 74 | { |
| 75 | u8 msg[2] = { reg >> 8, reg & 0xff }; |
| 76 | struct i2c_msg msgs[2] = { { .addr = adr, .flags = 0, |
| 77 | .buf = msg, .len = 2 }, |
| 78 | { .addr = adr, .flags = I2C_M_RD, |
Daniel Scheller | 22e7438 | 2017-08-12 07:55:52 -0400 | [diff] [blame] | 79 | .buf = val, .len = len } }; |
Daniel Scheller | a96e5ab | 2017-07-29 07:28:36 -0400 | [diff] [blame] | 80 | |
| 81 | return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1; |
| 82 | } |
| 83 | |
Daniel Scheller | 22e7438 | 2017-08-12 07:55:52 -0400 | [diff] [blame] | 84 | static int __maybe_unused i2c_write_reg16(struct i2c_adapter *adap, |
| 85 | u8 adr, u16 reg, u8 val) |
| 86 | { |
| 87 | u8 msg[3] = { reg >> 8, reg & 0xff, val }; |
| 88 | |
| 89 | return i2c_write(adap, adr, msg, 3); |
| 90 | } |
| 91 | |
Daniel Scheller | a96e5ab | 2017-07-29 07:28:36 -0400 | [diff] [blame] | 92 | static int __maybe_unused i2c_write_reg(struct i2c_adapter *adap, |
| 93 | u8 adr, u8 reg, u8 val) |
| 94 | { |
| 95 | u8 msg[2] = { reg, val }; |
| 96 | |
| 97 | return i2c_write(adap, adr, msg, 2); |
| 98 | } |
| 99 | |
Daniel Scheller | 22e7438 | 2017-08-12 07:55:52 -0400 | [diff] [blame] | 100 | static int __maybe_unused i2c_read_reg16(struct i2c_adapter *adapter, |
| 101 | u8 adr, u16 reg, u8 *val) |
| 102 | { |
| 103 | return i2c_read_regs16(adapter, adr, reg, val, 1); |
| 104 | } |
| 105 | |
| 106 | static int __maybe_unused i2c_read_reg(struct i2c_adapter *adapter, |
| 107 | u8 adr, u8 reg, u8 *val) |
| 108 | { |
| 109 | return i2c_read_regs(adapter, adr, reg, val, 1); |
| 110 | } |
| 111 | |
Daniel Scheller | a96e5ab | 2017-07-29 07:28:36 -0400 | [diff] [blame] | 112 | #endif /* __DDBRIDGE_I2C_H__ */ |