• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * ddbridge-i2c.h: Digital Devices bridge i2c driver
4  *
5  * Copyright (C) 2010-2017 Digital Devices GmbH
6  *                         Ralph Metzler <rjkm@metzlerbros.de>
7  *                         Marcus Metzler <mocm@metzlerbros.de>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 only, as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
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 
i2c_io(struct i2c_adapter * adapter,u8 adr,u8 * wbuf,u32 wlen,u8 * rbuf,u32 rlen)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 
i2c_write(struct i2c_adapter * adap,u8 adr,u8 * data,int len)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 
i2c_read(struct i2c_adapter * adapter,u8 adr,u8 * val)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 
i2c_read_regs(struct i2c_adapter * adapter,u8 adr,u8 reg,u8 * val,u8 len)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  = &reg, .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 
i2c_read_regs16(struct i2c_adapter * adapter,u8 adr,u16 reg,u8 * val,u8 len)72 static int __maybe_unused i2c_read_regs16(struct i2c_adapter *adapter,
73 					  u8 adr, u16 reg, u8 *val, u8 len)
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,
79 				     .buf  = val, .len   = len } };
80 
81 	return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1;
82 }
83 
i2c_write_reg16(struct i2c_adapter * adap,u8 adr,u16 reg,u8 val)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 
i2c_write_reg(struct i2c_adapter * adap,u8 adr,u8 reg,u8 val)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 
i2c_read_reg16(struct i2c_adapter * adapter,u8 adr,u16 reg,u8 * val)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 
i2c_read_reg(struct i2c_adapter * adapter,u8 adr,u8 reg,u8 * val)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 
112 #endif /* __DDBRIDGE_I2C_H__ */
113