Chinaunix首页 | 论坛 | 博客
  • 博客访问: 128002
  • 博文数量: 16
  • 博客积分: 355
  • 博客等级: 一等列兵
  • 技术积分: 237
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-01 22:01
文章分类

全部博文(16)

文章存档

2012年(16)

我的朋友

分类: LINUX

2012-06-20 00:04:43


点击(此处)折叠或打开

  1. probe:
  2. ret = sysfs_create_group(&client->dev.kobj, &adv7180_group);
  3. adv7180_dev_class = class_create(THIS_MODULE, "adv7180-dev");

  4.     struct device *dev;
  5.     dev = device_create(adv7180_dev_class, NULL,
  6.         MKDEV(ADV7180_MAJOR, 0),
  7.         NULL,
  8.         "adv7180fs");

点击(此处)折叠或打开

  1. static struct class *adv7180_dev_class;

  2. enum {
  3.     MMA_REG_R = 0,
  4.     MMA_REG_W,
  5.     MMA_CMD_MAX
  6. };

  7. static char *command[MMA_CMD_MAX] = {
  8.     [MMA_REG_R] = "readreg",
  9.     [MMA_REG_W] = "writereg",
  10. };

  11. static void parse_arg(const char *arg, int *reg, int *value)
  12. {
  13.     const char *p;

  14.     for (p = arg;; p++) {
  15.         if (*p == ' ' || *p == '\0')
  16.             break;
  17.     }

  18.     p++;

  19.     *reg = simple_strtoul(arg, NULL, 16);
  20.     *value = simple_strtoul(p, NULL, 16);
  21. }

  22. static void cmd_read_reg(const char *arg)
  23. {
  24.     int reg, value, ret;

  25.     parse_arg(arg, &reg, &value);
  26.     ret = adv7180_read(reg);
  27.     dev_info(&adv7180_data.i2c_client->dev, "read reg 0x%x = 0x%x\n", reg, ret);
  28. }

  29. static void cmd_write_reg(const char *arg)
  30. {
  31.     int reg, value, ret;

  32.     parse_arg(arg, &reg, &value);
  33.     ret = adv7180_write_reg(reg, value);
  34.     dev_info(&adv7180_data.i2c_client->dev, "write reg 0x%x = 0x%x result %s\n", reg, value, ret ? "failed" : "success");
  35. }

  36. static int exec_command(const char *buf, size_t count)
  37. {
  38.     const char *p, *s;
  39.     const char *arg;
  40.     int i, value = 0;

  41.     for (p = buf;; p++) {
  42.         if (*p == ' ' || *p == '\0' || p - buf >= count)
  43.             break;
  44.     }
  45.     arg = p + 1;

  46.     for (i = MMA_REG_R; i < MMA_CMD_MAX; i++) {
  47.         s = command[i];
  48.         if (s && !strncmp(buf, s, p - buf)) {
  49.             dev_info(&adv7180_data.i2c_client->dev, "command %s\n", s);
  50.             goto mma_exec_command;
  51.         }
  52.     }

  53.     dev_err(&adv7180_data.i2c_client->dev, "command is not found\n");
  54.     return -1;

  55. mma_exec_command:
  56.     switch (i) {
  57.     case MMA_REG_R:
  58.         cmd_read_reg(arg);
  59.         break;
  60.     case MMA_REG_W:
  61.         cmd_write_reg(arg);
  62.         break;
  63.     default:
  64.         dev_err(&adv7180_data.i2c_client->dev, "command is not found\n");
  65.         break;
  66.     }

  67.     return 0;
  68. }

  69. void adv7180_print_reg(u8 reg)
  70. {
  71.     int val = adv7180_read(reg);
  72.     printk("reg:0x%x val:0x%x\n", reg, val);
  73. }

  74. static ssize_t show_adv7180regs(struct device *dev,
  75.              struct device_attribute *attr, char *buf)
  76. {
  77.     int ret, reg;

  78.     adv7180_print_reg(0x31);
  79.     adv7180_print_reg(0x32);
  80.     adv7180_print_reg(0x33);
  81.     adv7180_print_reg(0x34);
  82.     adv7180_print_reg(0x35);
  83.     adv7180_print_reg(0x36);
  84.     adv7180_print_reg(0x37);
  85.     adv7180_print_reg(0xe8);
  86.     adv7180_print_reg(0xe9);
  87.     adv7180_print_reg(0xea);

  88.     return 0;
  89. }

  90. static ssize_t write_adv7180regs(struct device *dev,
  91.              struct device_attribute *attr, const char *buf,
  92.              size_t count)
  93. {
  94.     exec_command(buf, count);

  95.     return count;
  96. }

  97. static DEVICE_ATTR(adv7180_regs, S_IWUSR | S_IRUGO, show_adv7180regs, write_adv7180regs);

  98. static struct attribute *adv7180_attributes[] = {
  99.     &dev_attr_adv7180_regs.attr,
  100.     NULL
  101. };

  102. static const struct attribute_group adv7180_group = {
  103.     .attrs = adv7180_attributes,
  104. };

阅读(1534) | 评论(0) | 转发(0) |
0

上一篇:GPIO模拟I2C

下一篇:linux sysfs实现hdmi开关

给主人留下些什么吧!~~