1/* 2 * Copyright 2020, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5#ifndef _I2C_H_ 6#define _I2C_H_ 7 8 9#include <ACPI.h> 10#include <device_manager.h> 11#include <KernelExport.h> 12 13 14typedef uint16 i2c_addr; 15typedef enum { 16 I2C_OP_READ = 0, 17 I2C_OP_READ_STOP = 1, 18 I2C_OP_WRITE = 2, 19 I2C_OP_WRITE_STOP = 3, 20 I2C_OP_READ_BLOCK = 5, 21 I2C_OP_WRITE_BLOCK = 7, 22} i2c_op; 23 24 25#define IS_READ_OP(op) (((op) & I2C_OP_WRITE) == 0) 26#define IS_WRITE_OP(op) (((op) & I2C_OP_WRITE) != 0) 27#define IS_STOP_OP(op) (((op) & 1) != 0) 28#define IS_BLOCK_OP(op) (((op) & 4) != 0) 29 30 31// bus/device handle 32typedef void* i2c_bus; 33typedef void* i2c_device; 34 35 36// Device node 37 38// slave address (uint16) 39#define I2C_DEVICE_SLAVE_ADDR_ITEM "i2c/slave_addr" 40 41// node type 42#define I2C_DEVICE_TYPE_NAME "i2c/device/v1" 43 44// bus cookie, issued by i2c bus manager 45typedef void* i2c_bus; 46// device cookie, issued by i2c bus manager 47typedef void* i2c_device; 48 49// bus manager device interface for peripheral driver 50typedef struct { 51 driver_module_info info; 52 53 status_t (*exec_command)(i2c_device cookie, i2c_op op, 54 const void *cmdBuffer, size_t cmdLength, void* dataBuffer, 55 size_t dataLength); 56 status_t (*acquire_bus)(i2c_device cookie); 57 void (*release_bus)(i2c_device cookie); 58} i2c_device_interface; 59 60 61#define I2C_DEVICE_MODULE_NAME "bus_managers/i2c/device/driver_v1" 62 63 64// Bus node 65 66#define I2C_BUS_PATH_ID_ITEM "i2c/path_id" 67 68// node type 69#define I2C_BUS_TYPE_NAME "i2c/bus" 70 71// I2C bus node driver. 72// This interface can be used by peripheral drivers to access the 73// bus directly. 74typedef struct { 75 driver_module_info info; 76 77 status_t (*exec_command)(i2c_bus cookie, i2c_op op, i2c_addr slaveAddress, 78 const void *cmdBuffer, size_t cmdLength, void* dataBuffer, 79 size_t dataLength); 80 status_t (*acquire_bus)(i2c_bus cookie); 81 void (*release_bus)(i2c_bus cookie); 82} i2c_bus_interface; 83 84 85// name of I2C bus node driver 86#define I2C_BUS_MODULE_NAME "bus_managers/i2c/bus/driver_v1" 87 88 89 90// Interface for Controllers 91 92typedef struct { 93 driver_module_info info; 94 95 status_t (*register_device)(i2c_bus bus, i2c_addr slaveAddress, 96 char* hid, char** cid, acpi_handle acpiHandle); 97} i2c_for_controller_interface; 98 99#define I2C_FOR_CONTROLLER_MODULE_NAME "bus_managers/i2c/controller/driver_v1" 100 101 102// Controller Node 103 104// node type 105#define I2C_CONTROLLER_TYPE_NAME "bus/i2c/v1" 106// controller name (required, string) 107#define I2C_DESCRIPTION_CONTROLLER_NAME "controller_name" 108 109typedef void *i2c_bus_cookie; 110typedef void *i2c_intr_cookie; 111 112 113// Bus manager interface used by I2C controller drivers. 114typedef struct { 115 driver_module_info info; 116 117 void (*set_i2c_bus)(i2c_bus_cookie cookie, i2c_bus bus); 118 119 status_t (*exec_command)(i2c_bus_cookie cookie, i2c_op op, 120 i2c_addr slaveAddress, const void *cmdBuffer, size_t cmdLength, 121 void* dataBuffer, size_t dataLength); 122 123 status_t (*scan_bus)(i2c_bus_cookie cookie); 124 125 status_t (*acquire_bus)(i2c_bus_cookie cookie); 126 void (*release_bus)(i2c_bus_cookie cookie); 127 128 status_t (*install_interrupt_handler)(i2c_bus_cookie cookie, 129 i2c_intr_cookie intrCookie, interrupt_handler handler, void* data); 130 131 status_t (*uninstall_interrupt_handler)(i2c_bus_cookie cookie, 132 i2c_intr_cookie intrCookie); 133} i2c_sim_interface; 134 135 136// Devfs entry for raw bus access. 137// used by i2c diag tool 138 139enum { 140 I2CEXEC = B_DEVICE_OP_CODES_END + 1, 141}; 142 143 144typedef struct i2c_ioctl_exec 145{ 146 i2c_op op; 147 i2c_addr addr; 148 const void* cmdBuffer; 149 size_t cmdLength; 150 void* buffer; 151 size_t bufferLength; 152} i2c_ioctl_exec; 153 154#define I2C_EXEC_MAX_BUFFER_LENGTH 32 155 156#endif /* _I2C_H_ */ 157