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>
18 #define MPL115_SPI_WRITE(address) ((address) << 1)
19 #define MPL115_SPI_READ(address) (0x80 | (address) << 1)
21 struct mpl115_spi_buf
{
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
);
35 spi_set_drvdata(spi
, buf
);
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
= {
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);
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
= {
70 buf
->tx
[0] = MPL115_SPI_WRITE(address
);
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
[] = {
93 MODULE_DEVICE_TABLE(spi
, mpl115_spi_ids
);
95 static struct spi_driver mpl115_spi_driver
= {
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");