12#ifndef ZEPHYR_INCLUDE_DRIVERS_SMBUS_H_
13#define ZEPHYR_INCLUDE_DRIVERS_SMBUS_H_
55#define SMBUS_CMD_QUICK 0b000
78#define SMBUS_CMD_BYTE 0b001
104#define SMBUS_CMD_BYTE_DATA 0b010
132#define SMBUS_CMD_WORD_DATA 0b011
151#define SMBUS_CMD_PROC_CALL 0b100
182#define SMBUS_CMD_BLOCK 0b101
202#define SMBUS_CMD_BLOCK_PROC 0b111
206#define SMBUS_BLOCK_BYTES_MAX 32
216#define SMBUS_MODE_CONTROLLER BIT(0)
219#define SMBUS_MODE_PEC BIT(1)
222#define SMBUS_MODE_HOST_NOTIFY BIT(2)
225#define SMBUS_MODE_SMBALERT BIT(3)
242#define SMBUS_ADDRESS_ARA 0x0c
262#define SMBUS_MSG_RW_MASK BIT(0)
318#define SMBUS_DT_SPEC_GET(node_id) \
320 .bus = DEVICE_DT_GET(DT_BUS(node_id)), \
321 .addr = DT_REG_ADDR(node_id) \
332#define SMBUS_DT_SPEC_INST_GET(inst) SMBUS_DT_SPEC_GET(DT_DRV_INST(inst))
341typedef int (*smbus_api_configure_t)(
const struct device *dev,
343typedef int (*smbus_api_get_config_t)(
const struct device *dev,
345typedef int (*smbus_api_quick_t)(
const struct device *dev,
347typedef int (*smbus_api_byte_write_t)(
const struct device *dev,
349typedef int (*smbus_api_byte_read_t)(
const struct device *dev,
351typedef int (*smbus_api_byte_data_write_t)(
const struct device *dev,
354typedef int (*smbus_api_byte_data_read_t)(
const struct device *dev,
357typedef int (*smbus_api_word_data_write_t)(
const struct device *dev,
360typedef int (*smbus_api_word_data_read_t)(
const struct device *dev,
363typedef int (*smbus_api_pcall_t)(
const struct device *dev,
366typedef int (*smbus_api_block_write_t)(
const struct device *dev,
369typedef int (*smbus_api_block_read_t)(
const struct device *dev,
372typedef int (*smbus_api_block_pcall_t)(
const struct device *dev,
376typedef int (*smbus_api_smbalert_cb_t)(
const struct device *dev,
378typedef int (*smbus_api_host_notify_cb_t)(
const struct device *dev,
381__subsystem
struct smbus_driver_api {
382 smbus_api_configure_t configure;
383 smbus_api_get_config_t get_config;
405#if defined(CONFIG_SMBUS_STATS) || defined(__DOXYGEN__)
423struct smbus_device_state {
425 struct stats_smbus stats;
431#define Z_SMBUS_DEVICE_STATE_DEFINE(node_id, dev_name) \
432 static struct smbus_device_state Z_DEVICE_STATE_NAME(dev_name) \
433 __attribute__((__section__(".z_devstate")));
441#define Z_SMBUS_INIT_FN(dev_name, init_fn) \
443 UTIL_CAT(dev_name, _init)(const struct device *dev) \
445 struct smbus_device_state *state = \
446 CONTAINER_OF(dev->state, \
447 struct smbus_device_state, \
449 stats_init(&state->stats.s_hdr, STATS_SIZE_32, 4, \
450 STATS_NAME_INIT_PARMS(smbus)); \
451 stats_register(dev->name, &(state->stats.s_hdr)); \
452 return init_fn(dev); \
468 struct smbus_device_state *
state =
504#define SMBUS_DEVICE_DT_DEFINE(node_id, init_fn, pm_device, \
505 data_ptr, cfg_ptr, level, prio, \
507 Z_SMBUS_DEVICE_STATE_DEFINE(node_id, \
508 Z_DEVICE_DT_DEV_NAME(node_id)); \
509 Z_SMBUS_INIT_FN(Z_DEVICE_DT_DEV_NAME(node_id), init_fn) \
510 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_NAME(node_id), \
511 DEVICE_DT_NAME(node_id), \
512 &UTIL_CAT(Z_DEVICE_DT_DEV_NAME(node_id), _init),\
514 data_ptr, cfg_ptr, level, prio, \
516 &(Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_NAME \
517 (node_id)).devstate), \
530#define SMBUS_DEVICE_DT_DEFINE(node_id, init_fn, pm_device, \
531 data_ptr, cfg_ptr, level, prio, \
533 DEVICE_DT_DEFINE(node_id, &init_fn, pm_device, \
534 data_ptr, cfg_ptr, level, prio, \
535 api_ptr, __VA_ARGS__)
548#define SMBUS_DEVICE_DT_INST_DEFINE(inst, ...) \
549 SMBUS_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
563static inline int z_impl_smbus_configure(
const struct device *dev,
566 const struct smbus_driver_api *api =
567 (
const struct smbus_driver_api *)dev->
api;
569 return api->configure(dev, dev_config);
594static inline int z_impl_smbus_get_config(
const struct device *dev,
597 const struct smbus_driver_api *api =
598 (
const struct smbus_driver_api *)dev->
api;
600 if (api->get_config == NULL) {
604 return api->get_config(dev, dev_config);
621 const struct smbus_driver_api *api =
622 (
const struct smbus_driver_api *)dev->
api;
624 if (api->smbus_smbalert_set_cb == NULL) {
628 return api->smbus_smbalert_set_cb(dev, cb);
645static inline int z_impl_smbus_smbalert_remove_cb(
const struct device *dev,
648 const struct smbus_driver_api *api =
649 (
const struct smbus_driver_api *)dev->
api;
651 if (api->smbus_smbalert_remove_cb == NULL) {
655 return api->smbus_smbalert_remove_cb(dev, cb);
672 const struct smbus_driver_api *api =
673 (
const struct smbus_driver_api *)dev->
api;
675 if (api->smbus_host_notify_set_cb == NULL) {
679 return api->smbus_host_notify_set_cb(dev, cb);
696static inline int z_impl_smbus_host_notify_remove_cb(
const struct device *dev,
699 const struct smbus_driver_api *api =
700 (
const struct smbus_driver_api *)dev->
api;
702 if (api->smbus_host_notify_remove_cb == NULL) {
706 return api->smbus_host_notify_remove_cb(dev, cb);
728static inline int z_impl_smbus_quick(
const struct device *dev,
uint16_t addr,
731 const struct smbus_driver_api *api =
732 (
const struct smbus_driver_api *)dev->
api;
734 if (api->smbus_quick == NULL) {
742 return api->smbus_quick(dev, addr, direction);
763static inline int z_impl_smbus_byte_write(
const struct device *dev,
766 const struct smbus_driver_api *api =
767 (
const struct smbus_driver_api *)dev->
api;
769 if (api->smbus_byte_write == NULL) {
773 return api->smbus_byte_write(dev, addr,
byte);
794static inline int z_impl_smbus_byte_read(
const struct device *dev,
797 const struct smbus_driver_api *api =
798 (
const struct smbus_driver_api *)dev->
api;
800 if (api->smbus_byte_read == NULL) {
804 return api->smbus_byte_read(dev, addr,
byte);
826static inline int z_impl_smbus_byte_data_write(
const struct device *dev,
830 const struct smbus_driver_api *api =
831 (
const struct smbus_driver_api *)dev->
api;
833 if (api->smbus_byte_data_write == NULL) {
837 return api->smbus_byte_data_write(dev, addr,
cmd,
byte);
859static inline int z_impl_smbus_byte_data_read(
const struct device *dev,
863 const struct smbus_driver_api *api =
864 (
const struct smbus_driver_api *)dev->
api;
866 if (api->smbus_byte_data_read == NULL) {
870 return api->smbus_byte_data_read(dev, addr,
cmd,
byte);
892static inline int z_impl_smbus_word_data_write(
const struct device *dev,
896 const struct smbus_driver_api *api =
897 (
const struct smbus_driver_api *)dev->
api;
899 if (api->smbus_word_data_write == NULL) {
903 return api->smbus_word_data_write(dev, addr,
cmd, word);
925static inline int z_impl_smbus_word_data_read(
const struct device *dev,
929 const struct smbus_driver_api *api =
930 (
const struct smbus_driver_api *)dev->
api;
932 if (api->smbus_word_data_read == NULL) {
936 return api->smbus_word_data_read(dev, addr,
cmd, word);
960static inline int z_impl_smbus_pcall(
const struct device *dev,
964 const struct smbus_driver_api *api =
965 (
const struct smbus_driver_api *)dev->
api;
967 if (api->smbus_pcall == NULL) {
971 return api->smbus_pcall(dev, addr,
cmd, send_word, recv_word);
994static inline int z_impl_smbus_block_write(
const struct device *dev,
998 const struct smbus_driver_api *api =
999 (
const struct smbus_driver_api *)dev->
api;
1001 if (api->smbus_block_write == NULL) {
1009 return api->smbus_block_write(dev, addr,
cmd, count, buf);
1032static inline int z_impl_smbus_block_read(
const struct device *dev,
1036 const struct smbus_driver_api *api =
1037 (
const struct smbus_driver_api *)dev->
api;
1039 if (api->smbus_block_read == NULL) {
1043 return api->smbus_block_read(dev, addr,
cmd, count, buf);
1071static inline int z_impl_smbus_block_pcall(
const struct device *dev,
1076 const struct smbus_driver_api *api =
1077 (
const struct smbus_driver_api *)dev->
api;
1079 if (api->smbus_block_pcall == NULL) {
1083 return api->smbus_block_pcall(dev, addr,
cmd, snd_count, snd_buf,
1084 rcv_count, rcv_buf);
1095#include <zephyr/syscalls/smbus.h>
static ssize_t recv(int sock, void *buf, size_t max_len, int flags)
POSIX wrapper for zsock_recv.
Definition: socket.h:922
static void cmd(uint32_t command)
Execute a display list command by co-processor engine.
Definition: ft8xx_reference_api.h:153
struct _snode sys_snode_t
Single-linked list node structure.
Definition: slist.h:39
static void smbus_xfer_stats(const struct device *dev, uint8_t sent, uint8_t recv)
Updates the SMBus stats.
Definition: smbus.h:465
int smbus_byte_data_read(const struct device *dev, uint16_t addr, uint8_t cmd, uint8_t *byte)
Perform SMBus Byte Data Read operation.
int smbus_pcall(const struct device *dev, uint16_t addr, uint8_t cmd, uint16_t send_word, uint16_t *recv_word)
Perform SMBus Process Call operation.
static int smbus_host_notify_set_cb(const struct device *dev, struct smbus_callback *cb)
Add Host Notify callback for a SMBus host controller.
Definition: smbus.h:669
int smbus_configure(const struct device *dev, uint32_t dev_config)
Configure operation of a SMBus host controller.
int smbus_block_write(const struct device *dev, uint16_t addr, uint8_t cmd, uint8_t count, uint8_t *buf)
Perform SMBus Block Write operation.
#define SMBUS_BLOCK_BYTES_MAX
Maximum number of bytes in SMBus Block protocol.
Definition: smbus.h:206
int smbus_block_read(const struct device *dev, uint16_t addr, uint8_t cmd, uint8_t *count, uint8_t *buf)
Perform SMBus Block Read operation.
int smbus_block_pcall(const struct device *dev, uint16_t addr, uint8_t cmd, uint8_t snd_count, uint8_t *snd_buf, uint8_t *rcv_count, uint8_t *rcv_buf)
Perform SMBus Block Process Call operation.
void(* smbus_callback_handler_t)(const struct device *dev, struct smbus_callback *cb, uint8_t addr)
Define SMBus callback handler function signature.
Definition: smbus.h:274
int smbus_word_data_read(const struct device *dev, uint16_t addr, uint8_t cmd, uint16_t *word)
Perform SMBus Word Data Read operation.
int smbus_byte_write(const struct device *dev, uint16_t addr, uint8_t byte)
Perform SMBus Byte Write operation.
int smbus_get_config(const struct device *dev, uint32_t *dev_config)
Get configuration of a SMBus host controller.
int smbus_host_notify_remove_cb(const struct device *dev, struct smbus_callback *cb)
Remove Host Notify callback from a SMBus host controller.
int smbus_quick(const struct device *dev, uint16_t addr, enum smbus_direction direction)
Perform SMBus Quick operation.
int smbus_smbalert_remove_cb(const struct device *dev, struct smbus_callback *cb)
Remove SMBUSALERT callback from a SMBus host controller.
smbus_direction
SMBus read / write direction.
Definition: smbus.h:252
int smbus_byte_read(const struct device *dev, uint16_t addr, uint8_t *byte)
Perform SMBus Byte Read operation.
int smbus_byte_data_write(const struct device *dev, uint16_t addr, uint8_t cmd, uint8_t byte)
Perform SMBus Byte Data Write operation.
int smbus_word_data_write(const struct device *dev, uint16_t addr, uint8_t cmd, uint16_t word)
Perform SMBus Word Data Write operation.
static int smbus_smbalert_set_cb(const struct device *dev, struct smbus_callback *cb)
Add SMBUSALERT callback for a SMBus host controller.
Definition: smbus.h:618
@ SMBUS_MSG_WRITE
Write a message to SMBus peripheral.
Definition: smbus.h:254
@ SMBUS_MSG_READ
Read a message from SMBus peripheral.
Definition: smbus.h:256
#define CONTAINER_OF(ptr, type, field)
Get a pointer to a structure containing the element.
Definition: util.h:268
#define EINVAL
Invalid argument.
Definition: errno.h:60
#define ENOSYS
Function not implemented.
Definition: errno.h:82
state
Definition: parser_state.h:29
#define STATS_NAME_END(name__)
Definition: stats.h:391
#define STATS_NAME(name__, entry__)
Definition: stats.h:390
#define STATS_SECT_END
Ends a stats group struct definition.
Definition: stats.h:89
#define STATS_SECT_ENTRY32(var__)
Definition: stats.h:359
#define STATS_INC(group__, var__)
Definition: stats.h:364
#define STATS_NAME_START(name__)
Definition: stats.h:389
#define STATS_INCN(group__, var__, n__)
Definition: stats.h:363
#define STATS_SECT_START(group__)
Definition: stats.h:354
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
Runtime device dynamic structure (in RAM) per driver instance.
Definition: device.h:371
Runtime device structure (in ROM) per driver instance.
Definition: device.h:403
const void * api
Address of the API structure exposed by the device instance.
Definition: device.h:409
struct device_state * state
Address of the common device state.
Definition: device.h:411
SMBus callback structure.
Definition: smbus.h:287
smbus_callback_handler_t handler
Actual callback function being called when relevant.
Definition: smbus.h:292
uint8_t addr
Peripheral device address.
Definition: smbus.h:295
sys_snode_t node
This should be used in driver for a callback list management.
Definition: smbus.h:289
Complete SMBus DT information.
Definition: smbus.h:301
uint16_t addr
Address of the SMBus peripheral device.
Definition: smbus.h:305
const struct device * bus
SMBus bus.
Definition: smbus.h:303