Пример #1
0
int main() {
	int index = gpio_name_to_index("p2.10");

	printf("p2.10 = %d\n", index);
	printf("%d = %s\n", index, gpio_index_to_name(index));

	printf("foo = %d\n", gpio_name_to_index("foo"));
	printf("%d = %s\n", ARRAY_SIZE(gpio_names), gpio_index_to_name(ARRAY_SIZE(gpio_names)));

	return 0;
}
Пример #2
0
static int hall_ak8789_device_init(struct platform_device *pdev,
		struct hall_dev *phall_dev)
{
	int err = 0;
	int i = 0;
	int gpio_id = 0;
	int gpio_value = 0;
	int hall_type_err = 0;
	char gpio_name[MAX_GPIO_NAME_LEN];
        struct device_node *hall_ak8789_node = NULL;
	struct device *dev = NULL;
	struct hall_ak8789_dev *hall_ak8789 = NULL;

	if(NULL == phall_dev || NULL == pdev) {
		hwlog_err("[ak8789][%s] invalid pointer!\n", __func__);
		return -EINVAL;
	}

	hall_ak8789 = kzalloc(sizeof(struct hall_ak8789_dev), GFP_KERNEL);
	if(!hall_ak8789) {
		hwlog_err("[hall][%s]hall_dev kmalloc error!\n", __func__);
		err = -ENOMEM;
		return err;
	}

	dev = &pdev->dev;
	hall_ak8789_node = dev->of_node;
	hall_ak8789->hall_ak8789_type = phall_dev->hall_type;
	for (i = 0; i < AK8789_GPIO_NUM; i++) {
		hall_ak8789->gpio_poles[i] = 0;
	}

      switch (hall_ak8789->hall_ak8789_type) {
		case single_north_pole:
			hall_ak8789->gpio_poles[0] = of_get_named_gpio(hall_ak8789_node
				, NORTH_POLE_NAME, 0);;
			break;
		case single_south_pole:
			hall_ak8789->gpio_poles[1] = of_get_named_gpio(hall_ak8789_node
				, SOUTH_POLE_NAME, 0);
			break;
		case double_poles:
			hall_ak8789->gpio_poles[0] = of_get_named_gpio(hall_ak8789_node
				, NORTH_POLE_NAME, 0);
			hall_ak8789->gpio_poles[1] = of_get_named_gpio(hall_ak8789_node
				, SOUTH_POLE_NAME, 0);
			break;
		default:
			hwlog_err("[hall][%s]Invalid hall type !\n", __func__);
			kfree(hall_ak8789);
			hall_type_err = -1;
			return hall_type_err;
	}

	for (i = 0; i < AK8789_GPIO_NUM; i++) {
		gpio_id = hall_ak8789->gpio_poles[i];
		if(gpio_id) {
			gpio_index_to_name(i, gpio_name);
			err = setup_gpio(gpio_id, gpio_name);
			if(err) {
                                hwlog_err("[hall][%s] gpio %d direction input err %d\n",
					__func__, gpio_id, err);
                     }

			gpio_value = gpio_get_value(gpio_id);
			phall_dev->irq_info[i].irq = gpio_to_irq(gpio_id);
			phall_dev->irq_info[i].trigger_value = (gpio_value == GPIO_LOW_VOLTAGE)
				? IRQF_TRIGGER_HIGH : IRQF_TRIGGER_LOW;
			strcpy(phall_dev->irq_info[i].name, gpio_name);
		} else {
			phall_dev->irq_info[i].irq = 0;
			phall_dev->irq_info[i].trigger_value = 0;
		}
	}

	phall_dev->hall_device = hall_ak8789;
	return err;
}