aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/mediatek/files-5.10/drivers/net/phy/rtk/rtl8367c/i2c.c
blob: 17a5f3785bd4f54c71bb6067f88e10c88d45c470 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
 * Copyright (C) 2013 Realtek Semiconductor Corp.
 * All Rights Reserved.
 *
 * Unless you and Realtek execute a separate written software license
 * agreement governing use of this software, this software is licensed
 * to you under the terms of the GNU General Public License version 2,
 * available at https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 *
 * $Revision: 63932 $
 * $Date: 2015-12-08 14:06:29 +0800 (周二, 08 十二月 2015) $
 *
 * Purpose : RTK switch high-level API for RTL8367/RTL8367C
 * Feature : Here is a list of all functions and variables in i2c module.
 *
 */

#include <rtk_switch.h>
#include <rtk_error.h>
#include <port.h>
#include <string.h>
#include <rtl8367c_reg.h>

#include <rtl8367c_asicdrv_i2c.h>
#include <rtk_switch.h>
#include <rtl8367c_asicdrv.h>
#include <rtk_types.h>
#include <i2c.h>


static rtk_I2C_16bit_mode_t rtk_i2c_mode = I2C_LSB_16BIT_MODE;


/* Function Name:
 *      rtk_i2c_init
 * Description:
 *      I2C smart function initialization.
 * Input:
 *      None
 * Output:
 *      None
 * Return:
 *      RT_ERR_OK           - OK
 *      RT_ERR_FAILED       - Failed
 * Note:
 *      This API is used to initialize EEE status.
 *      need used GPIO pins
 *      OpenDrain and clock
 */
rtk_api_ret_t rtk_i2c_init(void)
{
    rtk_uint32 retVal;
  switch_chip_t ChipID;
  /* probe switch */
  if((retVal = rtk_switch_probe(&ChipID)) != RT_ERR_OK)
      return retVal;

  if( ChipID == CHIP_RTL8370B )
  {
   /*set GPIO8, GPIO9, OpenDrain as I2C, clock = 252KHZ   */
      if((retVal = rtl8367c_setAsicReg(RTL8367C_REG_M_I2C_SYS_CTL, 0x5c3f)) != RT_ERR_OK)
        return retVal;
  }
  else
      return RT_ERR_FAILED;
  return  RT_ERR_OK;
}


/* Function Name:
 *      rtk_i2c_mode_set
 * Description:
 *      Set I2C data byte-order.
 * Input:
 *      i2cmode - byte-order mode
 * Output:
 *      None
 * Return:
 *      RT_ERR_OK           - OK
 *      RT_ERR_FAILED       - Failed
 *      RT_ERR_INPUT        - Invalid input parameter.
 * Note:
 *      This API can set I2c traffic's byte-order .
 */
rtk_api_ret_t rtk_i2c_mode_set( rtk_I2C_16bit_mode_t i2cmode )
{
    if(i2cmode >= I2C_Mode_END)
    {
        return RT_ERR_INPUT;
    }
    else if(i2cmode == I2C_70B_LSB_16BIT_MODE)
    {
        rtk_i2c_mode = I2C_70B_LSB_16BIT_MODE;

        return RT_ERR_OK;
    }
    else if( i2cmode == I2C_LSB_16BIT_MODE)
    {
        rtk_i2c_mode = I2C_LSB_16BIT_MODE;
        return RT_ERR_OK;
    }
    else
        return RT_ERR_FAILED;

    return RT_ERR_OK;
}


/* Function Name:
 *      rtk_i2c_mode_get
 * Description:
 *      Get i2c traffic byte-order setting.
 * Input:
 *      None
 * Output:
 *      pI2cMode - i2c byte-order
 * Return:
 *      RT_ERR_OK               - OK
 *      RT_ERR_FAILED           - Failed
 *      RT_ERR_NULL_POINTER     - input parameter is null pointer
 * Note:
 *      The API can get i2c traffic byte-order setting.
 */
rtk_api_ret_t rtk_i2c_mode_get( rtk_I2C_16bit_mode_t * pI2cMode)
{
    /* Check initialization state */
    RTK_CHK_INIT_STATE();
    if(NULL == pI2cMode)
        return RT_ERR_NULL_POINTER;
    if(rtk_i2c_mode == I2C_70B_LSB_16BIT_MODE)
        *pI2cMode = 1;
    else if ((rtk_i2c_mode == I2C_LSB_16BIT_MODE))
        *pI2cMode = 0;
    else
        return RT_ERR_FAILED;
    return RT_ERR_OK;
}


/* Function Name:
 *      rtk_i2c_gpioPinGroup_set
 * Description:
 *      Set i2c SDA & SCL used GPIO pins group.
 * Input:
 *      pins_group - GPIO pins group
 * Output:
 *      None
 * Return:
 *      RT_ERR_OK               - OK
 *      RT_ERR_FAILED           - Failed
 *      RT_ERR_INPUT        - Invalid input parameter.
 * Note:
 *      The API can set i2c used gpio pins group.
 *      There are three group pins could be used
 */
