summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/nvrm/dispatch/nvrm_i2c_dispatch.c
blob: 6e2672dff89614a4ddf316cd288e7c6e1f99ffd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * Copyright (c) 2009 NVIDIA Corporation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the NVIDIA Corporation nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

#define NV_IDL_IS_DISPATCH

#include "nvcommon.h"
#include "nvos.h"
#include "nvassert.h"
#include "nvreftrack.h"
#include "nvidlcmd.h"
#include "nvrm_i2c.h"

#define OFFSET( s, e ) (NvU32)(void *)(&(((s*)0)->e))


typedef struct NvRmI2cTransaction_in_t
{
    NvU32 package_;
    NvU32 function_;
    NvRmI2cHandle hI2c;
    NvU32 I2cPinMap;
    NvU32 WaitTimeoutInMilliSeconds;
    NvU32 ClockSpeedKHz;
    NvU8  * Data;
    NvU32 DataLen;
    NvRmI2cTransactionInfo  * Transaction;
    NvU32 NumOfTransactions;
} NV_ALIGN(4) NvRmI2cTransaction_in;

typedef struct NvRmI2cTransaction_inout_t
{
    NvU32 dummy_;
} NV_ALIGN(4) NvRmI2cTransaction_inout;

typedef struct NvRmI2cTransaction_out_t
{
    NvError ret_;
} NV_ALIGN(4) NvRmI2cTransaction_out;

typedef struct NvRmI2cTransaction_params_t
{
    NvRmI2cTransaction_in in;
    NvRmI2cTransaction_inout inout;
    NvRmI2cTransaction_out out;
} NvRmI2cTransaction_params;

typedef struct NvRmI2cClose_in_t
{
    NvU32 package_;
    NvU32 function_;
    NvRmI2cHandle hI2c;
} NV_ALIGN(4) NvRmI2cClose_in;

typedef struct NvRmI2cClose_inout_t
{
    NvU32 dummy_;
} NV_ALIGN(4) NvRmI2cClose_inout;

typedef struct NvRmI2cClose_out_t
{
    NvU32 dummy_;
} NV_ALIGN(4) NvRmI2cClose_out;

typedef struct NvRmI2cClose_params_t
{
    NvRmI2cClose_in in;
    NvRmI2cClose_inout inout;
    NvRmI2cClose_out out;
} NvRmI2cClose_params;

typedef struct NvRmI2cOpen_in_t
{
    NvU32 package_;
    NvU32 function_;
    NvRmDeviceHandle hDevice;
    NvU32 IoModule;
    NvU32 instance;
} NV_ALIGN(4) NvRmI2cOpen_in;

typedef struct NvRmI2cOpen_inout_t
{
    NvU32 dummy_;
} NV_ALIGN(4) NvRmI2cOpen_inout;

typedef struct NvRmI2cOpen_out_t
{
    NvError ret_;
    NvRmI2cHandle phI2c;
} NV_ALIGN(4) NvRmI2cOpen_out;

typedef struct NvRmI2cOpen_params_t
{
    NvRmI2cOpen_in in;
    NvRmI2cOpen_inout inout;
    NvRmI2cOpen_out out;
} NvRmI2cOpen_params;

