static int ak8975_probe(struct i2c_client *client, const struct i2c_device_id *id) { struct ak8975_data *data; int err; /* Allocate our device context. */ data = kzalloc(sizeof(struct ak8975_data), GFP_KERNEL); if (!data) { dev_err(&client->dev, "Memory allocation fails\n"); err = -ENOMEM; goto exit; } i2c_set_clientdata(client, data); data->client = client; mutex_init(&data->lock); /* Grab and set up the supplied GPIO. */ data->eoc_irq = client->irq; data->eoc_gpio = irq_to_gpio(client->irq); /* We may not have a GPIO based IRQ to scan, that is fine, we will poll if so */ if (data->eoc_gpio > 0) { err = gpio_request(data->eoc_gpio, "ak_8975"); if (err < 0) { dev_err(&client->dev, "failed to request GPIO %d, error %d\n", data->eoc_gpio, err); goto exit_free; } err = gpio_direction_input(data->eoc_gpio); if (err < 0) { dev_err(&client->dev, "Failed to configure input direction for GPIO %d, error %d\n", data->eoc_gpio, err); goto exit_gpio; } } else data->eoc_gpio = 0; /* No GPIO available */ /* Perform some basic start-of-day setup of the device. */ err = ak8975_setup(client); if (err < 0) { dev_err(&client->dev, "AK8975 initialization fails\n"); goto exit_gpio; } /* Register with IIO */ data->indio_dev = iio_allocate_device(0); if (data->indio_dev == NULL) { err = -ENOMEM; goto exit_gpio; } data->indio_dev->dev.parent = &client->dev; data->indio_dev->info = &ak8975_info; data->indio_dev->dev_data = (void *)(data); data->indio_dev->modes = INDIO_DIRECT_MODE; err = iio_device_register(data->indio_dev); if (err < 0) goto exit_free_iio; return 0; exit_free_iio: iio_free_device(data->indio_dev); exit_gpio: if (data->eoc_gpio) gpio_free(data->eoc_gpio); exit_free: kfree(data); exit: return err; }
static int ak8975_probe(struct i2c_client *client, const struct i2c_device_id *id) { struct ak8975_data *data; struct iio_dev *indio_dev; int eoc_gpio; int err; /* Grab and set up the supplied GPIO. */ if (client->dev.platform_data == NULL) eoc_gpio = -1; else eoc_gpio = *(int *)(client->dev.platform_data); /* We may not have a GPIO based IRQ to scan, that is fine, we will poll if so */ if (gpio_is_valid(eoc_gpio)) { err = gpio_request_one(eoc_gpio, GPIOF_IN, "ak_8975"); if (err < 0) { dev_err(&client->dev, "failed to request GPIO %d, error %d\n", eoc_gpio, err); goto exit; } } /* Register with IIO */ indio_dev = iio_device_alloc(sizeof(*data)); if (indio_dev == NULL) { err = -ENOMEM; goto exit_gpio; } data = iio_priv(indio_dev); i2c_set_clientdata(client, indio_dev); /* Perform some basic start-of-day setup of the device. */ err = ak8975_setup(client); if (err < 0) { dev_err(&client->dev, "AK8975 initialization fails\n"); goto exit_free_iio; } data->client = client; mutex_init(&data->lock); data->eoc_gpio = eoc_gpio; indio_dev->dev.parent = &client->dev; indio_dev->channels = ak8975_channels; indio_dev->num_channels = ARRAY_SIZE(ak8975_channels); indio_dev->info = &ak8975_info; indio_dev->modes = INDIO_DIRECT_MODE; err = iio_device_register(indio_dev); if (err < 0) goto exit_free_iio; return 0; exit_free_iio: iio_device_free(indio_dev); exit_gpio: if (gpio_is_valid(eoc_gpio)) gpio_free(eoc_gpio); exit: return err; }
static int ak8975_probe(struct i2c_client *client, const struct i2c_device_id *id) { struct ak8975_data *data; struct iio_dev *indio_dev; int eoc_gpio; int err; char *name = NULL; /* Grab and set up the supplied GPIO. */ if (client->dev.platform_data) eoc_gpio = *(int *)(client->dev.platform_data); else if (client->dev.of_node) eoc_gpio = of_get_gpio(client->dev.of_node, 0); else eoc_gpio = -1; if (eoc_gpio == -EPROBE_DEFER) return -EPROBE_DEFER; /* We may not have a GPIO based IRQ to scan, that is fine, we will poll if so */ if (gpio_is_valid(eoc_gpio)) { err = devm_gpio_request_one(&client->dev, eoc_gpio, GPIOF_IN, "ak_8975"); if (err < 0) { dev_err(&client->dev, "failed to request GPIO %d, error %d\n", eoc_gpio, err); return err; } } /* Register with IIO */ indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); if (indio_dev == NULL) return -ENOMEM; data = iio_priv(indio_dev); i2c_set_clientdata(client, indio_dev); data->client = client; data->eoc_gpio = eoc_gpio; data->eoc_irq = 0; /* id will be NULL when enumerated via ACPI */ if (id) { data->chipset = (enum asahi_compass_chipset)(id->driver_data); name = (char *) id->name; } else if (ACPI_HANDLE(&client->dev)) name = ak8975_match_acpi_device(&client->dev, &data->chipset); else return -ENOSYS; dev_dbg(&client->dev, "Asahi compass chip %s\n", name); /* Perform some basic start-of-day setup of the device. */ err = ak8975_setup(client); if (err < 0) { dev_err(&client->dev, "AK8975 initialization fails\n"); return err; } data->client = client; mutex_init(&data->lock); data->eoc_gpio = eoc_gpio; indio_dev->dev.parent = &client->dev; indio_dev->channels = ak8975_channels; indio_dev->num_channels = ARRAY_SIZE(ak8975_channels); indio_dev->info = &ak8975_info; indio_dev->modes = INDIO_DIRECT_MODE; indio_dev->name = name; err = devm_iio_device_register(&client->dev, indio_dev); if (err < 0) return err; return 0; }