分享

sysfs接口函数DEVICE

 barry525 2015-07-31


/*
 * This module shows how to create a simple subdirectory in sysfs called
 * /sys/kernel/kobject-example  In that directory, 3 files are created:
 * "foo", "baz", and "bar".  If an integer is written to these files, it can be
 * later read out of it.
 */

static int foo;

/*
 * The "foo" file where a static variable is read from and written to.
 */

static struct msm_gpio qup_i2c_gpios_io[] = {
     { GPIO_CFG(60, 0, GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL, GPIO_CFG_8MA),
           "qup_scl" },
     { GPIO_CFG(61, 0, GPIO_CFG_INPUT, GPIO_CFG_NO_PULL, GPIO_CFG_8MA),
           "qup_sda" },
     { GPIO_CFG(131, 0, GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL, GPIO_CFG_8MA),
           "qup_scl" },
     { GPIO_CFG(132, 0, GPIO_CFG_INPUT, GPIO_CFG_NO_PULL, GPIO_CFG_8MA),
           "qup_sda" },
};

static struct msm_gpio qup_i2c_gpios_hw[] = {
     { GPIO_CFG(60, 1, GPIO_CFG_INPUT, GPIO_CFG_NO_PULL, GPIO_CFG_8MA),
           "qup_scl" },
     { GPIO_CFG(61, 1, GPIO_CFG_INPUT, GPIO_CFG_NO_PULL, GPIO_CFG_8MA),
           "qup_sda" },
     { GPIO_CFG(131, 2, GPIO_CFG_INPUT, GPIO_CFG_NO_PULL, GPIO_CFG_8MA),
           "qup_scl" },
     { GPIO_CFG(132, 2, GPIO_CFG_INPUT, GPIO_CFG_NO_PULL, GPIO_CFG_8MA),
           "qup_sda" },
};

static void gsbi_qup_i2c_gpio_config(int adap_id, int config_type)
{
     int rc;

     if (adap_id < 0 || adap_id > 1)
           return;

     /* Each adapter gets 2 lines from the table */
     if (config_type)
           rc = msm_gpios_enable(&qup_i2c_gpios_hw[adap_id*2], 2);
     else
           rc = msm_gpios_enable(&qup_i2c_gpios_io[adap_id*2], 2);
     if (rc < 0)
           pr_err("QUP GPIO request/enable failed: %d\n", rc);
}

static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
    char *buf)
{
      return sprintf(buf, "%d\n", foo);
}

static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
      const char *buf, size_t count)
{
     int sda,scl;
     int i;
     sscanf(buf, "%du", &foo);
     printk("foo = %d.\n",foo);

     foo = foo -1;

     if  (foo < 0 || foo > 1)
     {
           printk("input foo error. foo=%d.\n",foo);
           return 0;
     }
           
      sda = GPIO_PIN((&qup_i2c_gpios_hw[foo*2+1])->gpio_cfg);
      scl = GPIO_PIN((&qup_i2c_gpios_hw[foo*2])->gpio_cfg);
      
     printk("sda = %d.\n",sda);
     printk("scl = %d.\n",scl);
     
     gsbi_qup_i2c_gpio_config(foo,0);

     for(i=0;i<9;i++)
     {
           if(gpio_get_value(sda))
           {
                printk("sda of i2c%d is high when %d pulse is output on scl.\n",foo,i);
                break;
           }
           gpio_set_value(scl,0);
           udelay(5);
           gpio_set_value(scl,1);
           udelay(5);
     }
     
     gsbi_qup_i2c_gpio_config(foo,1);

     printk("finish.\n");
     return count;
}

static struct kobj_attribute foo_attribute =
      __ATTR(i2c_unlock, 0666, foo_show, foo_store);

/*
 * Create a group of attributes so that we can create and destroy them all
 * at once.
 */
static struct attribute *attrs[] = {
      &foo_attribute.attr,
      NULL,?  /* need to NULL terminate the list of attributes */
};

/*
 * An unnamed attribute group will put all of the attributes directly in
 * the kobject directory.  If we specify a name, a subdirectory will be
 * created for the attributes with the directory being the name of the
 * attribute group.
 */
static struct attribute_group attr_group = {
      .attrs = attrs,
};

static struct kobject *example_kobj;

static int __init example_init(void)
{
     int retval;

     /*
      * Create a simple kobject with the name of "kobject_example",
      * located under /sys/kernel/
      *
      * As this is a simple directory, no uevent will be sent to
      * userspace.  That is why this function should not be used for
      * any type of dynamic kobjects, where the name and number are
      * not known ahead of time.
      */
     example_kobj = kobject_create_and_add("i2c_recovery", kernel_kobj);
     if (!example_kobj)
          return -ENOMEM;

     /* Create the files associated with this kobject */
     retval = sysfs_create_group(example_kobj, &attr_group);
     if (retval)
          kobject_put(example_kobj);

     return retval;
}

static void __exit example_exit(void)
{
     kobject_put(example_kobj);
}

module_init(example_init);
module_exit(example_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Wupeng");

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多