root/drivers/iio/common/st_sensors/st_sensors_spi.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. st_sensors_is_spi_3_wire
  2. st_sensors_configure_spi_3_wire
  3. st_sensors_spi_configure

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  * STMicroelectronics sensors spi library 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/iio/iio.h>
  14 #include <linux/regmap.h>
  15 
  16 #include <linux/iio/common/st_sensors_spi.h>
  17 #include "st_sensors_core.h"
  18 
  19 #define ST_SENSORS_SPI_MULTIREAD        0xc0
  20 
  21 static const struct regmap_config st_sensors_spi_regmap_config = {
  22         .reg_bits = 8,
  23         .val_bits = 8,
  24 };
  25 
  26 static const struct regmap_config st_sensors_spi_regmap_multiread_bit_config = {
  27         .reg_bits = 8,
  28         .val_bits = 8,
  29         .read_flag_mask = ST_SENSORS_SPI_MULTIREAD,
  30 };
  31 
  32 /*
  33  * st_sensors_is_spi_3_wire() - check if SPI 3-wire mode has been selected
  34  * @spi: spi device reference.
  35  *
  36  * Return: true if SPI 3-wire mode is selected, false otherwise.
  37  */
  38 static bool st_sensors_is_spi_3_wire(struct spi_device *spi)
  39 {
  40         struct device_node *np = spi->dev.of_node;
  41         struct st_sensors_platform_data *pdata;
  42 
  43         pdata = (struct st_sensors_platform_data *)spi->dev.platform_data;
  44         if ((np && of_property_read_bool(np, "spi-3wire")) ||
  45             (pdata && pdata->spi_3wire)) {
  46                 return true;
  47         }
  48 
  49         return false;
  50 }
  51 
  52 /*
  53  * st_sensors_configure_spi_3_wire() - configure SPI 3-wire if needed
  54  * @spi: spi device reference.
  55  * @settings: sensor specific settings reference.
  56  *
  57  * Return: 0 on success, else a negative error code.
  58  */
  59 static int st_sensors_configure_spi_3_wire(struct spi_device *spi,
  60                                            struct st_sensor_settings *settings)
  61 {
  62         if (settings->sim.addr) {
  63                 u8 buffer[] = {
  64                         settings->sim.addr,
  65                         settings->sim.value
  66                 };
  67 
  68                 return spi_write(spi, buffer, 2);
  69         }
  70 
  71         return 0;
  72 }
  73 
  74 /*
  75  * st_sensors_spi_configure() - configure SPI interface
  76  * @indio_dev: IIO device reference.
  77  * @spi: spi device reference.
  78  *
  79  * Return: 0 on success, else a negative error code.
  80  */
  81 int st_sensors_spi_configure(struct iio_dev *indio_dev,
  82                              struct spi_device *spi)
  83 {
  84         struct st_sensor_data *sdata = iio_priv(indio_dev);
  85         const struct regmap_config *config;
  86         int err;
  87 
  88         if (st_sensors_is_spi_3_wire(spi)) {
  89                 err = st_sensors_configure_spi_3_wire(spi,
  90                                                       sdata->sensor_settings);
  91                 if (err < 0)
  92                         return err;
  93         }
  94 
  95         if (sdata->sensor_settings->multi_read_bit)
  96                 config = &st_sensors_spi_regmap_multiread_bit_config;
  97         else
  98                 config = &st_sensors_spi_regmap_config;
  99 
 100         sdata->regmap = devm_regmap_init_spi(spi, config);
 101         if (IS_ERR(sdata->regmap)) {
 102                 dev_err(&spi->dev, "Failed to register spi regmap (%d)\n",
 103                         (int)PTR_ERR(sdata->regmap));
 104                 return PTR_ERR(sdata->regmap);
 105         }
 106 
 107         spi_set_drvdata(spi, indio_dev);
 108 
 109         indio_dev->dev.parent = &spi->dev;
 110         indio_dev->name = spi->modalias;
 111 
 112         sdata->dev = &spi->dev;
 113         sdata->irq = spi->irq;
 114 
 115         return 0;
 116 }
 117 EXPORT_SYMBOL(st_sensors_spi_configure);
 118 
 119 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
 120 MODULE_DESCRIPTION("STMicroelectronics ST-sensors spi driver");
 121 MODULE_LICENSE("GPL v2");

/* [<][>][^][v][top][bottom][index][help] */