1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <hostIntf.h>
18 #include <hostIntf_priv.h>
19 #include <nanohubPacket.h>
20 #include <i2c.h>
21 
22 #define NANOHUB_I2C_SLAVE_ADDRESS     0x55
23 static uint32_t gBusId;
24 
hostIntfI2cPreambleCallback(void * cookie,size_t tx,size_t rx,int err)25 static void hostIntfI2cPreambleCallback(void *cookie, size_t tx, size_t rx, int err)
26 {
27 }
28 
hostIntfI2cRxCallback(void * cookie,size_t tx,size_t rx,int err)29 static void hostIntfI2cRxCallback(void *cookie, size_t tx, size_t rx, int err)
30 {
31     HostIntfCommCallbackF callback = cookie;
32     i2cSlaveTxPreamble(gBusId, NANOHUB_PREAMBLE_BYTE,
33             hostIntfI2cPreambleCallback, NULL);
34     callback(rx, err);
35 }
36 
hostIntfI2cTxCallback(void * cookie,size_t tx,size_t rx,int err)37 static void hostIntfI2cTxCallback(void *cookie, size_t tx, size_t rx, int err)
38 {
39     HostIntfCommCallbackF callback = cookie;
40     i2cSlaveTxPreamble(gBusId, NANOHUB_PREAMBLE_BYTE,
41             hostIntfI2cPreambleCallback, NULL);
42     callback(tx, err);
43 }
44 
hostIntfI2cRequest()45 static int hostIntfI2cRequest()
46 {
47     return i2cSlaveRequest(gBusId, NANOHUB_I2C_SLAVE_ADDRESS);
48 }
49 
hostIntfI2cRxPacket(void * rxBuf,size_t rxSize,HostIntfCommCallbackF callback)50 static int hostIntfI2cRxPacket(void *rxBuf, size_t rxSize,
51         HostIntfCommCallbackF callback)
52 {
53     i2cSlaveEnableRx(gBusId, rxBuf, rxSize, hostIntfI2cRxCallback,
54             callback);
55     return 0;
56 }
57 
hostIntfI2cTxPacket(const void * txBuf,size_t txSize,HostIntfCommCallbackF callback)58 static int hostIntfI2cTxPacket(const void *txBuf, size_t txSize,
59         HostIntfCommCallbackF callback)
60 {
61     return i2cSlaveTxPacket(gBusId, txBuf, txSize, hostIntfI2cTxCallback,
62             callback);
63 }
64 
hostIntfI2cRelease(void)65 static int hostIntfI2cRelease(void)
66 {
67     return i2cSlaveRelease(gBusId);
68 }
69 
70 static const struct HostIntfComm gI2cComm = {
71    .request = hostIntfI2cRequest,
72    .rxPacket = hostIntfI2cRxPacket,
73    .txPacket = hostIntfI2cTxPacket,
74    .release = hostIntfI2cRelease,
75 };
76 
hostIntfI2cInit(uint32_t busId)77 const struct HostIntfComm *hostIntfI2cInit(uint32_t busId)
78 {
79     gBusId = busId;
80     return &gI2cComm;
81 }
82