2 * STMicroelectronics st_lsm6dsx spi driver
4 * Copyright 2016 STMicroelectronics Inc.
6 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
7 * Denis Ciocca <denis.ciocca@st.com>
9 * Licensed under the GPL-2.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/spi/spi.h>
15 #include <linux/slab.h>
18 #include "st_lsm6dsx.h"
20 #define SENSORS_SPI_READ BIT(7)
22 static int st_lsm6dsx_spi_read(struct device *dev, u8 addr, int len,
25 struct spi_device *spi = to_spi_device(dev);
26 struct st_lsm6dsx_hw *hw = spi_get_drvdata(spi);
29 struct spi_transfer xfers[] = {
31 .tx_buf = hw->tb.tx_buf,
36 .rx_buf = hw->tb.rx_buf,
42 hw->tb.tx_buf[0] = addr | SENSORS_SPI_READ;
44 err = spi_sync_transfer(spi, xfers, ARRAY_SIZE(xfers));
48 memcpy(data, hw->tb.rx_buf, len * sizeof(u8));
53 static int st_lsm6dsx_spi_write(struct device *dev, u8 addr, int len,
56 struct st_lsm6dsx_hw *hw;
57 struct spi_device *spi;
59 if (len >= ST_LSM6DSX_TX_MAX_LENGTH)
62 spi = to_spi_device(dev);
63 hw = spi_get_drvdata(spi);
65 hw->tb.tx_buf[0] = addr;
66 memcpy(&hw->tb.tx_buf[1], data, len);
68 return spi_write(spi, hw->tb.tx_buf, len + 1);
71 static const struct st_lsm6dsx_transfer_function st_lsm6dsx_transfer_fn = {
72 .read = st_lsm6dsx_spi_read,
73 .write = st_lsm6dsx_spi_write,
76 static int st_lsm6dsx_spi_probe(struct spi_device *spi)
78 const struct spi_device_id *id = spi_get_device_id(spi);
80 return st_lsm6dsx_probe(&spi->dev, spi->irq,
81 (int)id->driver_data, id->name,
82 &st_lsm6dsx_transfer_fn);
85 static const struct of_device_id st_lsm6dsx_spi_of_match[] = {
87 .compatible = "st,lsm6ds3",
88 .data = (void *)ST_LSM6DS3_ID,
91 .compatible = "st,lsm6ds3h",
92 .data = (void *)ST_LSM6DS3H_ID,
95 .compatible = "st,lsm6dsl",
96 .data = (void *)ST_LSM6DSL_ID,
99 .compatible = "st,lsm6dsm",
100 .data = (void *)ST_LSM6DSM_ID,
104 MODULE_DEVICE_TABLE(of, st_lsm6dsx_spi_of_match);
106 static const struct spi_device_id st_lsm6dsx_spi_id_table[] = {
107 { ST_LSM6DS3_DEV_NAME, ST_LSM6DS3_ID },
108 { ST_LSM6DS3H_DEV_NAME, ST_LSM6DS3H_ID },
109 { ST_LSM6DSL_DEV_NAME, ST_LSM6DSL_ID },
110 { ST_LSM6DSM_DEV_NAME, ST_LSM6DSM_ID },
113 MODULE_DEVICE_TABLE(spi, st_lsm6dsx_spi_id_table);
115 static struct spi_driver st_lsm6dsx_driver = {
117 .name = "st_lsm6dsx_spi",
118 .pm = &st_lsm6dsx_pm_ops,
119 .of_match_table = of_match_ptr(st_lsm6dsx_spi_of_match),
121 .probe = st_lsm6dsx_spi_probe,
122 .id_table = st_lsm6dsx_spi_id_table,
124 module_spi_driver(st_lsm6dsx_driver);
126 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
127 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
128 MODULE_DESCRIPTION("STMicroelectronics st_lsm6dsx spi driver");
129 MODULE_LICENSE("GPL v2");