aboutsummaryrefslogtreecommitdiff
path: root/sys/compat/linuxkpi/common/include/linux/i2c.h
blob: 0bb8b470edd7d50b56133933f375ea8e231ab58d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*-
 * Copyright (c) 2021 Beckhoff Automation GmbH & Co. KG
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#ifndef _LINUX_I2C_H_
#define	_LINUX_I2C_H_

#include <sys/types.h>
#include <sys/errno.h>
#include <sys/systm.h>

#include <linux/device.h>

#define	I2C_MAX_ADAPTER_NAME_LENGTH	32

#define	I2C_M_RD	0x0001
#define	I2C_M_NOSTART	0x0002
#define	I2C_M_STOP	0x0004

/* No need for us */
#define	I2C_FUNC_I2C			0
#define	I2C_FUNC_SMBUS_EMUL		0
#define	I2C_FUNC_SMBUS_READ_BLOCK_DATA	0
#define	I2C_FUNC_SMBUS_BLOCK_PROC_CALL	0
#define	I2C_FUNC_10BIT_ADDR		0

#define	I2C_CLASS_DDC	0x8
#define	I2C_CLASS_SPD	0x80

struct i2c_adapter {
	struct module *owner;
	unsigned int class;

	char name[I2C_MAX_ADAPTER_NAME_LENGTH];
	struct device dev;

	const struct i2c_lock_operations *lock_ops;
	const struct i2c_algorithm *algo;
	void *algo_data;

	int retries;
	void *data;
};

struct i2c_msg {
	uint16_t addr;
	uint16_t flags;
	uint16_t len;
	uint8_t *buf;
};

struct i2c_algorithm {
	int (*master_xfer)(struct i2c_adapter *, struct i2c_msg *, int);
	uint32_t (*functionality)(struct i2c_adapter *);
};

struct i2c_lock_operations {
	void (*lock_bus)(struct i2c_adapter *, unsigned int);
	int (*trylock_bus)(struct i2c_adapter *, unsigned int);
	void (*unlock_bus)(struct i2c_adapter *, unsigned int);
};

int lkpi_i2c_add_adapter(struct i2c_adapter *adapter);
int lkpi_i2c_del_adapter(struct i2c_adapter *adapter);

int lkpi_i2cbb_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int nmsgs);

#define	i2c_add_adapter(adapter)	lkpi_i2c_add_adapter(adapter)
#define	i2c_del_adapter(adapter)	lkpi_i2c_del_adapter(adapter)

#define	i2c_get_adapter(x)	NULL
#define	i2c_put_adapter(x)

static inline int
do_i2c_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int nmsgs)
{
	int ret, retries;

	retries = adapter->retries == 0 ? 1 : adapter->retries;
	for (; retries != 0; retries--) {
		if (adapter->algo->master_xfer != NULL)
			ret = adapter->algo->master_xfer(adapter, msgs, nmsgs);
		else
			ret = lkpi_i2cbb_transfer(adapter, msgs, nmsgs);
		if (ret != -EAGAIN)
			break;
	}

	return (ret);
}

static inline int
i2c_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int nmsgs)
{
	int ret;

	if (!adapter->algo)
		return (-EOPNOTSUPP);

	if (adapter->lock_ops)
		adapter->lock_ops->lock_bus(adapter, 0);

	ret = do_i2c_transfer(adapter, msgs, nmsgs);

	if (adapter->lock_ops)
		adapter->lock_ops->unlock_bus(adapter, 0);

	return (ret);
}

/* Unlocked version of i2c_transfer */
static inline int
__i2c_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int nmsgs)
{
	return (do_i2c_transfer(adapter, msgs, nmsgs));
}

static inline void
i2c_set_adapdata(struct i2c_adapter *adapter, void *data)
{
	adapter->data = data;
}

static inline void *
i2c_get_adapdata(struct i2c_adapter *adapter)
{
	return (adapter->data);
}

#endif	/* _LINUX_I2C_H_ */