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