1*872e79adSDenis Ciocca /* 2*872e79adSDenis Ciocca * STMicroelectronics magnetometers driver 3*872e79adSDenis Ciocca * 4*872e79adSDenis Ciocca * Copyright 2012-2013 STMicroelectronics Inc. 5*872e79adSDenis Ciocca * 6*872e79adSDenis Ciocca * Denis Ciocca <denis.ciocca@st.com> 7*872e79adSDenis Ciocca * 8*872e79adSDenis Ciocca * Licensed under the GPL-2. 9*872e79adSDenis Ciocca */ 10*872e79adSDenis Ciocca 11*872e79adSDenis Ciocca #include <linux/kernel.h> 12*872e79adSDenis Ciocca #include <linux/module.h> 13*872e79adSDenis Ciocca #include <linux/slab.h> 14*872e79adSDenis Ciocca #include <linux/spi/spi.h> 15*872e79adSDenis Ciocca #include <linux/iio/iio.h> 16*872e79adSDenis Ciocca #include <linux/iio/trigger.h> 17*872e79adSDenis Ciocca 18*872e79adSDenis Ciocca #include <linux/iio/common/st_sensors.h> 19*872e79adSDenis Ciocca #include <linux/iio/common/st_sensors_spi.h> 20*872e79adSDenis Ciocca #include "st_magn.h" 21*872e79adSDenis Ciocca 22*872e79adSDenis Ciocca static int st_magn_spi_probe(struct spi_device *spi) 23*872e79adSDenis Ciocca { 24*872e79adSDenis Ciocca struct iio_dev *indio_dev; 25*872e79adSDenis Ciocca struct st_sensor_data *mdata; 26*872e79adSDenis Ciocca int err; 27*872e79adSDenis Ciocca 28*872e79adSDenis Ciocca indio_dev = iio_device_alloc(sizeof(*mdata)); 29*872e79adSDenis Ciocca if (indio_dev == NULL) { 30*872e79adSDenis Ciocca err = -ENOMEM; 31*872e79adSDenis Ciocca goto iio_device_alloc_error; 32*872e79adSDenis Ciocca } 33*872e79adSDenis Ciocca 34*872e79adSDenis Ciocca mdata = iio_priv(indio_dev); 35*872e79adSDenis Ciocca mdata->dev = &spi->dev; 36*872e79adSDenis Ciocca 37*872e79adSDenis Ciocca st_sensors_spi_configure(indio_dev, spi, mdata); 38*872e79adSDenis Ciocca 39*872e79adSDenis Ciocca err = st_magn_common_probe(indio_dev); 40*872e79adSDenis Ciocca if (err < 0) 41*872e79adSDenis Ciocca goto st_magn_common_probe_error; 42*872e79adSDenis Ciocca 43*872e79adSDenis Ciocca return 0; 44*872e79adSDenis Ciocca 45*872e79adSDenis Ciocca st_magn_common_probe_error: 46*872e79adSDenis Ciocca iio_device_free(indio_dev); 47*872e79adSDenis Ciocca iio_device_alloc_error: 48*872e79adSDenis Ciocca return err; 49*872e79adSDenis Ciocca } 50*872e79adSDenis Ciocca 51*872e79adSDenis Ciocca static int st_magn_spi_remove(struct spi_device *spi) 52*872e79adSDenis Ciocca { 53*872e79adSDenis Ciocca struct iio_dev *indio_dev = spi_get_drvdata(spi); 54*872e79adSDenis Ciocca st_magn_common_remove(indio_dev); 55*872e79adSDenis Ciocca 56*872e79adSDenis Ciocca return 0; 57*872e79adSDenis Ciocca } 58*872e79adSDenis Ciocca 59*872e79adSDenis Ciocca static const struct spi_device_id st_magn_id_table[] = { 60*872e79adSDenis Ciocca { LSM303DLHC_MAGN_DEV_NAME }, 61*872e79adSDenis Ciocca { LSM303DLM_MAGN_DEV_NAME }, 62*872e79adSDenis Ciocca { LIS3MDL_MAGN_DEV_NAME }, 63*872e79adSDenis Ciocca {}, 64*872e79adSDenis Ciocca }; 65*872e79adSDenis Ciocca MODULE_DEVICE_TABLE(spi, st_magn_id_table); 66*872e79adSDenis Ciocca 67*872e79adSDenis Ciocca static struct spi_driver st_magn_driver = { 68*872e79adSDenis Ciocca .driver = { 69*872e79adSDenis Ciocca .owner = THIS_MODULE, 70*872e79adSDenis Ciocca .name = "st-magn-spi", 71*872e79adSDenis Ciocca }, 72*872e79adSDenis Ciocca .probe = st_magn_spi_probe, 73*872e79adSDenis Ciocca .remove = st_magn_spi_remove, 74*872e79adSDenis Ciocca .id_table = st_magn_id_table, 75*872e79adSDenis Ciocca }; 76*872e79adSDenis Ciocca module_spi_driver(st_magn_driver); 77*872e79adSDenis Ciocca 78*872e79adSDenis Ciocca MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); 79*872e79adSDenis Ciocca MODULE_DESCRIPTION("STMicroelectronics magnetometers spi driver"); 80*872e79adSDenis Ciocca MODULE_LICENSE("GPL v2"); 81