Linux 4.16.11
[linux/fpc-iii.git] / drivers / iio / pressure / mpl115_spi.c
blob9ebf55f5b3aa29b5d3ac5a892caa1a6a2e12981b
1 /*
2 * Freescale MPL115A1 pressure/temperature sensor
4 * Copyright (c) 2016 Akinobu Mita <akinobu.mita@gmail.com>
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * Datasheet: http://www.nxp.com/files/sensors/doc/data_sheet/MPL115A1.pdf
13 #include <linux/module.h>
14 #include <linux/spi/spi.h>
16 #include "mpl115.h"
18 #define MPL115_SPI_WRITE(address) ((address) << 1)
19 #define MPL115_SPI_READ(address) (0x80 | (address) << 1)
21 struct mpl115_spi_buf {
22 u8 tx[4];
23 u8 rx[4];
26 static int mpl115_spi_init(struct device *dev)
28 struct spi_device *spi = to_spi_device(dev);
29 struct mpl115_spi_buf *buf;
31 buf = devm_kzalloc(dev, sizeof(*buf), GFP_KERNEL);
32 if (!buf)
33 return -ENOMEM;
35 spi_set_drvdata(spi, buf);
37 return 0;
40 static int mpl115_spi_read(struct device *dev, u8 address)
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,
49 int ret;
51 buf->tx[0] = MPL115_SPI_READ(address);
52 buf->tx[2] = MPL115_SPI_READ(address + 1);
54 ret = spi_sync_transfer(spi, &xfer, 1);
55 if (ret)
56 return ret;
58 return (buf->rx[1] << 8) | buf->rx[3];
61 static int mpl115_spi_write(struct device *dev, u8 address, u8 value)
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,
70 buf->tx[0] = MPL115_SPI_WRITE(address);
71 buf->tx[1] = value;
73 return spi_sync_transfer(spi, &xfer, 1);
76 static const struct mpl115_ops mpl115_spi_ops = {
77 .init = mpl115_spi_init,
78 .read = mpl115_spi_read,
79 .write = mpl115_spi_write,
82 static int mpl115_spi_probe(struct spi_device *spi)
84 const struct spi_device_id *id = spi_get_device_id(spi);
86 return mpl115_probe(&spi->dev, id->name, &mpl115_spi_ops);
89 static const struct spi_device_id mpl115_spi_ids[] = {
90 { "mpl115", 0 },
93 MODULE_DEVICE_TABLE(spi, mpl115_spi_ids);
95 static struct spi_driver mpl115_spi_driver = {
96 .driver = {
97 .name = "mpl115",
99 .probe = mpl115_spi_probe,
100 .id_table = mpl115_spi_ids,
102 module_spi_driver(mpl115_spi_driver);
104 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
105 MODULE_DESCRIPTION("Freescale MPL115A1 pressure/temperature driver");
106 MODULE_LICENSE("GPL");