upload tizen1.0 source
[kernel/linux-2.6.36.git] / drivers / media / video / s5p-tv / mhl_v1 / SiI9234_I2C_master.c
1 /***************************************************************************
2
3
4
5 *   SiI9244 - MHL Transmitter Driver
6
7 *
8
9 * Copyright (C) (2011, Silicon Image Inc)
10
11 *
12
13 * This program is free software; you can redistribute it and/or modify
14
15 * it under the terms of the GNU General Public License as published by
16
17 * the Free Software Foundation version 2.
18
19 *
20
21 * This program is distributed ¡°as is¡± WITHOUT ANY WARRANTY of any
22
23 * kind, whether express or implied; without even the implied warranty
24
25 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
27 * GNU General Public License for more details.
28
29 *
30
31 *****************************************************************************/
32
33 /*===========================================================================
34
35                       EDIT HISTORY FOR FILE
36
37 when              who                         what, where, why
38 --------        ---                        ----------------------------------------------------------
39 2010/10/25    Daniel Lee(Philju)      Initial version of file, SIMG Korea 
40 ===========================================================================*/
41 #include <linux/interrupt.h>
42 #include <linux/irq.h>
43 #include <linux/i2c.h>
44 #include <linux/gpio.h>
45 #include <plat/pm.h>
46 #include <asm/irq.h>
47 #include <linux/delay.h>
48 #include <plat/gpio-cfg.h>
49 #include <mach/regs-gpio.h>
50 #include <mach/regs-clock.h>
51
52 #include <linux/syscalls.h> 
53 #include <linux/fcntl.h> 
54 #include <asm/uaccess.h> 
55 #include <linux/types.h>
56
57
58 #include "Common_Def.h"
59 #include "SiI9234_I2C_slave_add.h"
60
61
62 /*===========================================================================
63
64 ===========================================================================*/
65
66 #define READ   1
67 #define WRITE  0
68
69 #define LAST_BYTE      1
70 #define NOT_LAST_BYTE  0
71
72
73 #define TPI_INDEXED_PAGE_REG            0xBC
74 #define TPI_INDEXED_OFFSET_REG          0xBD
75 #define TPI_INDEXED_VALUE_REG           0xBE
76
77
78 /*===========================================================================
79
80 ===========================================================================*/
81 //------------------------------------------------------------------------------
82 // Function: I2C_WriteByte
83 // Description:
84 //------------------------------------------------------------------------------
85 void I2C_WriteByte(byte deviceID, byte offset, byte value)
86 {
87
88         struct i2c_client* client_ptr = get_sii9234_client(deviceID);
89         if(!client_ptr)
90         {
91                 printk("[MHL]I2C_WriteByte error %x\n",deviceID); 
92                 return; 
93         }
94         
95         if(deviceID == 0x72)
96                 sii9234_i2c_write(client_ptr,offset,value);
97         else if(deviceID == 0x7A)
98                 sii9234_i2c_write(client_ptr,offset,value);
99         else if(deviceID == 0x92)
100                 sii9234_i2c_write(client_ptr,offset,value);
101         else if(deviceID == 0xC8)
102                 sii9234_i2c_write(client_ptr,offset,value);
103
104
105 }
106
107
108 byte I2C_ReadByte(byte deviceID, byte offset)
109 {
110         byte number = 0;
111         struct i2c_client* client_ptr = get_sii9234_client(deviceID);
112         if(!client_ptr)
113         {
114                 printk("[MHL]I2C_ReadByte error %x\n",deviceID); 
115                 return 0;       
116         }
117
118   
119         if(deviceID == 0x72)
120                 number = sii9234_i2c_read(client_ptr,offset);
121         else if(deviceID == 0x7A)
122                 number = sii9234_i2c_read(client_ptr,offset);
123         else if(deviceID == 0x92)
124                 number = sii9234_i2c_read(client_ptr,offset);
125         else if(deviceID == 0xC8)
126                 number = sii9234_i2c_read(client_ptr,offset);
127         
128     return (number);
129
130 }
131
132 byte ReadByteTPI (byte Offset) 
133 {
134         return I2C_ReadByte(SA_TX_Page0_Primary, Offset);
135 }
136
137 void WriteByteTPI (byte Offset, byte Data) 
138 {
139         I2C_WriteByte(SA_TX_Page0_Primary, Offset, Data);
140 }
141
142
143
144 void ReadModifyWriteTPI(byte Offset, byte Mask, byte Data) 
145 {
146
147         byte Temp;
148
149         Temp = ReadByteTPI(Offset);             // Read the current value of the register.
150         Temp &= ~Mask;                                  // Clear the bits that are set in Mask.
151         Temp |= (Data & Mask);                  // OR in new value. Apply Mask to Value for safety.
152         WriteByteTPI(Offset, Temp);             // Write new value back to register.
153 }
154
155 byte ReadByteCBUS (byte Offset) 
156 {
157         return I2C_ReadByte(SA_TX_CBUS_Primary, Offset);
158 }
159
160 void WriteByteCBUS(byte Offset, byte Data) 
161 {
162         I2C_WriteByte(SA_TX_CBUS_Primary, Offset, Data);
163 }
164
165 void ReadModifyWriteCBUS(byte Offset, byte Mask, byte Value) 
166 {
167   byte Temp;
168
169   Temp = ReadByteCBUS(Offset);
170   Temp &= ~Mask;
171   Temp |= (Value & Mask);
172   WriteByteCBUS(Offset, Temp);
173 }
174
175
176 //////////////////////////////////////////////////////////////////////////////
177 //
178 // FUNCTION             :       ReadIndexedRegister ()
179 //
180 // PURPOSE              :       Read the value from an indexed register.
181 //
182 //                                      Write:
183 //                                              1. 0xBC => Indexed page num
184 //                                              2. 0xBD => Indexed register offset
185 //
186 //                                      Read:
187 //                                              3. 0xBE => Returns the indexed register value
188 //
189 // INPUT PARAMS :       PageNum -       indexed page number
190 //                                      Offset  -       offset of the register within the indexed page.
191 //
192 // OUTPUT PARAMS:       None
193 //
194 // GLOBALS USED :       None
195 //
196 // RETURNS              :       The value read from the indexed register.
197 //
198 //////////////////////////////////////////////////////////////////////////////
199
200 byte ReadIndexedRegister (byte PageNum, byte Offset) 
201 {
202         WriteByteTPI(TPI_INDEXED_PAGE_REG, PageNum);            // Indexed page
203         WriteByteTPI(TPI_INDEXED_OFFSET_REG, Offset);           // Indexed register
204         return ReadByteTPI(TPI_INDEXED_VALUE_REG);                      // Return read value
205 }
206
207
208 //////////////////////////////////////////////////////////////////////////////
209 //
210 // FUNCTION             :       WriteIndexedRegister ()
211 //
212 // PURPOSE              :       Write a value to an indexed register
213 //
214 //                                      Write:
215 //                                              1. 0xBC => Indexed page num
216 //                                              2. 0xBD => Indexed register offset
217 //                                              3. 0xBE => Set the indexed register value
218 //
219 // INPUT PARAMS :       PageNum -       indexed page number
220 //                                      Offset  -       offset of the register within the indexed page.
221 //                                      Data    -       the value to be written.
222 //
223 // OUTPUT PARAMS:       None
224 //
225 // GLOBALS USED :       None
226 //
227 // RETURNS              :       None
228 //
229 //////////////////////////////////////////////////////////////////////////////
230
231 void WriteIndexedRegister (byte PageNum, byte Offset, byte Data) 
232 {
233         WriteByteTPI(TPI_INDEXED_PAGE_REG, PageNum);            // Indexed page
234         WriteByteTPI(TPI_INDEXED_OFFSET_REG, Offset);           // Indexed register
235         WriteByteTPI(TPI_INDEXED_VALUE_REG, Data);                      // Write value
236 }
237
238
239 //////////////////////////////////////////////////////////////////////////////
240 //
241 // FUNCTION             :       ReadModifyWriteIndexedRegister ()
242 //
243 // PURPOSE              :       Set or clear individual bits in a TPI register.
244 //
245 // INPUT PARAMS :       PageNum -       indexed page number
246 //                                      Offset  -       the offset of the indexed register to be modified.
247 //                                      Mask    -       "1" for each indexed register bit that needs to be
248 //                                                              modified
249 //                                      Data    -       The desired value for the register bits in their
250 //                                                              proper positions
251 //
252 // OUTPUT PARAMS:       None
253 //
254 // GLOBALS USED :       None
255 //
256 // RETURNS              :       void
257 //
258 //////////////////////////////////////////////////////////////////////////////
259
260 void ReadModifyWriteIndexedRegister (byte PageNum, byte Offset, byte Mask, byte Data) 
261 {
262
263         byte Temp;
264
265         Temp = ReadIndexedRegister (PageNum, Offset);   // Read the current value of the register.
266         Temp &= ~Mask;                                                                  // Clear the bits that are set in Mask.
267         Temp |= (Data & Mask);                                                  // OR in new value. Apply Mask to Value for safety.
268         WriteByteTPI(TPI_INDEXED_VALUE_REG, Temp);              // Write new value back to register.
269 }
270