rtk_api_ret_t rtk_i2c_gpioPinGroup_set( rtk_I2C_gpio_pin_t pins_group )
{
    rtk_uint32 retVal;


    if( ( pins_group > I2C_GPIO_PIN_END )|| ( pins_group < I2C_GPIO_PIN_8_9) )
        return RT_ERR_INPUT;

    if( (retVal = rtl8367c_setAsicI2CGpioPinGroup(pins_group) ) != RT_ERR_OK )
        return retVal ;

    return RT_ERR_OK;
}

/* Function Name:
 *      rtk_i2c_gpioPinGroup_get
 * Description:
 *      Get i2c SDA & SCL used GPIO pins group.
 * Input:
 *      None
 * Output:
 *      pPins_group - GPIO pins group
 * Return:
 *      RT_ERR_OK               - OK
 *      RT_ERR_NULL_POINTER     - input parameter is null pointer
 * Note:
 *      The API can get i2c used gpio pins group.
 *      There are three group pins could be used
 */
rtk_api_ret_t rtk_i2c_gpioPinGroup_get( rtk_I2C_gpio_pin_t * pPins_group )
{
    rtk_uint32 retVal;
    /* Check initialization state */
    RTK_CHK_INIT_STATE();

    if(NULL == pPins_group)
        return RT_ERR_NULL_POINTER;
    if( (retVal = rtl8367c_getAsicI2CGpioPinGroup(pPins_group) ) != RT_ERR_OK )
        return retVal;

    return RT_ERR_OK;
}

/* Function Name:
 *      rtk_i2c_data_read
 * Description:
 *      read i2c slave device register.
 * Input:
 *      deviceAddr   -   access Slave device address
 *      slaveRegAddr -   access Slave register address
 * Output:
 *      pRegData     -   read data
 * Return:
 *      RT_ERR_OK               - OK
 *      RT_ERR_NULL_POINTER     - input parameter is null pointer
 * Note:
 *      The API can access i2c slave and read i2c slave device register.
 */
rtk_api_ret_t rtk_i2c_data_read(rtk_uint8 deviceAddr, rtk_uint32 slaveRegAddr, rtk_uint32 *pRegData)
{
     rtk_uint32 retVal, counter=0;
     rtk_uint8 controlByte_W, controlByte_R;
     rtk_uint8 slaveRegAddr_L, slaveRegAddr_H = 0x0, temp;
     rtk_uint8 regData_L, regData_H;

   /* control byte :deviceAddress + W,  deviceAddress + R   */
    controlByte_W = (rtk_uint8)(deviceAddr << 1) ;
    controlByte_R = (rtk_uint8)(controlByte_W | 0x1);

    slaveRegAddr_L = (rtk_uint8) (slaveRegAddr & 0x00FF) ;
    slaveRegAddr_H = (rtk_uint8) (slaveRegAddr >>8) ;

    if( rtk_i2c_mode == I2C_70B_LSB_16BIT_MODE)
    {
        temp = slaveRegAddr_L ;
        slaveRegAddr_L = slaveRegAddr_H;
        slaveRegAddr_H = temp;
    }


  /*check bus state: idle*/
  for(counter = 3000; counter>0; counter--)
  {
    if ( (retVal = rtl8367c_setAsicI2C_checkBusIdle() ) == RT_ERR_OK)
         break;
  }
  if( counter ==0 )
      return retVal; /*i2c is busy*/

   /*tx Start cmd*/
   if( (retVal = rtl8367c_setAsicI2CStartCmd() ) != RT_ERR_OK )
       return retVal ;


  /*tx control _W*/
   if( (retVal = rtl8367c_setAsicI2CTxOneCharCmd(controlByte_W))!= RT_ERR_OK )
      return retVal ;


  /*check if RX ack from slave*/
   if( (retVal = rtl8367c_setAsicI2CcheckRxAck()) != RT_ERR_OK )
        return retVal;

    /* tx slave buffer address low 8 bits */
   if( (retVal = rtl8367c_setAsicI2CTxOneCharCmd(slaveRegAddr_L))!= RT_ERR_OK )
         return retVal  ;

   /*check if RX ack from slave*/
   if( (retVal = rtl8367c_setAsicI2CcheckRxAck()) != RT_ERR_OK )
        return retVal;



        /* tx slave buffer address high 8 bits */
   if( (retVal = rtl8367c_setAsicI2CTxOneCharCmd(slaveRegAddr_H))!= RT_ERR_OK )
         return retVal  ;


   /*check if RX ack from slave*/
   if( (retVal = rtl8367c_setAsicI2CcheckRxAck()) != RT_ERR_OK )
        return retVal;


   /*tx Start cmd*/
   if( (retVal = rtl8367c_setAsicI2CStartCmd() ) != RT_ERR_OK )
       return retVal ;

      /*tx control _R*/
   if( (retVal = rtl8367c_setAsicI2CTxOneCharCmd(controlByte_R))!= RT_ERR_OK )
       return retVal ;


  /*check if RX ack from slave*/
   if( (retVal = rtl8367c_setAsicI2CcheckRxAck()) != RT_ERR_OK )
        return retVal;


    /* rx low 8bit data*/
   if( ( retVal = rtl8367c_setAsicI2CRxOneCharCmd( &regData_L) ) != RT_ERR_OK )
        return retVal;



    /* tx ack to slave, keep receive */
    if( (retVal = rtl8367c_setAsicI2CTxAckCmd()) != RT_ERR_OK )
        return retVal;

     /* rx high 8bit data*/
    if( ( retVal = rtl8367c_setAsicI2CRxOneCharCmd( &regData_H) ) != RT_ERR_OK )
        return retVal;



    /* tx Noack to slave, Stop receive */
     if( (retVal = rtl8367c_setAsicI2CTxNoAckCmd()) != RT_ERR_OK )
        return retVal;


    /*tx Stop cmd */
    if( (retVal = rtl8367c_setAsicI2CStopCmd()) != RT_ERR_OK )
        return retVal;

    *pRegData = (regData_H << 8) | regData_L;

    return RT_ERR_OK;
}

