static int sx150x_probe()

in pinctrl-sx150x.c [1097:1248]


static int sx150x_probe(struct i2c_client *client,
			const struct i2c_device_id *id)
{
	static const u32 i2c_funcs = I2C_FUNC_SMBUS_BYTE_DATA |
				     I2C_FUNC_SMBUS_WRITE_WORD_DATA;
	struct device *dev = &client->dev;
	struct sx150x_pinctrl *pctl;
	int ret;

	if (!i2c_check_functionality(client->adapter, i2c_funcs))
		return -ENOSYS;

	pctl = devm_kzalloc(dev, sizeof(*pctl), GFP_KERNEL);
	if (!pctl)
		return -ENOMEM;

	i2c_set_clientdata(client, pctl);

	pctl->dev = dev;
	pctl->client = client;

	if (dev->of_node)
		pctl->data = of_device_get_match_data(dev);
	else
		pctl->data = (struct sx150x_device_data *)id->driver_data;

	if (!pctl->data)
		return -EINVAL;

	pctl->regmap = devm_regmap_init(dev, NULL, pctl,
					&sx150x_regmap_config);
	if (IS_ERR(pctl->regmap)) {
		ret = PTR_ERR(pctl->regmap);
		dev_err(dev, "Failed to allocate register map: %d\n",
			ret);
		return ret;
	}

	mutex_init(&pctl->lock);

	ret = sx150x_init_hw(pctl);
	if (ret)
		return ret;

	/* Pinctrl_desc */
	pctl->pinctrl_desc.name = "sx150x-pinctrl";
	pctl->pinctrl_desc.pctlops = &sx150x_pinctrl_ops;
	pctl->pinctrl_desc.confops = &sx150x_pinconf_ops;
	pctl->pinctrl_desc.pins = pctl->data->pins;
	pctl->pinctrl_desc.npins = pctl->data->npins;
	pctl->pinctrl_desc.owner = THIS_MODULE;

	ret = devm_pinctrl_register_and_init(dev, &pctl->pinctrl_desc,
					     pctl, &pctl->pctldev);
	if (ret) {
		dev_err(dev, "Failed to register pinctrl device\n");
		return ret;
	}

	/* Register GPIO controller */
	pctl->gpio.base = -1;
	pctl->gpio.ngpio = pctl->data->npins;
	pctl->gpio.get_direction = sx150x_gpio_get_direction;
	pctl->gpio.direction_input = sx150x_gpio_direction_input;
	pctl->gpio.direction_output = sx150x_gpio_direction_output;
	pctl->gpio.get = sx150x_gpio_get;
	pctl->gpio.set = sx150x_gpio_set;
	pctl->gpio.set_config = gpiochip_generic_config;
	pctl->gpio.parent = dev;
	pctl->gpio.can_sleep = true;
	pctl->gpio.label = devm_kstrdup(dev, client->name, GFP_KERNEL);
	if (!pctl->gpio.label)
		return -ENOMEM;

	/*
	 * Setting multiple pins is not safe when all pins are not
	 * handled by the same regmap register. The oscio pin (present
	 * on the SX150X_789 chips) lives in its own register, so
	 * would require locking that is not in place at this time.
	 */
	if (pctl->data->model != SX150X_789)
		pctl->gpio.set_multiple = sx150x_gpio_set_multiple;

	/* Add Interrupt support if an irq is specified */
	if (client->irq > 0) {
		struct gpio_irq_chip *girq;

		pctl->irq_chip.irq_mask = sx150x_irq_mask;
		pctl->irq_chip.irq_unmask = sx150x_irq_unmask;
		pctl->irq_chip.irq_set_type = sx150x_irq_set_type;
		pctl->irq_chip.irq_bus_lock = sx150x_irq_bus_lock;
		pctl->irq_chip.irq_bus_sync_unlock = sx150x_irq_bus_sync_unlock;
		pctl->irq_chip.name = devm_kstrdup(dev, client->name,
						   GFP_KERNEL);
		if (!pctl->irq_chip.name)
			return -ENOMEM;

		pctl->irq.masked = ~0;
		pctl->irq.sense = 0;

		/*
		 * Because sx150x_irq_threaded_fn invokes all of the
		 * nested interrupt handlers via handle_nested_irq,
		 * any "handler" assigned to struct gpio_irq_chip
		 * below is going to be ignored, so the choice of the
		 * function does not matter that much.
		 *
		 * We set it to handle_bad_irq to avoid confusion,
		 * plus it will be instantly noticeable if it is ever
		 * called (should not happen)
		 */
		girq = &pctl->gpio.irq;
		girq->chip = &pctl->irq_chip;
		/* This will let us handle the parent IRQ in the driver */
		girq->parent_handler = NULL;
		girq->num_parents = 0;
		girq->parents = NULL;
		girq->default_type = IRQ_TYPE_NONE;
		girq->handler = handle_bad_irq;
		girq->threaded = true;

		ret = devm_request_threaded_irq(dev, client->irq, NULL,
						sx150x_irq_thread_fn,
						IRQF_ONESHOT | IRQF_SHARED |
						IRQF_TRIGGER_FALLING,
						pctl->irq_chip.name, pctl);
		if (ret < 0)
			return ret;
	}

	ret = devm_gpiochip_add_data(dev, &pctl->gpio, pctl);
	if (ret)
		return ret;

	/*
	 * Pin control functions need to be enabled AFTER registering the
	 * GPIO chip because sx150x_pinconf_set() calls
	 * sx150x_gpio_direction_output().
	 */
	ret = pinctrl_enable(pctl->pctldev);
	if (ret) {
		dev_err(dev, "Failed to enable pinctrl device\n");
		return ret;
	}

	ret = gpiochip_add_pin_range(&pctl->gpio, dev_name(dev),
				     0, 0, pctl->data->npins);
	if (ret)
		return ret;

	return 0;
}