i2c: i2c_hal: remove the unnecessary debug message
[profile/mobile/platform/kernel/u-boot-tm1.git] / drivers / i2c / i2c_hal.c
1 /******************************************************************************
2  ** File Name:      I2C_drv.c                                                *
3  ** Author:         liuhao                                                   *
4  ** DATE:           06/28/2010                                                *
5  ** Copyright:      2010 Spreatrum, Incoporated. All Rights Reserved.         *
6  ** Description:    This file define the hal layer of I2C device.      *
7  ******************************************************************************
8
9  ******************************************************************************
10  **                        Edit History                                       *
11  ** ------------------------------------------------------------------------- *
12  ** DATE           NAME             DESCRIPTION                               *
13  ** 06/28/2010     liuhao     Create.                                   *
14  ******************************************************************************/
15
16 /**---------------------------------------------------------------------------*
17  **                         Dependencies                                      *
18  **---------------------------------------------------------------------------*/
19 #include "asm/arch/sci_types.h"
20 //#include "os_api.h"
21 //#include "chip_plf_export.h"
22 #include "sp8830_i2c.h"
23 #include "sc8830_i2c_cfg.h"
24
25 /**---------------------------------------------------------------------------*
26  **                         Debugging Flag                                    *
27  **---------------------------------------------------------------------------*/
28
29 /**---------------------------------------------------------------------------*
30  **                         Compiler Flag                                     *
31  **---------------------------------------------------------------------------*/
32 #ifdef   __cplusplus
33 extern   "C"
34 {
35 #endif
36
37 /**---------------------------------------------------------------------------*
38  **                            Macro Define
39  **---------------------------------------------------------------------------*/
40 typedef struct
41 {
42     //SCI_MUTEX_PTR mutex;
43     uint32 current_port;
44     uint32 current_freq;
45     uint32 reference;
46     I2C_PHY_FUN *phy_fun;
47 } I2C_BUS;
48
49 /*
50 #define I2C_GetMutex(mutex)  {\
51         uint32 ret = SCI_SUCCESS;\
52         if( NULL == mutex )\
53         {\
54             return ERR_I2C_NO_MUTEX;\
55         }\
56         if(SCI_InThreadContext())\
57         {\
58             ret = SCI_GetMutex(mutex, SCI_INVALID_BLOCK_ID != SCI_IdentifyThread() ? SCI_WAIT_FOREVER : 0);\
59         }\
60         SCI_ASSERT( ret == SCI_SUCCESS );//assert verified\
61     }
62
63 #define I2C_PutMutex(mutex)  {\
64         uint32 ret = SCI_SUCCESS;\
65         if( NULL == mutex )\
66         {\
67             return ERR_I2C_NO_MUTEX;\
68         }\
69         if(SCI_InThreadContext())\
70         {\
71             ret = SCI_PutMutex( mutex );\
72         }\
73         SCI_ASSERT( ret == SCI_SUCCESS );//assert verified\
74     }
75 */
76 /**---------------------------------------------------------------------------*
77  **                            Local Variables
78  **---------------------------------------------------------------------------*/
79 LOCAL I2C_DEV __i2c_dev[I2C_BUS_MAX *I2C_DEV_MAX];
80
81 /**---------------------------------------------------------------------------*
82  **                            Global Variables
83  **---------------------------------------------------------------------------*/
84 I2C_BUS __i2c_bus[I2C_BUS_MAX];
85 extern const I2C_PHY_CFG __i2c_phy_cfg[I2C_ID_MAX];
86
87 /**---------------------------------------------------------------------------*
88  **                      Function  Definitions
89  **---------------------------------------------------------------------------*/
90 /*********************************************************************************************************
91 ** Function name:
92 ** Descriptions:
93 ** input parameters:
94 **
95 **
96 **
97 ** output parameters:
98 ** Returned value:
99 *********************************************************************************************************/
100 LOCAL uint32 I2C_Bus_CFG (uint32 logic_id)
101 {
102     uint32 bus_id = __i2c_phy_cfg[logic_id].phy_id;
103     /*config bus method*/
104     __i2c_bus[bus_id].phy_fun = __i2c_phy_cfg[logic_id].phy_fun;
105     return 0;
106 }
107 /*********************************************************************************************************
108 ** Function name:
109 ** Descriptions:
110 ** input parameters:
111 **
112 **
113 **
114 ** output parameters:
115 ** Returned value:
116 *********************************************************************************************************/
117 LOCAL uint32 I2C_Bus_Init (uint32 logic_id, uint32 freq, uint32 port_id)
118 {
119     uint32 bus_id = __i2c_phy_cfg[logic_id].phy_id;
120     /*config bus property*/
121     __i2c_bus[bus_id].current_freq = freq;
122     __i2c_bus[bus_id].current_port = port_id;
123     //__i2c_bus[bus_id].mutex = SCI_CreateMutex ("I2C SYNC MUTEX", SCI_INHERIT);
124     //SCI_PASSERT ( (NULL!= __i2c_bus[bus_id].mutex), ("I2C Great MUTEX fail!"));/*assert verified*/
125     /*config bus method*/
126     I2C_Bus_CFG (logic_id);
127     return 0;
128 }
129
130 /*********************************************************************************************************
131 ** Function name:
132 ** Descriptions:
133 ** input parameters:
134 **
135 **
136 **
137 ** output parameters:
138 ** Returned value:
139 *********************************************************************************************************/
140 LOCAL uint32 I2C_Bus_Exit (uint32 bus_id)
141 {
142     uint32 status_val = 0;
143     __i2c_bus[bus_id].reference =  0;
144
145 /*
146     if (NULL != __i2c_bus[bus_id].mutex)
147     {
148         status_val = SCI_DeleteMutex (__i2c_bus[bus_id].mutex);
149         //SCI_PASSERT((0 != status_val),("I2C delete MUTEX fail!"));
150         __i2c_bus[bus_id].mutex = NULL;
151     }
152 */
153     /*release the bus method*/
154     __i2c_bus[bus_id].phy_fun = NULL;
155     return 0;
156 }
157
158 /*********************************************************************************************************
159 ** Function name: I2C_HAL_Open
160 ** Descriptions: i2c bus open function
161 ** input parameters:
162 **      dev - pointer of i2c slave dev
163 **
164 **
165 ** output parameters: NONE
166 ** Returned value: handle
167 *********************************************************************************************************/
168 PUBLIC int32 I2C_HAL_Open (I2C_DEV *dev)
169 {
170     uint32 handle;
171     uint32 bus_id;
172     uint32 port_id;
173     uint32 idx = 0;
174     int32 i =0;
175     //SCI_ASSERT (NULL != dev);/*assert verified*/
176     /*check if use the wrong port number*/
177    //SCI_ASSERT ( ( (I2C_ID_MAX > dev->id) && (0 <= dev->id)));/*assert verified*/
178     /*now we should get the i2c bus id and the controller's port id*/
179     bus_id = __i2c_phy_cfg[dev->id].phy_id;
180     port_id = __i2c_phy_cfg[dev->id].port_id;
181     /*reference shouldn't larger than I2C_DEV_MAX*/
182     //SCI_ASSERT ( ( (I2C_DEV_MAX > __i2c_bus[bus_id].reference) && (0 <= __i2c_bus[bus_id].reference)));/*assert verified*/
183
184     /*look for the handle: return the 1st available array member*/
185     for (i=0; i< I2C_DEV_MAX; i++)
186     {
187         if (0 == __i2c_dev[idx + bus_id * I2C_DEV_MAX].freq)
188         {
189             break;
190         }
191
192         idx++;
193
194         if (I2C_DEV_MAX == idx)
195         {
196             return -1;
197         }
198     }
199
200     /*caculate handle*/
201     handle = idx + bus_id * I2C_DEV_MAX;
202     /*set the parameters for i2c slave device*/
203     __i2c_dev[handle].id = dev->id;
204     __i2c_dev[handle].freq = dev->freq;
205     __i2c_dev[handle].bus = bus_id;
206     __i2c_dev[handle].slave_addr = dev->slave_addr;
207     __i2c_dev[handle].reg_addr_num = dev->reg_addr_num;
208     __i2c_dev[handle].check_ack = dev->check_ack;
209     __i2c_dev[handle].no_stop = dev->no_stop;
210
211     /*first open, than init i2c controller*/
212     if (0 == __i2c_bus[bus_id].reference)
213     {
214         I2C_Bus_Init (dev->id, dev->freq, port_id);
215         __i2c_bus[bus_id].phy_fun->init (bus_id, dev->freq, port_id);
216     }
217
218     __i2c_bus[bus_id].reference++;
219
220     return  handle;
221 }
222
223 /*********************************************************************************************************
224 ** Function name:
225 ** Descriptions:
226 ** input parameters:
227 **
228 **
229 **
230 ** output parameters:
231 ** Returned value:
232 *********************************************************************************************************/
233 PUBLIC int32 I2C_HAL_Close (uint32 handle)
234 {
235     uint32 bus_id;
236     /*handle shouldn't larger than I2C_BUS_MAX*I2C_DEV_MAX*/
237     //SCI_ASSERT ( ( (I2C_BUS_MAX * I2C_DEV_MAX > handle) && (0 <= handle)));/*assert verified*/
238
239     /*this handle is already closed*/
240     if (0 == __i2c_dev[handle].freq)
241     {
242         return -1;
243     }
244
245     bus_id = __i2c_dev[handle].bus;
246
247     if (1 == __i2c_bus[bus_id].reference)
248     {
249         I2C_Bus_Exit (bus_id);
250     }
251     else
252     {
253         __i2c_bus[bus_id].reference--;
254     }
255
256     /*release the handle*/
257     __i2c_dev[handle].id = 0;
258     __i2c_dev[handle].freq = 0;
259     __i2c_dev[handle].slave_addr = 0;
260     __i2c_dev[handle].reg_addr_num = 0;
261     __i2c_dev[handle].check_ack = 0;
262     __i2c_dev[handle].no_stop = 0;
263
264     return  0;
265 }
266
267 /*********************************************************************************************************
268 ** Function name:
269 ** Descriptions:
270 ** input parameters:
271 **
272 **
273 **
274 ** output parameters:
275 ** Returned value:
276 *********************************************************************************************************/
277 PUBLIC uint32 I2C_HAL_Read (uint32 handle, uint8 *reg_addr, uint8 *buffer, uint32 bytes)
278 {
279     uint32 ret_val;
280     uint32 bus_id;
281     uint32 port_id;
282     uint32 err_code;
283     /*handle shouldn't larger than I2C_BUS_MAX*I2C_DEV_MAX*/
284     //SCI_ASSERT ( ( (I2C_BUS_MAX * I2C_DEV_MAX > handle) && (0 <= handle)));/*assert verified*/
285     //SCI_ASSERT (NULL != buffer);/*assert verified*/
286     //SCI_ASSERT (0 < bytes);/*assert verified*/
287     /*the handle has destroied*/
288     //SCI_ASSERT (0 != __i2c_dev[handle].freq);/*assert verified*/
289     bus_id = __i2c_dev[handle].bus;
290     port_id = __i2c_phy_cfg[__i2c_dev[handle].id].port_id;
291     /*get i2c bus*/
292     //I2C_GetMutex (__i2c_bus[bus_id].mutex);
293
294     /*set i2c controller*/
295     if ( (__i2c_bus[bus_id].current_freq != __i2c_dev[handle].freq) ||
296             (__i2c_bus[bus_id].current_port != port_id))
297     {
298         __i2c_bus[bus_id].phy_fun->init (bus_id,  __i2c_dev[handle].freq, port_id);
299         __i2c_bus[bus_id].current_freq = __i2c_dev[handle].freq;
300         __i2c_bus[bus_id].current_port = port_id;
301     }
302
303     do
304     {
305         err_code = 0;
306
307         /*this is for combined format*/
308         if (0 < __i2c_dev[handle].reg_addr_num)
309         {
310             //SCI_ASSERT (NULL != reg_addr);/*assert verified*/
311             /*start i2c bus*/
312             ret_val = __i2c_bus[bus_id].phy_fun->start (bus_id, __i2c_dev[handle].slave_addr, 0, __i2c_dev[handle].check_ack);
313
314             if (ERR_I2C_NONE != ret_val)
315             {
316                 break;
317             }
318
319             /*write i2c slave device's sub address*/
320             ret_val = __i2c_bus[bus_id].phy_fun->write (bus_id, reg_addr, __i2c_dev[handle].reg_addr_num, __i2c_dev[handle].check_ack, __i2c_dev[handle].no_stop);
321
322             if (ERR_I2C_NONE != ret_val)
323             {
324                 break;
325             }
326         }
327
328         /*restart i2c bus*/
329         ret_val = __i2c_bus[bus_id].phy_fun->start (bus_id, __i2c_dev[handle].slave_addr, 1, __i2c_dev[handle].check_ack);
330
331         if (ERR_I2C_NONE != ret_val)
332         {
333             break;
334         }
335
336         /*read data and stop bus*/
337         ret_val = __i2c_bus[bus_id].phy_fun->read (bus_id, buffer, bytes, __i2c_dev[handle].check_ack);
338
339         if (ERR_I2C_NONE != ret_val)
340         {
341             break;
342         }
343
344         err_code = bytes;
345     }
346     while (0);
347
348     /*release i2c bus*/
349     //I2C_PutMutex (__i2c_bus[bus_id].mutex);
350     return  err_code;
351 }
352
353 /*********************************************************************************************************
354 ** Function name:
355 ** Descriptions:
356 ** input parameters:
357 **
358 **
359 **
360 ** output parameters:
361 ** Returned value:
362 *********************************************************************************************************/
363 PUBLIC uint32 I2C_HAL_Write (uint32 handle, uint8 *reg_addr, uint8 *buffer, uint32 bytes)
364 {
365     uint32 ret_val;
366     uint32 bus_id;
367     uint32 port_id;
368     uint32 err_code;
369     /*handle shouldn't larger than I2C_BUS_MAX*I2C_DEV_MAX*/
370     //SCI_ASSERT ( ( (I2C_BUS_MAX * I2C_DEV_MAX > handle) && (0 <= handle)));/*assert verified*/
371     //SCI_ASSERT (NULL != buffer);/*assert verified*/
372     //SCI_ASSERT (0 < bytes);/*assert verified*/
373     /*the handle has destroied*/
374     //SCI_ASSERT (0 != __i2c_dev[handle].freq);/*assert verified*/
375     bus_id = __i2c_dev[handle].bus;
376     port_id = __i2c_phy_cfg[__i2c_dev[handle].id].port_id;
377     /*get i2c bus*/
378     //I2C_GetMutex (__i2c_bus[bus_id].mutex);
379
380     /*set i2c controller*/
381     if ( (__i2c_bus[bus_id].current_freq != __i2c_dev[handle].freq) ||
382             (__i2c_bus[bus_id].current_port != port_id))
383     {
384         __i2c_bus[bus_id].phy_fun->init (bus_id, __i2c_dev[handle].freq, port_id);
385         __i2c_bus[bus_id].current_freq = __i2c_dev[handle].freq;
386         __i2c_bus[bus_id].current_port = port_id;
387     }
388
389     do
390     {
391         err_code = 0;
392         /*start i2c bus*/
393         ret_val = __i2c_bus[bus_id].phy_fun->start (bus_id, __i2c_dev[handle].slave_addr, 0, __i2c_dev[handle].check_ack);
394
395         if (ERR_I2C_NONE != ret_val)
396         {
397             break;
398         }
399
400         /*write i2c slave device's sub address*/
401         if (0 < __i2c_dev[handle].reg_addr_num)
402         {
403             //SCI_ASSERT (NULL != reg_addr);/*assert verified*/
404             ret_val = __i2c_bus[bus_id].phy_fun->write (bus_id, reg_addr, __i2c_dev[handle].reg_addr_num, __i2c_dev[handle].check_ack, 2);
405
406             if (ERR_I2C_NONE != ret_val)
407             {
408                 break;
409             }
410         }
411
412         /*write i2c slave device's data and stop bus*/
413         ret_val = __i2c_bus[bus_id].phy_fun->write (bus_id, buffer, bytes, __i2c_dev[handle].check_ack, 0);
414
415         if (ERR_I2C_NONE != ret_val)
416         {
417             break;
418         }
419
420         err_code = bytes;
421     }
422     while (0);
423
424     /*release i2c bus*/
425     //I2C_PutMutex (__i2c_bus[bus_id].mutex);
426     return  err_code;
427 }
428
429 /*********************************************************************************************************
430 ** Function name:
431 ** Descriptions:
432 ** input parameters:
433 **
434 **
435 **
436 ** output parameters:
437 ** Returned value:
438 *********************************************************************************************************/
439 PUBLIC uint32 I2C_HAL_Ioctl (uint32 handle, uint32 cmd, uint32 *arg)
440 {
441     uint32 ret_val = 0;
442     uint32 bus_id;
443     /*handle shouldn't larger than I2C_BUS_MAX*I2C_DEV_MAX*/
444     //SCI_ASSERT ( ( (I2C_BUS_MAX * I2C_DEV_MAX > handle) && (0 <= handle)));/*assert verified*/
445     //SCI_ASSERT (NULL != arg);/*assert verified*/
446     bus_id = __i2c_dev[handle].bus;
447
448     switch (cmd)
449     {
450         case I2C_CTL_G_FREQ:
451             *arg = __i2c_dev[handle].freq;
452             break;
453         case I2C_CTL_S_FREQ:
454             __i2c_dev[handle].freq = *arg;
455             break;
456         case I2C_CTL_G_PORT:
457             *arg = __i2c_dev[handle].id;
458             break;
459         case I2C_CTL_S_PORT:
460             __i2c_dev[handle].id = *arg;
461             break;
462         case I2C_CTL_STOP_BUS:
463             /*get i2c bus*/
464            // I2C_GetMutex (__i2c_bus[bus_id].mutex);
465             ret_val = __i2c_bus[bus_id].phy_fun->stop (bus_id);
466             /*release i2c bus*/
467            // I2C_PutMutex (__i2c_bus[bus_id].mutex);
468             break;
469         default:
470             break;
471     }
472
473     return  ret_val;
474 }
475
476 /**---------------------------------------------------------------------------*
477  **                         Compiler Flag                                     *
478  **---------------------------------------------------------------------------*/
479 #ifdef   __cplusplus
480 }
481 #endif
482 /*  End Of File */