1 // SPDX-License-Identifier: GPL-2.0-only
3 * Freescale MPL115A1 pressure/temperature sensor
5 * Copyright (c) 2016 Akinobu Mita <akinobu.mita@gmail.com>
7 * Datasheet: http://www.nxp.com/files/sensors/doc/data_sheet/MPL115A1.pdf
10 #include <linux/module.h>
11 #include <linux/spi/spi.h>
15 #define MPL115_SPI_WRITE(address) ((address) << 1)
16 #define MPL115_SPI_READ(address) (0x80 | (address) << 1)
18 struct mpl115_spi_buf
{
23 static int mpl115_spi_init(struct device
*dev
)
25 struct spi_device
*spi
= to_spi_device(dev
);
26 struct mpl115_spi_buf
*buf
;
28 buf
= devm_kzalloc(dev
, sizeof(*buf
), GFP_KERNEL
);
32 spi_set_drvdata(spi
, buf
);
37 static int mpl115_spi_read(struct device
*dev
, u8 address
)
39 struct spi_device
*spi
= to_spi_device(dev
);
40 struct mpl115_spi_buf
*buf
= spi_get_drvdata(spi
);
41 struct spi_transfer xfer
= {
48 buf
->tx
[0] = MPL115_SPI_READ(address
);
49 buf
->tx
[2] = MPL115_SPI_READ(address
+ 1);
51 ret
= spi_sync_transfer(spi
, &xfer
, 1);
55 return (buf
->rx
[1] << 8) | buf
->rx
[3];
58 static int mpl115_spi_write(struct device
*dev
, u8 address
, u8 value
)
60 struct spi_device
*spi
= to_spi_device(dev
);
61 struct mpl115_spi_buf
*buf
= spi_get_drvdata(spi
);
62 struct spi_transfer xfer
= {
67 buf
->tx
[0] = MPL115_SPI_WRITE(address
);
70 return spi_sync_transfer(spi
, &xfer
, 1);
73 static const struct mpl115_ops mpl115_spi_ops
= {
74 .init
= mpl115_spi_init
,
75 .read
= mpl115_spi_read
,
76 .write
= mpl115_spi_write
,
79 static int mpl115_spi_probe(struct spi_device
*spi
)
81 const struct spi_device_id
*id
= spi_get_device_id(spi
);
83 return mpl115_probe(&spi
->dev
, id
->name
, &mpl115_spi_ops
);
86 static const struct spi_device_id mpl115_spi_ids
[] = {
90 MODULE_DEVICE_TABLE(spi
, mpl115_spi_ids
);
92 static struct spi_driver mpl115_spi_driver
= {
95 .pm
= pm_ptr(&mpl115_dev_pm_ops
),
97 .probe
= mpl115_spi_probe
,
98 .id_table
= mpl115_spi_ids
,
100 module_spi_driver(mpl115_spi_driver
);
102 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
103 MODULE_DESCRIPTION("Freescale MPL115A1 pressure/temperature driver");
104 MODULE_LICENSE("GPL");
105 MODULE_IMPORT_NS("IIO_MPL115");