static NvError NvRmI2cTransaction_dispatch_( void *InBuffer, NvU32 InSize, void *OutBuffer, NvU32 OutSize, NvDispatchCtx* Ctx )
{
    NvError err_ = NvSuccess;
    NvRmI2cTransaction_in *p_in;
    NvRmI2cTransaction_out *p_out;
    NvU8  *Data = NULL;
    NvRmI2cTransactionInfo *Transaction = NULL;

    p_in = (NvRmI2cTransaction_in *)InBuffer;
    p_out = (NvRmI2cTransaction_out *)((NvU8 *)OutBuffer + OFFSET(NvRmI2cTransaction_params, out) - OFFSET(NvRmI2cTransaction_params, inout));

    if( p_in->DataLen && p_in->Data )
    {
        Data = (NvU8  *)NvOsAlloc( p_in->DataLen * sizeof( NvU8  ) );
        if( !Data )
        {
            err_ = NvError_InsufficientMemory;
            goto clean;
        }
        if( p_in->Data )
        {
            err_ = NvOsCopyIn( Data, p_in->Data, p_in->DataLen * sizeof( NvU8  ) );
            if( err_ != NvSuccess )
            {
                err_ = NvError_BadParameter;
                goto clean;
            }
        }
    }
    if( p_in->NumOfTransactions && p_in->Transaction )
    {
        Transaction = (NvRmI2cTransactionInfo  *)NvOsAlloc( p_in->NumOfTransactions * sizeof( NvRmI2cTransactionInfo  ) );
        if( !Transaction )
        {
            err_ = NvError_InsufficientMemory;
            goto clean;
        }
        if( p_in->Transaction )
        {
            err_ = NvOsCopyIn( Transaction, p_in->Transaction, p_in->NumOfTransactions * sizeof( NvRmI2cTransactionInfo  ) );
            if( err_ != NvSuccess )
            {
                err_ = NvError_BadParameter;
                goto clean;
            }
        }
    }

    p_out->ret_ = NvRmI2cTransaction( p_in->hI2c, p_in->I2cPinMap, p_in->WaitTimeoutInMilliSeconds, p_in->ClockSpeedKHz, Data, p_in->DataLen, Transaction, p_in->NumOfTransactions );

    if(p_in->Data && Data)
    {
        err_ = NvOsCopyOut( p_in->Data, Data, p_in->DataLen * sizeof( NvU8  ) );
        if( err_ != NvSuccess )
        {
            err_ = NvError_BadParameter;
        }
    }
clean:
    NvOsFree( Data );
    NvOsFree( Transaction );
    return err_;
}

static NvError NvRmI2cClose_dispatch_( void *InBuffer, NvU32 InSize, void *OutBuffer, NvU32 OutSize, NvDispatchCtx* Ctx )
{
    NvError err_ = NvSuccess;
    NvRmI2cClose_in *p_in;

    p_in = (NvRmI2cClose_in *)InBuffer;


    NvRmI2cClose( p_in->hI2c );

    return err_;
}

static NvError NvRmI2cOpen_dispatch_( void *InBuffer, NvU32 InSize, void *OutBuffer, NvU32 OutSize, NvDispatchCtx* Ctx )
{
    NvError err_ = NvSuccess;
    NvRmI2cOpen_in *p_in;
    NvRmI2cOpen_out *p_out;

    p_in = (NvRmI2cOpen_in *)InBuffer;
    p_out = (NvRmI2cOpen_out *)((NvU8 *)OutBuffer + OFFSET(NvRmI2cOpen_params, out) - OFFSET(NvRmI2cOpen_params, inout));


    p_out->ret_ = NvRmI2cOpen( p_in->hDevice, p_in->IoModule, p_in->instance, &p_out->phI2c );

    return err_;
}

NvError nvrm_i2c_Dispatch( NvU32 function, void *InBuffer, NvU32 InSize, void *OutBuffer, NvU32 OutSize, NvDispatchCtx* Ctx );
NvError nvrm_i2c_Dispatch( NvU32 function, void *InBuffer, NvU32 InSize, void *OutBuffer, NvU32 OutSize, NvDispatchCtx* Ctx )
{
    NvError err_ = NvSuccess;

    switch( function ) {
    case 2:
        err_ = NvRmI2cTransaction_dispatch_( InBuffer, InSize, OutBuffer, OutSize, Ctx );
        break;
    case 1:
        err_ = NvRmI2cClose_dispatch_( InBuffer, InSize, OutBuffer, OutSize, Ctx );
        break;
    case 0:
        err_ = NvRmI2cOpen_dispatch_( InBuffer, InSize, OutBuffer, OutSize, Ctx );
        break;
    default:
        err_ = NvError_BadParameter;
        break;
    }

    return err_;
}