1 /*
2  * STMicroelectronics st_lsm6dsx spi driver
3  *
4  * Copyright 2016 STMicroelectronics Inc.
5  *
6  * Lorenzo Bianconi <lorenzo.bianconi@st.com>
7  * Denis Ciocca <denis.ciocca@st.com>
8  *
9  * Licensed under the GPL-2.
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/spi/spi.h>
15 #include <linux/slab.h>
16 #include <linux/of.h>
17 
18 #include "st_lsm6dsx.h"
19 
20 #define SENSORS_SPI_READ	BIT(7)
21 
22 static int st_lsm6dsx_spi_read(struct device *dev, u8 addr, int len,
23 			       u8 *data)
24 {
25 	struct spi_device *spi = to_spi_device(dev);
26 	struct st_lsm6dsx_hw *hw = spi_get_drvdata(spi);
27 	int err;
28 
29 	struct spi_transfer xfers[] = {
30 		{
31 			.tx_buf = hw->tb.tx_buf,
32 			.bits_per_word = 8,
33 			.len = 1,
34 		},
35 		{
36 			.rx_buf = hw->tb.rx_buf,
37 			.bits_per_word = 8,
38 			.len = len,
39 		}
40 	};
41 
42 	hw->tb.tx_buf[0] = addr | SENSORS_SPI_READ;
43 
44 	err = spi_sync_transfer(spi, xfers,  ARRAY_SIZE(xfers));
45 	if (err < 0)
46 		return err;
47 
48 	memcpy(data, hw->tb.rx_buf, len * sizeof(u8));
49 
50 	return len;
51 }
52 
53 static int st_lsm6dsx_spi_write(struct device *dev, u8 addr, int len,
54 				u8 *data)
55 {
56 	struct st_lsm6dsx_hw *hw;
57 	struct spi_device *spi;
58 
59 	if (len >= ST_LSM6DSX_TX_MAX_LENGTH)
60 		return -ENOMEM;
61 
62 	spi = to_spi_device(dev);
63 	hw = spi_get_drvdata(spi);
64 
65 	hw->tb.tx_buf[0] = addr;
66 	memcpy(&hw->tb.tx_buf[1], data, len);
67 
68 	return spi_write(spi, hw->tb.tx_buf, len + 1);
69 }
70 
71 static const struct st_lsm6dsx_transfer_function st_lsm6dsx_transfer_fn = {
72 	.read = st_lsm6dsx_spi_read,
73 	.write = st_lsm6dsx_spi_write,
74 };
75 
76 static int st_lsm6dsx_spi_probe(struct spi_device *spi)
77 {
78 	const struct spi_device_id *id = spi_get_device_id(spi);
79 
80 	return st_lsm6dsx_probe(&spi->dev, spi->irq,
81 				(int)id->driver_data,
82 				&st_lsm6dsx_transfer_fn);
83 }
84 
85 static const struct of_device_id st_lsm6dsx_spi_of_match[] = {
86 	{
87 		.compatible = "st,lsm6ds3",
88 		.data = (void *)ST_LSM6DS3_ID,
89 	},
90 	{
91 		.compatible = "st,lsm6dsm",
92 		.data = (void *)ST_LSM6DSM_ID,
93 	},
94 	{},
95 };
96 MODULE_DEVICE_TABLE(of, st_lsm6dsx_spi_of_match);
97 
98 static const struct spi_device_id st_lsm6dsx_spi_id_table[] = {
99 	{ ST_LSM6DS3_DEV_NAME, ST_LSM6DS3_ID },
100 	{ ST_LSM6DSM_DEV_NAME, ST_LSM6DSM_ID },
101 	{},
102 };
103 MODULE_DEVICE_TABLE(spi, st_lsm6dsx_spi_id_table);
104 
105 static struct spi_driver st_lsm6dsx_driver = {
106 	.driver = {
107 		.name = "st_lsm6dsx_spi",
108 		.of_match_table = of_match_ptr(st_lsm6dsx_spi_of_match),
109 	},
110 	.probe = st_lsm6dsx_spi_probe,
111 	.id_table = st_lsm6dsx_spi_id_table,
112 };
113 module_spi_driver(st_lsm6dsx_driver);
114 
115 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
116 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
117 MODULE_DESCRIPTION("STMicroelectronics st_lsm6dsx spi driver");
118 MODULE_LICENSE("GPL v2");
119