xref: /OK3568_Linux_fs/kernel/drivers/iio/imu/st_lsm6dsr/st_lsm6dsr_spi.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics st_lsm6dsr spi driver
4  *
5  * Copyright 2020 STMicroelectronics Inc.
6  *
7  * Lorenzo Bianconi <lorenzo.bianconi@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_lsm6dsr.h"
19 
20 #define SENSORS_SPI_READ	BIT(7)
21 
st_lsm6dsr_spi_read(struct device * dev,u8 addr,int len,u8 * data)22 static int st_lsm6dsr_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_lsm6dsr_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 
st_lsm6dsr_spi_write(struct device * dev,u8 addr,int len,const u8 * data)53 static int st_lsm6dsr_spi_write(struct device *dev, u8 addr, int len,
54 				const u8 *data)
55 {
56 	struct st_lsm6dsr_hw *hw;
57 	struct spi_device *spi;
58 
59 	if (len >= ST_LSM6DSR_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_lsm6dsr_transfer_function st_lsm6dsr_transfer_fn = {
72 	.read = st_lsm6dsr_spi_read,
73 	.write = st_lsm6dsr_spi_write,
74 };
75 
st_lsm6dsr_spi_probe(struct spi_device * spi)76 static int st_lsm6dsr_spi_probe(struct spi_device *spi)
77 {
78 	return st_lsm6dsr_probe(&spi->dev, spi->irq,
79 				&st_lsm6dsr_transfer_fn);
80 }
81 
st_lsm6dsr_spi_remove(struct spi_device * spi)82 static int st_lsm6dsr_spi_remove(struct spi_device *spi)
83 {
84 	return st_lsm6dsr_remove(&spi->dev);
85 }
86 
87 static const struct of_device_id st_lsm6dsr_spi_of_match[] = {
88 	{
89 		.compatible = "st,lsm6dsr",
90 	},
91 	{},
92 };
93 MODULE_DEVICE_TABLE(of, st_lsm6dsr_spi_of_match);
94 
95 static const struct spi_device_id st_lsm6dsr_spi_id_table[] = {
96 	{ ST_LSM6DSR_DEV_NAME },
97 	{},
98 };
99 MODULE_DEVICE_TABLE(spi, st_lsm6dsr_spi_id_table);
100 
101 static struct spi_driver st_lsm6dsr_driver = {
102 	.driver = {
103 		.name = "st_lsm6dsr_spi",
104 		.pm = &st_lsm6dsr_pm_ops,
105 		.of_match_table = of_match_ptr(st_lsm6dsr_spi_of_match),
106 	},
107 	.probe = st_lsm6dsr_spi_probe,
108 	.remove = st_lsm6dsr_spi_remove,
109 	.id_table = st_lsm6dsr_spi_id_table,
110 };
111 module_spi_driver(st_lsm6dsr_driver);
112 
113 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
114 MODULE_DESCRIPTION("STMicroelectronics st_lsm6dsr spi driver");
115 MODULE_LICENSE("GPL");
116