blob: 7ed220506c05e5e95c28005b24ece2b304dc88b5 [file] [log] [blame]
Daniel Schellera96e5ab2017-07-29 07:28:36 -04001/*
Daniel Scheller22e74382017-08-12 07:55:52 -04002 * ddbridge-i2c.c: Digital Devices bridge i2c driver
Daniel Schellera96e5ab2017-07-29 07:28:36 -04003 *
Daniel Scheller22e74382017-08-12 07:55:52 -04004 * Copyright (C) 2010-2017 Digital Devices GmbH
5 * Ralph Metzler <rjkm@metzlerbros.de>
6 * Marcus Metzler <mocm@metzlerbros.de>
Daniel Schellera96e5ab2017-07-29 07:28:36 -04007 *
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 Schellera96e5ab2017-07-29 07:28:36 -040012 * 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 Schellera96e5ab2017-07-29 07:28:36 -040017 */
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
28void ddb_i2c_release(struct ddb *dev);
29int ddb_i2c_init(struct ddb *dev);
30
31/******************************************************************************/
32
33static 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
44static 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
53static 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
61static 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
Daniel Scheller22e74382017-08-12 07:55:52 -040072static int __maybe_unused i2c_read_regs16(struct i2c_adapter *adapter,
73 u8 adr, u16 reg, u8 *val, u8 len)
Daniel Schellera96e5ab2017-07-29 07:28:36 -040074{
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 Scheller22e74382017-08-12 07:55:52 -040079 .buf = val, .len = len } };
Daniel Schellera96e5ab2017-07-29 07:28:36 -040080
81 return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1;
82}
83
Daniel Scheller22e74382017-08-12 07:55:52 -040084static 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 Schellera96e5ab2017-07-29 07:28:36 -040092static 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 Scheller22e74382017-08-12 07:55:52 -0400100static 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
106static 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 Schellera96e5ab2017-07-29 07:28:36 -0400112#endif /* __DDBRIDGE_I2C_H__ */