source: rtems/cpukit/include/dev/i2c/i2c.h

Last change on this file was bcef89f2, checked in by Sebastian Huber <sebastian.huber@…>, on 05/19/23 at 06:18:25

Update company name

The embedded brains GmbH & Co. KG is the legal successor of embedded
brains GmbH.

  • Property mode set to 100644
File size: 11.7 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/**
4 * @file
5 *
6 * @brief Inter-Integrated Circuit (I2C) Driver API
7 *
8 * @ingroup I2C
9 */
10
11/*
12 * Copyright (C) 2014, 2017 embedded brains GmbH & Co. KG
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36#ifndef _DEV_I2C_I2C_H
37#define _DEV_I2C_I2C_H
38
39#include <linux/i2c.h>
40#include <linux/i2c-dev.h>
41
42#include <rtems.h>
43#include <rtems/seterr.h>
44#include <rtems/thread.h>
45
46#include <sys/ioctl.h>
47#include <sys/stat.h>
48
49#ifdef __cplusplus
50extern "C" {
51#endif /* __cplusplus */
52
53typedef struct i2c_msg i2c_msg;
54
55typedef struct i2c_bus i2c_bus;
56
57typedef struct i2c_dev i2c_dev;
58
59typedef struct i2c_rdwr_ioctl_data i2c_rdwr_ioctl_data;
60
61/**
62 * @defgroup I2C Inter-Integrated Circuit (I2C) Driver
63 *
64 * @ingroup RTEMSDeviceDrivers
65 *
66 * @brief Inter-Integrated Circuit (I2C) bus and device driver support.
67 *
68 * @{
69 */
70
71/**
72 * @defgroup I2CBus I2C Bus Driver
73 *
74 * @ingroup I2C
75 *
76 * @{
77 */
78
79/**
80 * @name I2C IO Control Commands
81 *
82 * @{
83 */
84
85/**
86 * @brief Obtains the bus.
87 *
88 * This command has no argument.
89 */
90#define I2C_BUS_OBTAIN 0x800
91
92/**
93 * @brief Releases the bus.
94 *
95 * This command has no argument.
96 */
97#define I2C_BUS_RELEASE 0x801
98
99/**
100 * @brief Gets the bus control.
101 *
102 * The argument type is a pointer to i2c_bus pointer.
103 */
104#define I2C_BUS_GET_CONTROL 0x802
105
106/**
107 * @brief Sets the bus clock in Hz.
108 *
109 * The argument type is unsigned long.
110 */
111#define I2C_BUS_SET_CLOCK 0x803
112
113/** @} */
114
115/**
116 * @brief Default I2C bus clock in Hz.
117 */
118#define I2C_BUS_CLOCK_DEFAULT 100000
119
120/**
121 * @brief I2C bus control.
122 */
123struct i2c_bus {
124  /**
125   * @brief Transfers I2C messages.
126   *
127   * @param[in] bus The bus control.
128   * @param[in] msgs The messages to transfer.
129   * @param[in] msg_count The count of messages to transfer.  It must be
130   * positive.
131   *
132   * @retval 0 Successful operation.
133   * @retval negative Negative error number in case of an error.
134   */
135  int (*transfer)(i2c_bus *bus, i2c_msg *msgs, uint32_t msg_count);
136
137  /**
138   * @brief Sets the bus clock.
139   *
140   * @param[in] bus The bus control.
141   * @param[in] clock The desired bus clock in Hz.
142   *
143   * @retval 0 Successful operation.
144   * @retval negative Negative error number in case of an error.
145   */
146  int (*set_clock)(i2c_bus *bus, unsigned long clock);
147
148  /**
149   * @brief Destroys the bus.
150   *
151   * @param[in] bus The bus control.
152   */
153  void (*destroy)(i2c_bus *bus);
154
155  /**
156   * @brief Mutex to protect the bus access.
157   */
158  rtems_recursive_mutex mutex;
159
160  /**
161   * @brief Default slave device address.
162   */
163  uint16_t default_address;
164
165  /**
166   * @brief Use 10-bit addresses.
167   */
168  bool ten_bit_address;
169
170  /**
171   * @brief Use SMBus PEC.
172   */
173  bool use_pec;
174
175  /**
176   * @brief Transfer retry count.
177   */
178  unsigned long retries;
179
180  /**
181   * @brief Transaction timeout in ticks.
182   */
183  rtems_interval timeout;
184
185  /**
186   * @brief Controller functionality.
187   */
188  unsigned long functionality;
189};
190
191/**
192 * @brief Initializes a bus control.
193 *
194 * After a sucessful initialization the bus control must be destroyed via
195 * i2c_bus_destroy().  A registered bus control will be automatically destroyed
196 * in case the device file is unlinked.  Make sure to call i2c_bus_destroy() in
197 * a custom destruction handler.
198 *
199 * @param[in] bus The bus control.
200 *
201 * @retval 0 Successful operation.
202 * @retval -1 An error occurred.  The errno is set to indicate the error.
203 *
204 * @see i2c_bus_register()
205 */
206int i2c_bus_init(i2c_bus *bus);
207
208/**
209 * @brief Allocates a bus control from the heap and initializes it.
210 *
211 * After a sucessful allocation and initialization the bus control must be
212 * destroyed via i2c_bus_destroy_and_free().  A registered bus control will be
213 * automatically destroyed in case the device file is unlinked.  Make sure to
214 * call i2c_bus_destroy_and_free() in a custom destruction handler.
215 *
216 * @param[in] size The size of the bus control.  This enables the addition of
217 * bus controller specific data to the base bus control.  The bus control is
218 * zero initialized.
219 *
220 * @retval non-NULL The new bus control.
221 * @retval NULL An error occurred.  The errno is set to indicate the error.
222 *
223 * @see i2c_bus_register()
224 */
225i2c_bus *i2c_bus_alloc_and_init(size_t size);
226
227/**
228 * @brief Destroys a bus control.
229 *
230 * @param[in] bus The bus control.
231 */
232void i2c_bus_destroy(i2c_bus *bus);
233
234/**
235 * @brief Destroys a bus control and frees its memory.
236 *
237 * @param[in] bus The bus control.
238 */
239void i2c_bus_destroy_and_free(i2c_bus *bus);
240
241/**
242 * @brief Registers a bus control.
243 *
244 * This function claims ownership of the bus control regardless if the
245 * registration is successful or not.
246 *
247 * @param[in] bus The bus control.
248 * @param[in] bus_path The path to the bus device file.
249 *
250 * @retval 0 Successful operation.
251 * @retval -1 An error occurred.  The errno is set to indicate the error.
252 */
253int i2c_bus_register(
254  i2c_bus *bus,
255  const char *bus_path
256);
257
258/**
259 * @brief Try to obtain the bus.
260 *
261 * @param[in] bus The bus control.
262 *
263 * @retval 0 Successful operation.
264 * @retval EBUSY if mutex is already locked.
265 */
266int i2c_bus_try_obtain(i2c_bus *bus);
267
268/**
269 * @brief Obtains the bus.
270 *
271 * @param[in] bus The bus control.
272 */
273void i2c_bus_obtain(i2c_bus *bus);
274
275/**
276 * @brief Releases the bus.
277 *
278 * @param[in] bus The bus control.
279 */
280void i2c_bus_release(i2c_bus *bus);
281
282/**
283 * @brief Transfers I2C messages.
284 *
285 * The bus is obtained before the transfer and released afterwards. This is the
286 * same like calling @ref i2c_bus_do_transfer with flags set to 0.
287 *
288 * @param[in] bus The bus control.
289 * @param[in] msgs The messages to transfer.
290 * @param[in] msg_count The count of messages to transfer.  It must be
291 * positive.
292 *
293 * @retval 0 Successful operation.
294 * @retval negative Negative error number in case of an error.
295 */
296int i2c_bus_transfer(i2c_bus *bus, i2c_msg *msgs, uint32_t msg_count);
297
298/**
299 * @brief Transfers I2C messages with optional flags.
300 *
301 * The bus is obtained before the transfer and released afterwards. If the flag
302 * I2C_BUS_NOBLOCK is set and the bus is already obtained, nothing will be
303 * transfered and the function returns with an -EAGAIN.
304 *
305 * @param[in] bus The bus control.
306 * @param[in] msgs The messages to transfer.
307 * @param[in] msg_count The count of messages to transfer.  It must be
308 * positive.
309 * @param[in] flags Options for the whole transfer.
310 *
311 * @retval 0 Successful operation.
312 * @retval -EAGAIN if @ref I2C_BUS_NOBLOCK is set and the bus is already
313 * obtained.
314 * @retval negative Negative error number in case of an error.
315 */
316int i2c_bus_do_transfer(
317  i2c_bus *bus,
318  i2c_msg *msgs,
319  uint32_t msg_count,
320  uint32_t flags
321);
322
323/**
324 * @brief I2C bus transfer flag to indicate that the task should not block if
325 * the bus is busy on a new transfer.
326 */
327#define I2C_BUS_NOBLOCK (1u << 0)
328
329/** @} */
330
331/**
332 * @defgroup I2CDevice I2C Device Driver
333 *
334 * @ingroup I2C
335 *
336 * @{
337 */
338
339/**
340 * @brief Base number for device IO control commands.
341 */
342#define I2C_DEV_IO_CONTROL 0x900
343
344/**
345 * @brief I2C slave device control.
346 */
347struct i2c_dev {
348  /**
349   * @brief Reads from the device.
350   *
351   * @retval non-negative Bytes transferred from device.
352   * @retval negative Negative error number in case of an error.
353   */
354  ssize_t (*read)(i2c_dev *dev, void *buf, size_t n, off_t offset);
355
356  /**
357   * @brief Writes to the device.
358   *
359   * @retval non-negative Bytes transferred to device.
360   * @retval negative Negative error number in case of an error.
361   */
362  ssize_t (*write)(i2c_dev *dev, const void *buf, size_t n, off_t offset);
363
364  /**
365   * @brief Device IO control.
366   *
367   * @retval 0 Successful operation.
368   * @retval negative Negative error number in case of an error.
369   */
370  int (*ioctl)(i2c_dev *dev, ioctl_command_t command, void *arg);
371
372  /**
373   * @brief Gets the file size.
374   */
375  off_t (*get_size)(i2c_dev *dev);
376
377  /**
378   * @brief Gets the file block size.
379   */
380  blksize_t (*get_block_size)(i2c_dev *dev);
381
382  /**
383   * @brief Destroys the device.
384   */
385  void (*destroy)(i2c_dev *dev);
386
387  /**
388   * @brief The bus control.
389   */
390  i2c_bus *bus;
391
392  /**
393   * @brief The device address.
394   */
395  uint16_t address;
396
397  /**
398   * @brief File descriptor of the bus.
399   *
400   * This prevents destruction of the bus since we hold a reference to it with
401   * this.
402   */
403  int bus_fd;
404};
405
406
407/**
408 * @brief Initializes a device control.
409 *
410 * After a sucessful initialization the device control must be destroyed via
411 * i2c_dev_destroy().  A registered device control will be automatically
412 * destroyed in case the device file is unlinked.  Make sure to call
413 * i2c_dev_destroy_and_free() in a custom destruction handler.
414 *
415 * @param[in] device The device control.
416 * @param[in] bus_path The path to the bus device file.
417 * @param[in] address The address of the device.
418 *
419 * @retval 0 Successful operation.
420 * @retval -1 An error occurred.  The errno is set to indicate the error.
421 *
422 * @see i2c_dev_register()
423 */
424int i2c_dev_init(i2c_dev *dev, const char *bus_path, uint16_t address);
425
426/**
427 * @brief Allocates a device control from the heap and initializes it.
428 *
429 * After a sucessful allocation and initialization the device control must be
430 * destroyed via i2c_dev_destroy_and_free().  A registered device control will
431 * be automatically destroyed in case the device file is unlinked.  Make sure
432 * to call i2c_dev_destroy_and_free() in a custom destruction handler.
433 *
434 * @param[in] size The size of the device control.  This enables the addition
435 * of device specific data to the base device control.  The device control is
436 * zero initialized.
437 * @param[in] bus_path The path to the bus device file.
438 * @param[in] address The address of the device.
439 *
440 * @retval non-NULL The new device control.
441 * @retval NULL An error occurred.  The errno is set to indicate the error.
442 *
443 * @see i2c_dev_register()
444 */
445i2c_dev *i2c_dev_alloc_and_init(
446  size_t size,
447  const char *bus_path,
448  uint16_t address
449);
450
451/**
452 * @brief Destroys a device control.
453 *
454 * @param[in] dev The device control.
455 */
456void i2c_dev_destroy(i2c_dev *dev);
457
458/**
459 * @brief Destroys a device control and frees its memory.
460 *
461 * @param[in] dev The device control.
462 */
463void i2c_dev_destroy_and_free(i2c_dev *dev);
464
465/**
466 * @brief Registers a device control.
467 *
468 * This function claims ownership of the device control regardless if the
469 * registration is successful or not.
470 *
471 * @param[in] dev The dev control.
472 * @param[in] dev_path The path to the device file of the device.
473 *
474 * @retval 0 Successful operation.
475 * @retval -1 An error occurred.  The errno is set to indicate the error.
476 */
477int i2c_dev_register(
478  i2c_dev *dev,
479  const char *dev_path
480);
481
482/** @} */  /* end of i2c device driver */
483
484/** @} */
485
486#ifdef __cplusplus
487}
488#endif /* __cplusplus */
489
490#endif /* _DEV_I2C_I2C_H */
Note: See TracBrowser for help on using the repository browser.