blob: b5e77105b5b584705b0b9dab33157b657391ab00 [file] [log] [blame]
Angel Pons32859fc2020-04-02 23:48:27 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Nico Huber0f2dd1e2017-08-01 14:02:40 +02002
3#ifndef _DEVICE_I2C_BUS_H_
4#define _DEVICE_I2C_BUS_H_
5
Elyes HAOUAS5817c562020-07-12 09:03:22 +02006#include <stddef.h>
Nico Huber0f2dd1e2017-08-01 14:02:40 +02007#include <stdint.h>
8#include <device/i2c.h>
9#include <device/device.h>
10
11/* I2C bus operation for ramstage drivers */
12struct i2c_bus_operations {
Nico Huber58173862017-08-01 17:09:35 +020013 int (*transfer)(struct device *, const struct i2c_msg *, size_t count);
Nico Huber0f2dd1e2017-08-01 14:02:40 +020014};
15
Nico Huber58173862017-08-01 17:09:35 +020016/*
17 * Returns the first upstream facing link whose bus implements either
18 * `i2c_bus_operations` *or* `smbus_bus_operations`.
19 *
20 * If not NULL, guarantees that `->dev`, `->dev->ops` and either
21 * `->dev->ops->ops_i2c_bus` or `->dev->ops->ops_smbus_bus` are
22 * not NULL.
23 */
24struct bus *i2c_link(struct device *);
Nico Huber0f2dd1e2017-08-01 14:02:40 +020025
Nico Huber58173862017-08-01 17:09:35 +020026/*
27 * Shorthand for `i2c_link(dev)->dev`.
28 *
29 * Returns NULL if i2c_link(dev) returns NULL.
30 */
Aaron Durbin439cee92018-01-22 21:24:35 -070031static inline DEVTREE_CONST struct device *i2c_busdev(struct device *dev)
Nico Huber58173862017-08-01 17:09:35 +020032{
33 struct bus *const link = i2c_link(dev);
34 return link ? link->dev : NULL;
35}
36
37/*
38 * Slave driver interface functions. These will look for the next
39 * `i2c_bus_operations` *or* `smbus_bus_operations` and perform the
40 * respective transfers.
41 *
42 * The interface is limited to what current slave drivers demand.
43 * Extend as required.
44 *
45 * All functions return a negative `enum cb_err` value on error.
46 * Either CB_ERR, CB_ERR_ARG or any CB_I2C_* (cf. include/types.h).
47 */
48
49/*
50 * Reads one byte.
51 * Compatible to smbus_recv_byte().
52 *
53 * Returns the read byte on success, negative `enum cb_err` value on error.
54 */
Aaron Durbin439cee92018-01-22 21:24:35 -070055int i2c_dev_readb(struct device *);
Nico Huber58173862017-08-01 17:09:35 +020056
57/*
58 * Writes the byte `val`.
59 * Compatible to smbus_send_byte().
60 *
61 * Returns 0 on success, negative `enum cb_err` value on error.
62 */
Aaron Durbin439cee92018-01-22 21:24:35 -070063int i2c_dev_writeb(struct device *, uint8_t val);
Nico Huber58173862017-08-01 17:09:35 +020064
65/*
66 * Sends the register offset `off` and reads one byte.
67 * Compatible to smbus_read_byte().
68 *
69 * Returns the read byte on success, negative `enum cb_err` value on error.
70 */
Aaron Durbin439cee92018-01-22 21:24:35 -070071int i2c_dev_readb_at(struct device *, uint8_t off);
Nico Huber58173862017-08-01 17:09:35 +020072
73/*
74 * Sends the register offset `off` followed by the byte `val`.
75 * Compatible to smbus_write_byte().
76 *
77 * Returns 0 on success, negative `enum cb_err` value on error.
78 */
Aaron Durbin439cee92018-01-22 21:24:35 -070079int i2c_dev_writeb_at(struct device *, uint8_t off, uint8_t val);
Nico Huber0f2dd1e2017-08-01 14:02:40 +020080
Nico Huber9734af62017-11-01 13:15:33 +010081/*
82 * Sends the 16-bit register offset `off` and reads `len` bytes into `buf`.
83 *
84 * Returns the number of bytes read on success, negative `enum cb_err`
85 * value on error.
86 */
87int i2c_dev_read_at16(struct device *, uint8_t *buf, size_t len, uint16_t off);
88
Nico Huber0f2dd1e2017-08-01 14:02:40 +020089#endif /* _DEVICE_I2C_BUS_H_ */