I tried out this code but the pins still dont work on the p1021.
What am I missing here?
Thanks
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/mm.h>
static struct MyGPIO* MyGPIO;
static const char* MODULENAME = "P1021_GPIO";
static const u32 ccsr_gut_base = 0xffee0000;
static const u32 ccsr_gut_length = 0x1000;
/*
* @gpio:
* @value: 0..2
* 0: disabled
* 1: out
* 2: in
* 3: in and out
*/
int MyGPIO_SetDirection(unsigned int gpio, u8 value)
{
u8 status = -1;
u32 tmp, offset;
spin_lock(&MyGPIO->gpio_lock);
offset = 0x128;
switch(value){
case 0:
break;
case 1: //out 01
tmp = in_be32(MyGPIO->gpio_base + offset);
tmp &= ~(1<<(31 - (gpio * 2))); //
tmp |= (1<<(30 - (gpio * 2))); //
out_be32(MyGPIO->gpio_base + offset, tmp);
tmp = in_be32(MyGPIO->gpio_base + offset);
if(tmp & (1<<(30 - (gpio * 2))))
status = 1;
else {
printk(KERN_ERR "%s gpio nr %d direction write error\n", __func__, gpio);
status = -2;
}
break;
case 2: //in 10
tmp = in_be32(MyGPIO->gpio_base + offset);
tmp |= (1<<(31 - (gpio * 2))); //
tmp &= ~(1<<(30 - (gpio * 2))); //
out_be32(MyGPIO->gpio_base + offset, tmp);
tmp = in_be32(MyGPIO->gpio_base + offset);
if(tmp & (1<<(31 - (gpio * 2))))
status = 1;
else {
printk(KERN_ERR "%s gpio nr %d direction write error\n", __func__, gpio);
status = -2;
}
break;
case 3: //in and out 11
tmp = in_be32(MyGPIO->gpio_base + offset);
tmp |= (1<<(31 - (gpio * 2))); //
tmp |= (1<<(30 - (gpio * 2))); //
out_be32(MyGPIO->gpio_base + offset, tmp);
tmp = in_be32(MyGPIO->gpio_base + offset);
if((tmp & (1<<(31 - (gpio * 2)))) && (tmp & (1<<(30 - (gpio * 2)))))
status = 1;
else {
printk(KERN_ERR "%s gpio nr %d direction write error\n", __func__, gpio);
status = -2;
}
break;
default:
break;
}
spin_unlock(&MyGPIO->gpio_lock);
return status;
}
/*
*/
int MyGPIO_write(const unsigned int _gpio, bool value)
{
u8 status;
u32 tmp, offset;
u32 gpio;
spin_lock(&MyGPIO->gpio_lock);
gpio = _gpio;
offset = 0x124;
tmp = in_be32(MyGPIO->gpio_base + offset);
if(value){
tmp |= (1<<(31 - gpio)); //
out_be32((MyGPIO->gpio_base + offset), tmp);
tmp = in_be32(MyGPIO->gpio_base + offset);
if((tmp & (1<<(31 - gpio))) == (1<<(31-gpio)))
status = 1;
else {
printk(KERN_ERR "%s gpio nr %d write error\n", __func__, gpio);
status = -2;
}
} else {
tmp &= ~(1<<(31 - gpio)); //
out_be32(MyGPIO->gpio_base + offset, tmp);
tmp = in_be32(MyGPIO->gpio_base + offset);
if((tmp & (1<<(31 - gpio))) == 0)
status = 1;
else {
printk(KERN_ERR "%s gpio nr %d write error\n", __func__, gpio);
status = -2;
}
}
spin_unlock(&MyGPIO->gpio_lock);
return status;
}
static int __init gpio_init(void)
{
......
spin_lock_init(&MyGPIO->gpio_lock);
/* map GUT bus memory into CPU space for use */
MyGPIO->gpio_base = ioremap(ccsr_gut_base, ccsr_gut_length);
if(!MyGPIO->gpio_base){
printk(KERN_INFO "can't ioremap region\r\n");
return -1;
}
printk(KERN_INFO "gpio_base: 0x%08X\r\n", (u32)(MyGPIO->gpio_base));
/* Init Gpio directions */
MyGPIO_SetDirection(0, 2); //PB0 out
MyGPIO_SetDirection(1, 2); //PB1 out
MyGPIO_SetDirection(6, 2); //PB6
MyGPIO_SetDirection(4, 2); //PB4
/***** Write 1 to PB4 fail !!!!!!!!!!!!!!!! *****************/
MyGPIO_write(4, 1)
return 0;
}
static void __exit gpio_exit(void)
{
printk(KERN_INFO "gpio module exit called\n");
/* iounmap */
iounmap(MyGPIO->gpio_base);
return;
}
module_init( gpio_init);
module_exit( gpio_exit );
//Meta information
MODULE_AUTHOR("Alfonsox");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Freescale P1021 GPIO Init Module!");
Thanks in advance for your Reply!!