1 #include <linux/module.h>
2 #include <linux/init.h>
4 #include <linux/platform_device.h>
5 #include <linux/dma-mapping.h>
6 #include <linux/slab.h>
7 #include <linux/errno.h>
9 #include <mach/mt_pm_ldo.h>
12 static char data_buffer
[256 * 4];
14 static ssize_t
show_config(struct device
*dev
, struct device_attribute
*attr
, char *buff
)
16 int len
= strlen(data_buffer
);
17 memcpy(buff
,data_buffer
,len
);
18 printk("Return Value:%s\n",data_buffer
);
22 static int pows(int x
, int y
)
25 while (y
--) result
*=x
;
29 int string2hex(const char * buffer
, int cnt
){
34 t
= *(buffer
+ cnt
- count
- 1);
35 if ( t
>= 'A' && t
<= 'F') {
36 c
+= ((t
- 'A') + 10 ) * pows(16,count
);
37 } else if (t
>= '0' && t
<= '9'){
38 c
+= (t
- '0') * pows(16,count
);
46 char * get_hexbuffer(char *data_buffer
, char *hex_buffer
)
48 char * ptr
= data_buffer
;
50 while (*ptr
&& *++ptr
) {
51 *(hex_buffer
+ index
++) = string2hex(ptr
-1, 2);
54 *(hex_buffer
+ index
) = 0;
58 int i2c_trans_data(int bus_id
, int address
, char *buf
, int count
, unsigned int ext_flag
,int timing
)
63 struct i2c_adapter
*adap
;
64 adap
= i2c_get_adapter(bus_id
);
68 msg
.flags
= ((ext_flag
& 0x80000000)?I2C_M_RD
:0);
71 msg
.buf
= (char *)buf
;
72 msg
.ext_flag
= ext_flag
& 0x7FFFFFFF;
73 // msg.ext_flag = (ext_flag & 0x7FFF00FF);
74 // msg.addr |= ext_flag & 0x0000FF00;
75 ret
= i2c_transfer(adap
, &msg
, 1);
77 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
78 transmitted, else error code. */
79 i2c_put_adapter(adap
);
80 return (ret
== 1) ? count
: ret
;
82 int mt_i2c_test(int id
, int addr
)
86 unsigned char buffer
[]={0x55};
88 flag
= I2C_DIRECTION_FLAG
;
91 ret
= i2c_trans_data(id
, addr
,buffer
,1,flag
,200);
94 EXPORT_SYMBOL(mt_i2c_test
);
97 static int i2c_test_reg(int bus_id
,int val
)
100 struct i2c_adapter
*adap
;
102 adap
= i2c_get_adapter(bus_id
);
103 if (!adap
) return -1;
104 i2c
= container_of(adap
,mt_i2c
,adap
);
105 printk("I2C%d base address %8x\n",bus_id
,i2c
->base
);
106 //write i2c writable register with 0
107 i2c_writel(i2c
,OFFSET_SLAVE_ADDR
,val
);
108 i2c_writel(i2c
,OFFSET_INTR_MASK
,val
);
109 i2c_writel(i2c
,OFFSET_INTR_STAT
,val
);
110 i2c_writel(i2c
,OFFSET_CONTROL
,val
);
111 i2c_writel(i2c
,OFFSET_TRANSFER_LEN
,val
);
112 i2c_writel(i2c
,OFFSET_TRANSAC_LEN
,val
);
113 i2c_writel(i2c
,OFFSET_DELAY_LEN
,val
);
114 i2c_writel(i2c
,OFFSET_TIMING
,val
);
115 i2c_writel(i2c
,OFFSET_EXT_CONF
,val
);
116 i2c_writel(i2c
,OFFSET_IO_CONFIG
,val
);
117 i2c_writel(i2c
,OFFSET_HS
,val
);
118 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
119 transmitted, else error code. */
120 i2c_put_adapter(adap
);
123 static int i2c_soft_reset(int bus_id
)
126 struct i2c_adapter
*adap
;
128 adap
= i2c_get_adapter(bus_id
);
129 if (!adap
) return -1;
130 i2c
= container_of(adap
,mt_i2c
,adap
);
131 printk("I2C%d base address %8x\n",bus_id
,i2c
->base
);
132 //write i2c writable register with 0
133 i2c_writel(i2c
,OFFSET_SOFTRESET
,1);
134 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
135 transmitted, else error code. */
136 i2c_put_adapter(adap
);
139 static int i2c_ext_conf_test(int bus_id
,int val
)
142 struct i2c_adapter
*adap
;
144 adap
= i2c_get_adapter(bus_id
);
145 if (!adap
) return -1;
146 i2c
= container_of(adap
,mt_i2c
,adap
);
147 printk("I2C%d base address %8x\n",bus_id
,i2c
->base
);
148 //write i2c writable register with 0
149 i2c_writel(i2c
,OFFSET_EXT_CONF
,val
);
150 printk("EXT_CONF 0x%x",i2c_readl(i2c
,OFFSET_EXT_CONF
));
151 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
152 transmitted, else error code. */
153 i2c_put_adapter(adap
);
156 static void hex2string(unsigned char * in
, unsigned char * out
, int length
)
158 unsigned char * ptr
= in
;
159 unsigned char * ptrout
= out
;
163 t
= (*ptr
& 0xF0) >> 4 ;
164 if ( t
< 10 ) *ptrout
= t
+ '0';
165 else *ptrout
= t
+ 'A' - 10;
170 if ( t
< 10 ) *ptrout
= t
+ '0';
171 else *ptrout
= t
+ 'A' - 10;
179 static ssize_t
set_config(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
196 unsigned int ext_flag
= 0;
197 dma_addr_t dma_addr
= 0;
198 void * vir_addr
= NULL
;
202 unsigned char tmpbuffer
[128];
204 //if ( sscanf(buf, "%d %d %d %d %d %d %d %d %d %d %d %d %s", &bus_id, &address, &operation, &trans_mode, &trans_stop, &speed_mode, &pushpull_mode, &query_mode, &timing, &trans_num, &trans_auxlen,&dir, data_buffer) ) {
205 if ( sscanf(buf
, "%d %x %d %d %d %d %d %d %d %d %d %1023s", &bus_id
, &address
, &operation
, &trans_mode
, &trans_stop
, &speed_mode
, &pushpull_mode
, &query_mode
, &timing
, &trans_num
, &trans_auxlen
,data_buffer
) ) {
206 if((address
!= 0)&&(operation
<=2)){
207 length
= strlen(data_buffer
);
209 ext_flag
|= I2C_WR_FLAG
;
210 number
= (trans_auxlen
<< 8 ) | (length
>> 1);
211 } else if (operation
== 1) {
212 ext_flag
|= 0x80000000;
213 number
= (trans_num
<< 8 ) | (length
>> 1);
214 } else if (operation
== 2) {
215 ext_flag
&= 0x7FFFFFFF;
216 number
= (trans_num
<< 8 ) | (length
>> 1);
219 printk("invalid operation\n");
223 ext_flag
|= I2C_DIRECTION_FLAG
;
225 if (trans_mode
== 0){
227 } else if (trans_mode
== 1) {
228 ext_flag
|= I2C_DMA_FLAG
;
231 printk("invalid trans_mod fifo/dma\n");
235 if (trans_stop
== 0) {
237 } else if (trans_stop
== 1) {
238 ext_flag
|= I2C_RS_FLAG
;
241 printk("invalid trans_stop\n");
245 if (speed_mode
== 0) {
247 } else if (speed_mode
== 1) {
248 //ext_flag |= I2C_FS_FLAG;//FS MODE
249 } else if (speed_mode
== 2) {
250 ext_flag
|= I2C_HS_FLAG
;//HS mode
252 printk("invalid speed_mode\n");
256 if (pushpull_mode
== 0){
258 } else if (pushpull_mode
== 1) {
259 ext_flag
|= I2C_PUSHPULL_FLAG
;
262 printk("invalid pushpull mode\n");
266 if ( query_mode
== 0 ){
268 } else if (query_mode
== 1) {
269 ext_flag
|= I2C_POLLING_FLAG
;
272 printk("invalid query mode interrupt/polling\n");
276 if (trans_mode
== 1) {/*DMA MODE*/
277 vir_addr
= dma_alloc_coherent(NULL
, length
>> 1, &dma_addr
, GFP_KERNEL
);
278 if ( vir_addr
== NULL
){
280 printk("alloc dma memory failed\n");
284 vir_addr
= kzalloc(length
>> 1, GFP_KERNEL
);
285 if ( vir_addr
== NULL
){
287 printk("alloc virtual memory failed\n");
292 get_hexbuffer(data_buffer
, vir_addr
);
293 printk(KERN_ALERT
"bus_id:%d,address:%x,count:%x,ext_flag:0x%x,timing:%d\n", bus_id
,address
,number
,ext_flag
,timing
);
294 printk(KERN_ALERT
"data_buffer:%s\n", data_buffer
);
296 if ( trans_mode
== 1 ){
298 ret
= i2c_trans_data(bus_id
, address
, (void *)dma_addr
, number
, ext_flag
, timing
);
300 ret
= i2c_trans_data(bus_id
, address
, vir_addr
, number
, ext_flag
, timing
);
307 if ( operation
== 1 ) {
308 hex2string(vir_addr
, tmpbuffer
, length
>> 1);
309 sprintf(data_buffer
, "1 %s", tmpbuffer
);
310 } else if ( operation
== 0 ){
311 hex2string(vir_addr
, tmpbuffer
, trans_auxlen
);
312 sprintf(data_buffer
, "1 %s", tmpbuffer
);
314 sprintf(data_buffer
, "1 %s", "00");
316 printk("Actual return Value:%d %p\n",ret
, vir_addr
);
317 } else if ( ret
< 0 ) {
319 if ( ret
== -EINVAL
) {
320 sprintf(data_buffer
, "0 %s", "Invalid Parameter");
321 } else if ( ret
== -ETIMEDOUT
) {
322 sprintf(data_buffer
, "0 %s", "Transfer Timeout");
323 } else if ( ret
== -EREMOTEIO
) {
324 sprintf(data_buffer
, "0 %s", "Ack Error");
326 sprintf(data_buffer
, "0 %s", "unknow error");
328 printk("Actual return Value:%d %p\n",ret
, vir_addr
);
331 if (trans_mode
== 1 && vir_addr
!= NULL
) {/*DMA MODE*/
332 dma_free_coherent(NULL
, length
>> 1, vir_addr
, dma_addr
);
339 struct i2c_adapter
*adap
= i2c_get_adapter(bus_id
);
340 mt_i2c
*i2c
= i2c_get_adapdata(adap
);
344 struct i2c_adapter
*adap
= i2c_get_adapter(bus_id
);
345 mt_i2c
*i2c
= i2c_get_adapdata(adap
);
348 }else if(operation
== 4){
349 i2c_test_reg(bus_id
,0);
351 i2c_test_reg(bus_id
,0xFFFFFFFF);
353 }else if(operation
== 5){
354 i2c_ext_conf_test(bus_id
,address
);
355 }else if(operation
== 9){
356 i2c_soft_reset(bus_id
);
358 }else if(operation
== 6){
360 //I2C0 PINMUX2 power on
361 #ifdef CONFIG_MTK_PMIC_MT6397
363 hwPowerOn(MT65XX_POWER_LDO_VMC1
,VOL_DEFAULT
,"i2c_pinmux");
364 hwPowerOn(MT65XX_POWER_LDO_VMCH1
,VOL_DEFAULT
,"i2c_pinmux");
368 }else if(operation
== 7){
371 dev_err(dev
, "i2c debug system: Parameter invalid!\n");
377 /*parameter invalid*/
378 dev_err(dev
, "i2c debug system: Parameter invalid!\n");
383 printk("analyze failed\n");
387 static DEVICE_ATTR(ut
, 660, show_config
, set_config
);
389 static int __init
i2c_common_probe(struct platform_device
*pdev
)
392 //your code here£¬your should save client in your own way
393 printk(KERN_ALERT
"i2c_common device probe\n");
394 ret
= device_create_file(&pdev
->dev
, &dev_attr_ut
);
398 static int __exit
i2c_common_remove(struct platform_device
*pdev
)
402 device_remove_file(&pdev
->dev
, &dev_attr_ut
);
406 static struct platform_driver i2c_common_driver
= {
409 .owner
= THIS_MODULE
,
412 .probe
= i2c_common_probe
,
413 .remove
= i2c_common_remove
,
418 static struct platform_device i2c_common_device
= {
422 static int __init
xxx_init( void )
424 printk(KERN_ALERT
"i2c_common device init\n");
425 platform_device_register(&i2c_common_device
);
426 return platform_driver_register(&i2c_common_driver
);
429 static void __exit
xxx_exit(void)
431 platform_driver_unregister(&i2c_common_driver
);
432 platform_device_unregister(&i2c_common_device
);
435 module_init( xxx_init
);
436 module_exit( xxx_exit
);
439 MODULE_LICENSE("GPL");
440 MODULE_DESCRIPTION("MediaTek I2C Bus Driver Test Driver");
441 MODULE_AUTHOR("Ranran Lu");