Devin Heitmueller | ca3355a | 2010-07-04 18:42:11 -0300 | [diff] [blame] | 1 | /* |
Mauro Carvalho Chehab | 4a8a869 | 2014-01-16 12:05:15 -0300 | [diff] [blame] | 2 | I2C API, implementation depends on board specifics |
| 3 | |
Devin Heitmueller | ca3355a | 2010-07-04 18:42:11 -0300 | [diff] [blame] | 4 | Copyright (c), 2004-2005,2007-2010 Trident Microsystems, Inc. |
| 5 | All rights reserved. |
| 6 | |
| 7 | Redistribution and use in source and binary forms, with or without |
| 8 | modification, are permitted provided that the following conditions are met: |
| 9 | |
| 10 | * Redistributions of source code must retain the above copyright notice, |
| 11 | this list of conditions and the following disclaimer. |
| 12 | * Redistributions in binary form must reproduce the above copyright notice, |
| 13 | this list of conditions and the following disclaimer in the documentation |
| 14 | and/or other materials provided with the distribution. |
| 15 | * Neither the name of Trident Microsystems nor Hauppauge Computer Works |
| 16 | nor the names of its contributors may be used to endorse or promote |
| 17 | products derived from this software without specific prior written |
| 18 | permission. |
| 19 | |
| 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 24 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | POSSIBILITY OF SUCH DAMAGE. |
Devin Heitmueller | ca3355a | 2010-07-04 18:42:11 -0300 | [diff] [blame] | 31 | |
Mauro Carvalho Chehab | 4a8a869 | 2014-01-16 12:05:15 -0300 | [diff] [blame] | 32 | This module encapsulates I2C access.In some applications several devices |
| 33 | share one I2C bus. If these devices have the same I2C address some kind |
| 34 | off "switch" must be implemented to ensure error free communication with |
| 35 | one device. In case such a "switch" is used, the device ID can be used |
| 36 | to implement control over this "switch". |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 37 | */ |
| 38 | |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 39 | #ifndef __BSPI2C_H__ |
| 40 | #define __BSPI2C_H__ |
Mauro Carvalho Chehab | 5b223b3 | 2012-03-20 00:33:46 -0300 | [diff] [blame] | 41 | |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 42 | #include "bsp_types.h" |
| 43 | |
Mauro Carvalho Chehab | 5b223b3 | 2012-03-20 00:33:46 -0300 | [diff] [blame] | 44 | /* |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 45 | * This structure contains the I2C address, the device ID and a user_data pointer. |
| 46 | * The user_data pointer can be used for application specific purposes. |
Mauro Carvalho Chehab | 5b223b3 | 2012-03-20 00:33:46 -0300 | [diff] [blame] | 47 | */ |
| 48 | struct i2c_device_addr { |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 49 | u16 i2c_addr; /* The I2C address of the device. */ |
| 50 | u16 i2c_dev_id; /* The device identifier. */ |
| 51 | void *user_data; /* User data pointer */ |
Mauro Carvalho Chehab | 5b223b3 | 2012-03-20 00:33:46 -0300 | [diff] [blame] | 52 | }; |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 53 | |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * \def IS_I2C_10BIT( addr ) |
| 57 | * \brief Determine if I2C address 'addr' is a 10 bits address or not. |
| 58 | * \param addr The I2C address. |
| 59 | * \return int. |
| 60 | * \retval 0 if address is not a 10 bits I2C address. |
| 61 | * \retval 1 if address is a 10 bits I2C address. |
| 62 | */ |
| 63 | #define IS_I2C_10BIT(addr) \ |
| 64 | (((addr) & 0xF8) == 0xF0) |
| 65 | |
| 66 | /*------------------------------------------------------------------------------ |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 67 | Exported FUNCTIONS |
| 68 | ------------------------------------------------------------------------------*/ |
| 69 | |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 70 | /** |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 71 | * \fn drxbsp_i2c_init() |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 72 | * \brief Initialize I2C communication module. |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 73 | * \return drx_status_t Return status. |
Mauro Carvalho Chehab | 9482354 | 2014-01-17 10:18:16 -0300 | [diff] [blame] | 74 | * \retval 0 Initialization successful. |
| 75 | * \retval -EIO Initialization failed. |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 76 | */ |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 77 | drx_status_t drxbsp_i2c_init(void); |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 78 | |
| 79 | /** |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 80 | * \fn drxbsp_i2c_term() |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 81 | * \brief Terminate I2C communication module. |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 82 | * \return drx_status_t Return status. |
Mauro Carvalho Chehab | 9482354 | 2014-01-17 10:18:16 -0300 | [diff] [blame] | 83 | * \retval 0 Termination successful. |
| 84 | * \retval -EIO Termination failed. |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 85 | */ |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 86 | drx_status_t drxbsp_i2c_term(void); |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 87 | |
| 88 | /** |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 89 | * \fn drx_status_t drxbsp_i2c_write_read( struct i2c_device_addr *w_dev_addr, |
| 90 | * u16 w_count, |
Mauro Carvalho Chehab | 43a431e | 2012-03-20 00:49:45 -0300 | [diff] [blame] | 91 | * u8 *wData, |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 92 | * struct i2c_device_addr *r_dev_addr, |
| 93 | * u16 r_count, |
| 94 | * u8 *r_data) |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 95 | * \brief Read and/or write count bytes from I2C bus, store them in data[]. |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 96 | * \param w_dev_addr The device i2c address and the device ID to write to |
| 97 | * \param w_count The number of bytes to write |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 98 | * \param wData The array to write the data to |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 99 | * \param r_dev_addr The device i2c address and the device ID to read from |
| 100 | * \param r_count The number of bytes to read |
| 101 | * \param r_data The array to read the data from |
| 102 | * \return drx_status_t Return status. |
Mauro Carvalho Chehab | 9482354 | 2014-01-17 10:18:16 -0300 | [diff] [blame] | 103 | * \retval 0 Succes. |
| 104 | * \retval -EIO Failure. |
| 105 | * \retval -EINVAL Parameter 'wcount' is not zero but parameter |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 106 | * 'wdata' contains NULL. |
| 107 | * Idem for 'rcount' and 'rdata'. |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 108 | * Both w_dev_addr and r_dev_addr are NULL. |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 109 | * |
| 110 | * This function must implement an atomic write and/or read action on the I2C bus |
| 111 | * No other process may use the I2C bus when this function is executing. |
| 112 | * The critical section of this function runs from and including the I2C |
| 113 | * write, up to and including the I2C read action. |
| 114 | * |
| 115 | * The device ID can be useful if several devices share an I2C address. |
| 116 | * It can be used to control a "switch" on the I2C bus to the correct device. |
| 117 | */ |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 118 | drx_status_t drxbsp_i2c_write_read(struct i2c_device_addr *w_dev_addr, |
| 119 | u16 w_count, |
Mauro Carvalho Chehab | 4a8a869 | 2014-01-16 12:05:15 -0300 | [diff] [blame] | 120 | u8 *w_data, |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 121 | struct i2c_device_addr *r_dev_addr, |
| 122 | u16 r_count, u8 *r_data); |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 123 | |
| 124 | /** |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 125 | * \fn drxbsp_i2c_error_text() |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 126 | * \brief Returns a human readable error. |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 127 | * Counter part of numerical drx_i2c_error_g. |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 128 | * |
| 129 | * \return char* Pointer to human readable error text. |
| 130 | */ |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 131 | char *drxbsp_i2c_error_text(void); |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 132 | |
| 133 | /** |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 134 | * \var drx_i2c_error_g; |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 135 | * \brief I2C specific error codes, platform dependent. |
| 136 | */ |
Mauro Carvalho Chehab | 57afe2f | 2014-01-16 11:24:57 -0300 | [diff] [blame] | 137 | extern int drx_i2c_error_g; |
Devin Heitmueller | 38b2df9 | 2012-08-13 21:18:02 -0300 | [diff] [blame] | 138 | |
Mauro Carvalho Chehab | 443f18d | 2012-03-20 00:00:42 -0300 | [diff] [blame] | 139 | #endif /* __BSPI2C_H__ */ |