/* Function Name:
 *      rtk_i2c_data_write
 * Description:
 *      write data to i2c slave device register
 * Input:
 *      deviceAddr   -   access Slave device address
 *      slaveRegAddr -   access Slave register address
 *      regData      -   data to set
 * Output:
 *      None
 * Return:
 *      RT_ERR_OK               - OK
 * Note:
 *      The API can access i2c slave and setting i2c slave device register.
 */
rtk_api_ret_t rtk_i2c_data_write(rtk_uint8 deviceAddr, rtk_uint32 slaveRegAddr, rtk_uint32 regData)
{
     rtk_uint32 retVal,counter;
     rtk_uint8 controlByte_W;
     rtk_uint8 slaveRegAddr_L, slaveRegAddr_H = 0x0, temp;
     rtk_uint8 regData_L, regData_H;

  /* control byte :deviceAddress + W    */
    controlByte_W = (rtk_uint8)(deviceAddr<< 1) ;

    slaveRegAddr_L = (rtk_uint8) (slaveRegAddr & 0x00FF) ;
    slaveRegAddr_H = (rtk_uint8) (slaveRegAddr >>8) ;

    regData_H   = (rtk_uint8) (regData>> 8);
    regData_L   = (rtk_uint8) (regData & 0x00FF);

    if( rtk_i2c_mode == I2C_70B_LSB_16BIT_MODE)
    {
        temp = slaveRegAddr_L ;
        slaveRegAddr_L = slaveRegAddr_H;
        slaveRegAddr_H = temp;
    }


  /*check bus state: idle*/
  for(counter = 3000; counter>0; counter--)
  {
    if ( (retVal = rtl8367c_setAsicI2C_checkBusIdle() ) == RT_ERR_OK)
        break;
  }

  if( counter ==0 )
      return retVal; /*i2c is busy*/


   /*tx Start cmd*/
   if( (retVal = rtl8367c_setAsicI2CStartCmd() ) != RT_ERR_OK )
       return retVal ;


  /*tx control _W*/
   if( (retVal = rtl8367c_setAsicI2CTxOneCharCmd(controlByte_W))!= RT_ERR_OK )
      return retVal ;


  /*check if RX ack from slave*/
   if( (retVal = rtl8367c_setAsicI2CcheckRxAck()) != RT_ERR_OK )
        return retVal;


    /* tx slave buffer address low 8 bits */
   if( (retVal = rtl8367c_setAsicI2CTxOneCharCmd(slaveRegAddr_L))!= RT_ERR_OK )
        return retVal;


   /*check if RX ack from slave*/
   if( (retVal = rtl8367c_setAsicI2CcheckRxAck()) != RT_ERR_OK )
        return retVal;


   /* tx slave buffer address high 8 bits */
   if( (retVal = rtl8367c_setAsicI2CTxOneCharCmd(slaveRegAddr_H))!= RT_ERR_OK )
        return retVal;


   /*check if RX ack from slave*/
   if( (retVal = rtl8367c_setAsicI2CcheckRxAck()) != RT_ERR_OK )
        return retVal;


     /*tx Datavlue LSB*/
   if( (retVal = rtl8367c_setAsicI2CTxOneCharCmd(regData_L))!= RT_ERR_OK )
        return retVal;


   /*check if RX ack from slave*/
   if( (retVal = rtl8367c_setAsicI2CcheckRxAck()) != RT_ERR_OK )
        return retVal;


   /*tx Datavlue MSB*/
   if( (retVal = rtl8367c_setAsicI2CTxOneCharCmd(regData_H))!= RT_ERR_OK )
        return retVal;


   /*check if RX ack from slave*/
   if( (retVal = rtl8367c_setAsicI2CcheckRxAck()) != RT_ERR_OK )
        return retVal;


    /*tx Stop cmd */
    if( (retVal = rtl8367c_setAsicI2CStopCmd()) != RT_ERR_OK )
        return retVal;

    return RT_ERR_OK;
}