1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/module.h>
14#include <linux/spi/spi.h>
15
16#include "mpl115.h"
17
18#define MPL115_SPI_WRITE(address) ((address) << 1)
19#define MPL115_SPI_READ(address) (0x80 | (address) << 1)
20
21struct mpl115_spi_buf {
22 u8 tx[4];
23 u8 rx[4];
24};
25
26static int mpl115_spi_init(struct device *dev)
27{
28 struct spi_device *spi = to_spi_device(dev);
29 struct mpl115_spi_buf *buf;
30
31 buf = devm_kzalloc(dev, sizeof(*buf), GFP_KERNEL);
32 if (!buf)
33 return -ENOMEM;
34
35 spi_set_drvdata(spi, buf);
36
37 return 0;
38}
39
40static int mpl115_spi_read(struct device *dev, u8 address)
41{
42 struct spi_device *spi = to_spi_device(dev);
43 struct mpl115_spi_buf *buf = spi_get_drvdata(spi);
44 struct spi_transfer xfer = {
45 .tx_buf = buf->tx,
46 .rx_buf = buf->rx,
47 .len = 4,
48 };
49 int ret;
50
51 buf->tx[0] = MPL115_SPI_READ(address);
52 buf->tx[2] = MPL115_SPI_READ(address + 1);
53
54 ret = spi_sync_transfer(spi, &xfer, 1);
55 if (ret)
56 return ret;
57
58 return (buf->rx[1] << 8) | buf->rx[3];
59}
60
61static int mpl115_spi_write(struct device *dev, u8 address, u8 value)
62{
63 struct spi_device *spi = to_spi_device(dev);
64 struct mpl115_spi_buf *buf = spi_get_drvdata(spi);
65 struct spi_transfer xfer = {
66 .tx_buf = buf->tx,
67 .len = 2,
68 };
69
70 buf->tx[0] = MPL115_SPI_WRITE(address);
71 buf->tx[1] = value;
72
73 return spi_sync_transfer(spi, &xfer, 1);
74}
75
76static const struct mpl115_ops mpl115_spi_ops = {
77 .init = mpl115_spi_init,
78 .read = mpl115_spi_read,
79 .write = mpl115_spi_write,
80};
81
82static int mpl115_spi_probe(struct spi_device *spi)
83{
84 const struct spi_device_id *id = spi_get_device_id(spi);
85
86 return mpl115_probe(&spi->dev, id->name, &mpl115_spi_ops);
87}
88
89static const struct spi_device_id mpl115_spi_ids[] = {
90 { "mpl115", 0 },
91 {}
92};
93MODULE_DEVICE_TABLE(spi, mpl115_spi_ids);
94
95static struct spi_driver mpl115_spi_driver = {
96 .driver = {
97 .name = "mpl115",
98 },
99 .probe = mpl115_spi_probe,
100 .id_table = mpl115_spi_ids,
101};
102module_spi_driver(mpl115_spi_driver);
103
104MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
105MODULE_DESCRIPTION("Freescale MPL115A1 pressure/temperature driver");
106MODULE_LICENSE("GPL");
107