opnsense-src/sys/compat/linuxkpi/common/include/linux/i2c.h
Emmanuel Vadot 1961a14a47 linuxkpi: Add i2c support
Add i2c support to linuxkpi. This is needed by drm-kmod.
For every i2c_adapter added by i2c_add_adapter we add a child to the
device named "lkpi_iic". This child handle the conversion between
Linux i2c_msgs to FreeBSD iic_msgs.
For every i2c_adapter added by i2c_bit_add_bus we add a child to the
device named "lkpi_iicbb". This child handle the conversion between
Linux i2c_msgs to FreeBSD iic_msgs.
With the help of iic(4), this expose the i2c controller to userspace
allowing a user to query DDC information from a monitor.
e.g.: i2c -f /dev/iic0 -a 0x28 -c 128 -d r
will query the standard EDID from the monitor if plugged.

The bitbang part (lkpi_iicbb) isn't tested at all for now as I don't have
compatible hardware (all my hardware have native i2c controller).

Tested on:	Intel (SandyBridge, Skylake, ApolloLake)
Tested on:	AMD (Picasso, Polaris (amd64 and arm64))

MFC after:	1 month
Reviewed by:	hselasky
Sponsored by:	Beckhoff Automation GmbH & Co. KG
Differential Revision:	https://reviews.freebsd.org/D33053
2022-01-25 16:15:39 +01:00

152 lines
4 KiB
C

/*-
* 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_ */