853105454c9023d61e3c5c8e952d67c37a0003ca
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-rk2928 / i2c_sram.c
1 #include <linux/io.h>
2 #include <mach/board.h>
3 #include <mach/sram.h>
4 #include <mach/iomux.h>
5 #include <mach/cru.h>
6 #include <asm/io.h>
7 #include <mach/gpio.h>
8
9 #define cru_readl(offset)       readl_relaxed(RK2928_CRU_BASE + offset)
10 #define cru_writel(v, offset)   do { writel_relaxed(v, RK2928_CRU_BASE + offset); dsb(); } while (0)
11
12 #if defined(CONFIG_RK30_I2C_INSRAM)
13
14 /******************need set when you use i2c*************************/
15 #define I2C_SPEED 100
16 #define I2C_SADDR               (0x2D)        /* slave address ,wm8310 addr is 0x34*/
17 #define SRAM_I2C_CH 1   //CH==0, i2c0,CH==1, i2c1,CH==2, i2c2,CH==3, i2c3
18 #if defined (CONFIG_MACH_RK2928_SDK)
19 #define SRAM_I2C_ADDRBASE (RK2928_RKI2C1_BASE )//RK29_I2C0_BASE\RK29_I2C2_BASE\RK29_I2C3_BASE
20 #else
21 #define SRAM_I2C_ADDRBASE (RK2928_RKI2C0_BASE )
22 #endif
23
24 #define I2C_SLAVE_ADDR_LEN 1  // 2:slav addr is 10bit ,1:slav addr is 7bit
25 #define I2C_SLAVE_REG_LEN 1   // 2:slav reg addr is 16 bit ,1:is 8 bit
26 #define SRAM_I2C_DATA_BYTE 1  //i2c transmission data is 1bit(8wei) or 2bit(16wei)
27 #define GRF_GPIO_IOMUX 0xd4 //GRF_GPIO2D_IOMUX
28 /*ch=0:GRF_GPIO2L_IOMUX,ch=1:GRF_GPIO1L_IOMUX,ch=2:GRF_GPIO5H_IOMUX,ch=3:GRF_GPIO2L_IOMUX*/
29 #define I2C_GRF_GPIO_IOMUX (0x01<<14)|(0x01<<12)
30 /*CH=0:(~(0x03<<30))&(~(0x03<<28))|(0x01<<30)|(0x01<<28),CH=1:(~(0x03<<14))&(~(0x03<<12))|(0x01<<14)|(0x01<<12),
31 CH=2:(~(0x03<<24))&(~(0x03<<22))|(0x01<<24)|(0x01<<22),CH=3:(~(0x03<<26))&(~(0x03<<24))|(0x02<<26)|(0x02<<24)*/
32 /***************************************/
33
34 #define I2C_SLAVE_TYPE (((I2C_SLAVE_ADDR_LEN-1)<<4)|((I2C_SLAVE_REG_LEN-1)))
35
36 #define uint8 unsigned char
37 #define uint16 unsigned short
38 #define uint32 unsigned int
39 uint32 __sramdata data[5];
40 uint8 __sramdata arm_voltage = 0;
41
42 #define CRU_CLKGATE0_CON   0xd0
43 #define CRU_CLKGATE8_CON        0xf0
44 #define CRU_CLKSEL1_CON         0x48
45 #define GRF_GPIO5H_IOMUX        0x74
46 #define GRF_GPIO2L_IOMUX        0x58
47 #define GRF_GPIO1L_IOMUX        0x50
48
49 #define COMPLETE_READ     (1<<STATE_START|1<<STATE_READ|1<<STATE_STOP)
50 #define COMPLETE_WRITE     (1<<STATE_START|1<<STATE_WRITE|1<<STATE_STOP)
51
52 /* Control register */
53 #define I2C_CON                 0x0000
54 #define I2C_CON_EN              (1 << 0)
55 #define I2C_CON_MOD(mod)        ((mod) << 1)
56 #define I2C_CON_MASK            (3 << 1)
57 enum{
58         I2C_CON_MOD_TX = 0,
59         I2C_CON_MOD_TRX,
60         I2C_CON_MOD_RX,
61         I2C_CON_MOD_RRX,
62 };
63 #define I2C_CON_START           (1 << 3)
64 #define I2C_CON_STOP            (1 << 4)
65 #define I2C_CON_LASTACK         (1 << 5)
66 #define I2C_CON_ACTACK          (1 << 6)
67
68 /* Clock dividor register */
69 #define I2C_CLKDIV              0x0004
70 #define I2C_CLKDIV_VAL(divl, divh) (((divl) & 0xffff) | (((divh) << 16) & 0xffff0000))  
71 #define rk30_ceil(x, y) \
72         ({ unsigned long __x = (x), __y = (y); (__x + __y - 1) / __y; })
73
74 /* the slave address accessed  for master rx mode */
75 #define I2C_MRXADDR             0x0008
76 #define I2C_MRXADDR_LOW         (1 << 24)
77 #define I2C_MRXADDR_MID         (1 << 25)
78 #define I2C_MRXADDR_HIGH        (1 << 26)
79
80 /* the slave register address accessed  for master rx mode */
81 #define I2C_MRXRADDR            0x000c
82 #define I2C_MRXRADDR_LOW        (1 << 24)
83 #define I2C_MRXRADDR_MID        (1 << 25)
84 #define I2C_MRXRADDR_HIGH       (1 << 26)
85
86 /* master tx count */
87 #define I2C_MTXCNT              0x0010
88
89 /* master rx count */
90 #define I2C_MRXCNT              0x0014
91
92 /* interrupt enable register */
93 #define I2C_IEN                 0x0018
94 #define I2C_BTFIEN              (1 << 0)
95 #define I2C_BRFIEN              (1 << 1)
96 #define I2C_MBTFIEN             (1 << 2)
97 #define I2C_MBRFIEN             (1 << 3)
98 #define I2C_STARTIEN            (1 << 4)
99 #define I2C_STOPIEN             (1 << 5)
100 #define I2C_NAKRCVIEN           (1 << 6)
101 #define IRQ_MST_ENABLE          (I2C_MBTFIEN | I2C_MBRFIEN | I2C_NAKRCVIEN | I2C_STARTIEN | I2C_STOPIEN)
102 #define IRQ_ALL_DISABLE         0
103
104 /* interrupt pending register */
105 #define I2C_IPD                 0x001c
106 #define I2C_BTFIPD              (1 << 0)
107 #define I2C_BRFIPD              (1 << 1)
108 #define I2C_MBTFIPD             (1 << 2)
109 #define I2C_MBRFIPD             (1 << 3)
110 #define I2C_STARTIPD            (1 << 4)
111 #define I2C_STOPIPD             (1 << 5)
112 #define I2C_NAKRCVIPD           (1 << 6)
113 #define I2C_IPD_ALL_CLEAN       0x7f
114
115 /* finished count */
116 #define I2C_FCNT                0x0020
117
118 /* I2C tx data register */
119 #define I2C_TXDATA_BASE         0X0100
120
121 /* I2C rx data register */
122 #define I2C_RXDATA_BASE         0x0200
123
124 void __sramfunc sram_i2c_enable(void);
125 void __sramfunc sram_i2c_disenable(void);
126
127 void __sramfunc sram_printch(char byte);
128 void __sramfunc sram_printhex(unsigned int hex);
129
130 #define sram_udelay(usecs)      SRAM_LOOP((usecs)*SRAM_LOOPS_PER_USEC)
131
132 /*-------------------------------------------------------------------------------------------------------
133 Name      : sram_i2c_init
134 Desc      : initialize the necessary registers
135 Params    : channel-determine which I2C bus we used
136 Return    : none
137 ------------------------------------------------------------------------------------------------------*/
138
139 void __sramfunc sram_i2c_init()
140 {
141           unsigned int div, divl, divh;
142     //enable cru_clkgate8 clock
143     data[1] = cru_readl(CLK_GATE_CLKID_CONS(CLK_GATE_CLKID(8)));
144     
145         #if defined (CONFIG_MACH_RK2928_SDK)
146     cru_writel(CLK_GATE_W_MSK(CLK_GATE_PCLK_I2C1)|CLK_UN_GATE(CLK_GATE_PCLK_I2C1), 
147                 CLK_GATE_CLKID_CONS(CLK_GATE_PCLK_I2C1));
148         #else
149     cru_writel(CLK_GATE_W_MSK(CLK_GATE_PCLK_I2C0)|CLK_UN_GATE(CLK_GATE_PCLK_I2C0), 
150                 CLK_GATE_CLKID_CONS(CLK_GATE_PCLK_I2C0));
151         #endif
152         
153     data[2] = readl_relaxed(RK2928_GRF_BASE + GRF_GPIO_IOMUX);
154     writel_relaxed(data[2]| I2C_GRF_GPIO_IOMUX, RK2928_GRF_BASE + GRF_GPIO_IOMUX);
155         
156         div = 0x1e;
157         divh = divl = 0xf;              
158         writel_relaxed(I2C_CLKDIV_VAL(divl, divh), SRAM_I2C_ADDRBASE + I2C_CLKDIV);
159         data[3]  = readl_relaxed(SRAM_I2C_ADDRBASE + I2C_CLKDIV);
160         
161 }
162
163
164 /*-------------------------------------------------------------------------------------------------------
165 Name      : sram_i2c_deinit
166 Desc      : de-initialize the necessary registers
167 Params    : noe
168 Return    : none
169 ------------------------------------------------------------------------------------------------------*/
170 void __sramfunc sram_i2c_deinit(void)
171 {
172     //restore iomux reg
173     writel_relaxed(data[2], RK2928_GRF_BASE + GRF_GPIO_IOMUX);
174
175     //restore scu clock reg
176     cru_writel(data[1], CLK_GATE_CLKID_CONS(CLK_GATE_PCLK_I2C1));
177
178
179 }
180
181 /*-------------------------------------------------------------------------------------------------------
182 Name      : sram_i2c_start
183 Desc      : start i2c
184 Params    : none
185 Return    : none
186 ------------------------------------------------------------------------------------------------------*/
187 void __sramfunc sram_i2c_read_enable(void)
188 {
189         writel_relaxed(((((I2C_CON_EN | I2C_CON_MOD(1)) | I2C_CON_LASTACK) )| I2C_CON_START) & (~(I2C_CON_STOP)) , SRAM_I2C_ADDRBASE + I2C_CON);
190 }
191 void __sramfunc sram_i2c_write_enable(void)
192 {
193         writel_relaxed(((((I2C_CON_EN | I2C_CON_MOD(0)) | I2C_CON_LASTACK) )| I2C_CON_START) & (~(I2C_CON_STOP)) , SRAM_I2C_ADDRBASE + I2C_CON);
194 }
195
196 void __sramfunc sram_i2c_disenable(void)
197 {
198     writel_relaxed(0, SRAM_I2C_ADDRBASE + I2C_CON);
199 }
200
201 void __sramfunc sram_i2c_clean_start(void)
202 {
203         unsigned int con = readl_relaxed(SRAM_I2C_ADDRBASE + I2C_CON);
204
205         con = (con & (~I2C_CON_START)) ;
206         writel_relaxed(con, SRAM_I2C_ADDRBASE + I2C_CON);
207 }
208 void __sramfunc sram_i2c_send_start(void)
209 {
210         unsigned int con = readl_relaxed(SRAM_I2C_ADDRBASE + I2C_CON);
211         con |= I2C_CON_START;
212         if(con & I2C_CON_STOP)
213                         sram_printch('E');
214         writel_relaxed(con, SRAM_I2C_ADDRBASE + I2C_CON);
215 }
216 void __sramfunc sram_i2c_send_stop(void)
217 {
218         unsigned int con = readl_relaxed(SRAM_I2C_ADDRBASE + I2C_CON);
219                 con &=  ~I2C_CON_START;
220         con |= I2C_CON_STOP;
221         if(con & I2C_CON_START) 
222                         sram_printch('E');
223         writel_relaxed(con, SRAM_I2C_ADDRBASE + I2C_CON);
224 }
225 void __sramfunc sram_i2c_clean_stop(void)
226 {
227         unsigned int con = readl_relaxed(SRAM_I2C_ADDRBASE + I2C_CON);
228
229         con = (con & (~I2C_CON_STOP)) ;
230         writel_relaxed(con, SRAM_I2C_ADDRBASE + I2C_CON);
231 }
232
233 void __sramfunc sram_i2c_get_ipd_event(int type)
234 {
235         int time = 2000;
236         unsigned int con = readl_relaxed(SRAM_I2C_ADDRBASE + I2C_IPD);
237         writel_relaxed(type, SRAM_I2C_ADDRBASE + I2C_IEN);
238         do{
239                 sram_udelay(10);
240                 con = readl_relaxed(SRAM_I2C_ADDRBASE + I2C_IPD);
241         }while(((--time) & (~(con & type))));
242         writel_relaxed(type,SRAM_I2C_ADDRBASE + I2C_IPD);
243         
244         if(time <= 0){
245         sram_printch('T');
246         }
247 }
248
249 void __sramfunc sram_i2c_write_prepare(uint8 I2CSlaveAddr, uint8 regAddr,uint8 pdata)
250 {
251     u32 data = 0;
252     unsigned int addr = (I2CSlaveAddr & 0x7f) << 1;
253
254         data = (addr | (regAddr << 8))|(pdata << 16);
255     writel_relaxed(data , SRAM_I2C_ADDRBASE + I2C_TXDATA_BASE);
256         writel_relaxed(3, SRAM_I2C_ADDRBASE + I2C_MTXCNT);
257 }
258 uint8 __sramfunc sram_i2c_read_prepare(uint8 I2CSlaveAddr, uint8 regAddr)
259 {
260         unsigned int addr = (I2CSlaveAddr & 0x7f) << 1;
261  
262          writel_relaxed(addr | I2C_MRXADDR_LOW, SRAM_I2C_ADDRBASE + I2C_MRXADDR);
263      writel_relaxed(regAddr | I2C_MRXADDR_LOW, SRAM_I2C_ADDRBASE + I2C_MRXRADDR);
264     writel_relaxed(SRAM_I2C_DATA_BYTE, SRAM_I2C_ADDRBASE + I2C_MRXCNT);
265         return 0;
266 }
267
268 uint8 __sramfunc sram_i2c_read_get_data(uint8 I2CSlaveAddr, uint8 regAddr)
269 {
270         unsigned int ret;
271     ret =  readl_relaxed(SRAM_I2C_ADDRBASE + I2C_RXDATA_BASE);
272         ret = ret & 0x000000ff;
273         return ret;
274         
275 }
276 uint8 __sramfunc sram_i2c_write(uint8 I2CSlaveAddr, uint8 regAddr,uint8 data)
277 {
278         sram_i2c_write_enable();
279         sram_i2c_get_ipd_event(I2C_STARTIPD);
280         sram_i2c_clean_start();
281         
282         sram_i2c_write_prepare(I2CSlaveAddr,regAddr,data);
283         sram_i2c_get_ipd_event(I2C_MBTFIPD);
284         
285         sram_i2c_send_stop();
286         sram_i2c_get_ipd_event(I2C_STOPIPD);
287         sram_i2c_clean_stop();
288         sram_i2c_disenable();
289         return 0;
290         
291 }
292
293 uint8 __sramfunc sram_i2c_read(uint8 I2CSlaveAddr, uint8 regAddr)
294 {
295         unsigned int data;
296         sram_i2c_read_enable();
297         sram_i2c_get_ipd_event(I2C_STARTIPD);
298         sram_i2c_clean_start();
299
300         sram_i2c_read_prepare(I2CSlaveAddr,regAddr);
301         sram_i2c_get_ipd_event(I2C_MBRFIPD);
302         
303         data = sram_i2c_read_get_data(I2CSlaveAddr,regAddr);
304         
305         sram_i2c_send_stop();
306         sram_i2c_get_ipd_event(I2C_STOPIPD);
307         sram_i2c_clean_stop();
308         sram_i2c_disenable();
309         return data;
310 }
311 extern int __sramdata g_pmic_type ;
312 void __sramfunc rk30_suspend_voltage_set(unsigned int vol)
313 {
314     uint8 slaveaddr;
315     uint16 slavereg;
316     uint8 data,ret = 0;
317         uint8 rtc_status_reg = 0x11;
318         sram_i2c_init();  //init i2c device
319         #if defined(CONFIG_MFD_TPS65910)        
320         if(g_pmic_type == PMIC_TYPE_TPS65910)
321         {
322         slaveaddr = 0x2d;            //slave device addr
323         slavereg = 0x22;            // reg addr
324         data = 0x23;       //set arm 1.0v
325
326         ret = sram_i2c_read(slaveaddr, rtc_status_reg);
327         sram_i2c_write(slaveaddr, rtc_status_reg, ret);
328         arm_voltage = sram_i2c_read(slaveaddr, slavereg);
329         //sram_printhex(ret);
330         sram_i2c_write(slaveaddr, slavereg, data);//    
331         }
332         #endif
333         
334         #if defined(CONFIG_REGULATOR_ACT8931)   
335         if(g_pmic_type == PMIC_TYPE_ACT8931)
336         {
337         slaveaddr = 0x5b;            //slave device addr
338         slavereg = 0x40;            // reg addr
339         data = 0x10;       //set arm 1.0v
340
341         arm_voltage = sram_i2c_read(slaveaddr, slavereg);
342         //sram_printhex(ret);
343         sram_i2c_write(slaveaddr, slavereg, data);//    
344         sram_i2c_write(slaveaddr,( slavereg+0x1), data);//      
345         }
346         #endif
347           sram_i2c_deinit();  //deinit i2c device
348
349 }
350
351 void __sramfunc rk30_suspend_voltage_resume(unsigned int vol)
352 {
353     uint8 slaveaddr;
354     uint16 slavereg;
355     uint8 data,ret = 0;
356         
357         data = arm_voltage;
358         sram_i2c_init();  //init i2c device
359         #if defined(CONFIG_MFD_TPS65910)        
360         if(g_pmic_type == PMIC_TYPE_TPS65910)
361         {
362                 slaveaddr = 0x2d;            //slave device addr
363                 slavereg = 0x22;            // reg add
364                  sram_i2c_write(slaveaddr, slavereg, data);
365                  sram_udelay(20000);
366         }
367         #endif
368         #if defined(CONFIG_REGULATOR_ACT8931)   
369         if(g_pmic_type == PMIC_TYPE_ACT8931)
370         {
371                 slaveaddr = 0x5b;            //slave device addr
372                 slavereg = 0x40;            // reg addr
373                 sram_i2c_write(slaveaddr, slavereg, data);
374                 sram_i2c_write(slaveaddr, (slavereg+0x1), data);
375                 sram_udelay(20000);
376         }
377         #endif
378     sram_i2c_deinit();  //deinit i2c device
379 }
380
381 int __sramfunc act8931_dc_det(unsigned int vol)
382 {
383         uint8 slaveaddr;
384         uint16 slavereg;
385         uint8 ret = 0;
386         int data = 0;
387         slaveaddr = 0x5b;            //slave device addr
388        slavereg = 0x78;            // reg addr  
389        
390         sram_i2c_init();  //init i2c device
391         ret = sram_i2c_read(slaveaddr,slavereg);
392         data = (ret & (1<<1) )? 1:0;
393         sram_i2c_deinit();  //deinit i2c device
394         return data;
395 }
396
397 #else
398 void __sramfunc rk30_suspend_voltage_set(unsigned int vol)
399 {
400
401 }
402 void __sramfunc rk30_suspend_voltage_resume(unsigned int vol)
403 {
404    
405 }
406 int __sramfunc act8931_dc_det(unsigned int vol)
407 {
408         return -1;
409 }
410 #endif
411
412
413