linux/drivers/iio/magnetometer/st_magn_spi.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * STMicroelectronics magnetometers driver
   4 *
   5 * Copyright 2012-2013 STMicroelectronics Inc.
   6 *
   7 * Denis Ciocca <denis.ciocca@st.com>
   8 */
   9
  10#include <linux/kernel.h>
  11#include <linux/module.h>
  12#include <linux/slab.h>
  13#include <linux/spi/spi.h>
  14#include <linux/iio/iio.h>
  15
  16#include <linux/iio/common/st_sensors.h>
  17#include <linux/iio/common/st_sensors_spi.h>
  18#include "st_magn.h"
  19
  20#ifdef CONFIG_OF
  21/*
  22 * For new single-chip sensors use <device_name> as compatible string.
  23 * For old single-chip devices keep <device_name>-magn to maintain
  24 * compatibility
  25 * For multi-chip devices, use <device_name>-magn to distinguish which
  26 * capability is being used
  27 */
  28static const struct of_device_id st_magn_of_match[] = {
  29        {
  30                .compatible = "st,lis3mdl-magn",
  31                .data = LIS3MDL_MAGN_DEV_NAME,
  32        },
  33        {
  34                .compatible = "st,lsm303agr-magn",
  35                .data = LSM303AGR_MAGN_DEV_NAME,
  36        },
  37        {
  38                .compatible = "st,lis2mdl",
  39                .data = LIS2MDL_MAGN_DEV_NAME,
  40        },
  41        {
  42                .compatible = "st,lsm9ds1-magn",
  43                .data = LSM9DS1_MAGN_DEV_NAME,
  44        },
  45        {}
  46};
  47MODULE_DEVICE_TABLE(of, st_magn_of_match);
  48#else
  49#define st_magn_of_match        NULL
  50#endif
  51
  52static int st_magn_spi_probe(struct spi_device *spi)
  53{
  54        const struct st_sensor_settings *settings;
  55        struct st_sensor_data *mdata;
  56        struct iio_dev *indio_dev;
  57        int err;
  58
  59        st_sensors_of_name_probe(&spi->dev, st_magn_of_match,
  60                                 spi->modalias, sizeof(spi->modalias));
  61
  62        settings = st_magn_get_settings(spi->modalias);
  63        if (!settings) {
  64                dev_err(&spi->dev, "device name %s not recognized.\n",
  65                        spi->modalias);
  66                return -ENODEV;
  67        }
  68
  69        indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*mdata));
  70        if (!indio_dev)
  71                return -ENOMEM;
  72
  73        mdata = iio_priv(indio_dev);
  74        mdata->sensor_settings = (struct st_sensor_settings *)settings;
  75
  76        err = st_sensors_spi_configure(indio_dev, spi);
  77        if (err < 0)
  78                return err;
  79
  80        err = st_magn_common_probe(indio_dev);
  81        if (err < 0)
  82                return err;
  83
  84        return 0;
  85}
  86
  87static int st_magn_spi_remove(struct spi_device *spi)
  88{
  89        struct iio_dev *indio_dev = spi_get_drvdata(spi);
  90        st_magn_common_remove(indio_dev);
  91
  92        return 0;
  93}
  94
  95static const struct spi_device_id st_magn_id_table[] = {
  96        { LIS3MDL_MAGN_DEV_NAME },
  97        { LSM303AGR_MAGN_DEV_NAME },
  98        { LIS2MDL_MAGN_DEV_NAME },
  99        { LSM9DS1_MAGN_DEV_NAME },
 100        {},
 101};
 102MODULE_DEVICE_TABLE(spi, st_magn_id_table);
 103
 104static struct spi_driver st_magn_driver = {
 105        .driver = {
 106                .name = "st-magn-spi",
 107                .of_match_table = of_match_ptr(st_magn_of_match),
 108        },
 109        .probe = st_magn_spi_probe,
 110        .remove = st_magn_spi_remove,
 111        .id_table = st_magn_id_table,
 112};
 113module_spi_driver(st_magn_driver);
 114
 115MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
 116MODULE_DESCRIPTION("STMicroelectronics magnetometers spi driver");
 117MODULE_LICENSE("GPL v2");
 118