xref: /linux/drivers/iio/pressure/mprls0025pa_spi.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
1*a0858f0cSPetre Rodan // SPDX-License-Identifier: GPL-2.0-only
2*a0858f0cSPetre Rodan /*
3*a0858f0cSPetre Rodan  * MPRLS0025PA - Honeywell MicroPressure MPR series SPI sensor driver
4*a0858f0cSPetre Rodan  *
5*a0858f0cSPetre Rodan  * Copyright (c) 2024 Petre Rodan <petre.rodan@subdimension.ro>
6*a0858f0cSPetre Rodan  *
7*a0858f0cSPetre Rodan  * Data sheet:
8*a0858f0cSPetre Rodan  *  https://prod-edam.honeywell.com/content/dam/honeywell-edam/sps/siot/en-us/products/sensors/pressure-sensors/board-mount-pressure-sensors/micropressure-mpr-series/documents/sps-siot-mpr-series-datasheet-32332628-ciid-172626.pdf
9*a0858f0cSPetre Rodan  */
10*a0858f0cSPetre Rodan 
11*a0858f0cSPetre Rodan #include <linux/device.h>
12*a0858f0cSPetre Rodan #include <linux/errno.h>
13*a0858f0cSPetre Rodan #include <linux/mod_devicetable.h>
14*a0858f0cSPetre Rodan #include <linux/module.h>
15*a0858f0cSPetre Rodan #include <linux/spi/spi.h>
16*a0858f0cSPetre Rodan #include <linux/stddef.h>
17*a0858f0cSPetre Rodan #include <linux/types.h>
18*a0858f0cSPetre Rodan 
19*a0858f0cSPetre Rodan #include "mprls0025pa.h"
20*a0858f0cSPetre Rodan 
21*a0858f0cSPetre Rodan struct mpr_spi_buf {
22*a0858f0cSPetre Rodan 	u8 tx[MPR_MEASUREMENT_RD_SIZE] __aligned(IIO_DMA_MINALIGN);
23*a0858f0cSPetre Rodan };
24*a0858f0cSPetre Rodan 
mpr_spi_init(struct device * dev)25*a0858f0cSPetre Rodan static int mpr_spi_init(struct device *dev)
26*a0858f0cSPetre Rodan {
27*a0858f0cSPetre Rodan 	struct spi_device *spi = to_spi_device(dev);
28*a0858f0cSPetre Rodan 	struct mpr_spi_buf *buf;
29*a0858f0cSPetre Rodan 
30*a0858f0cSPetre Rodan 	buf = devm_kzalloc(dev, sizeof(*buf), GFP_KERNEL);
31*a0858f0cSPetre Rodan 	if (!buf)
32*a0858f0cSPetre Rodan 		return -ENOMEM;
33*a0858f0cSPetre Rodan 
34*a0858f0cSPetre Rodan 	spi_set_drvdata(spi, buf);
35*a0858f0cSPetre Rodan 
36*a0858f0cSPetre Rodan 	return 0;
37*a0858f0cSPetre Rodan }
38*a0858f0cSPetre Rodan 
mpr_spi_xfer(struct mpr_data * data,const u8 cmd,const u8 pkt_len)39*a0858f0cSPetre Rodan static int mpr_spi_xfer(struct mpr_data *data, const u8 cmd, const u8 pkt_len)
40*a0858f0cSPetre Rodan {
41*a0858f0cSPetre Rodan 	struct spi_device *spi = to_spi_device(data->dev);
42*a0858f0cSPetre Rodan 	struct mpr_spi_buf *buf = spi_get_drvdata(spi);
43*a0858f0cSPetre Rodan 	struct spi_transfer xfer;
44*a0858f0cSPetre Rodan 
45*a0858f0cSPetre Rodan 	if (pkt_len > MPR_MEASUREMENT_RD_SIZE)
46*a0858f0cSPetre Rodan 		return -EOVERFLOW;
47*a0858f0cSPetre Rodan 
48*a0858f0cSPetre Rodan 	buf->tx[0] = cmd;
49*a0858f0cSPetre Rodan 	xfer.tx_buf = buf->tx;
50*a0858f0cSPetre Rodan 	xfer.rx_buf = data->buffer;
51*a0858f0cSPetre Rodan 	xfer.len = pkt_len;
52*a0858f0cSPetre Rodan 
53*a0858f0cSPetre Rodan 	return spi_sync_transfer(spi, &xfer, 1);
54*a0858f0cSPetre Rodan }
55*a0858f0cSPetre Rodan 
56*a0858f0cSPetre Rodan static const struct mpr_ops mpr_spi_ops = {
57*a0858f0cSPetre Rodan 	.init = mpr_spi_init,
58*a0858f0cSPetre Rodan 	.read = mpr_spi_xfer,
59*a0858f0cSPetre Rodan 	.write = mpr_spi_xfer,
60*a0858f0cSPetre Rodan };
61*a0858f0cSPetre Rodan 
mpr_spi_probe(struct spi_device * spi)62*a0858f0cSPetre Rodan static int mpr_spi_probe(struct spi_device *spi)
63*a0858f0cSPetre Rodan {
64*a0858f0cSPetre Rodan 	return mpr_common_probe(&spi->dev, &mpr_spi_ops, spi->irq);
65*a0858f0cSPetre Rodan }
66*a0858f0cSPetre Rodan 
67*a0858f0cSPetre Rodan static const struct of_device_id mpr_spi_match[] = {
68*a0858f0cSPetre Rodan 	{ .compatible = "honeywell,mprls0025pa" },
69*a0858f0cSPetre Rodan 	{}
70*a0858f0cSPetre Rodan };
71*a0858f0cSPetre Rodan MODULE_DEVICE_TABLE(of, mpr_spi_match);
72*a0858f0cSPetre Rodan 
73*a0858f0cSPetre Rodan static const struct spi_device_id mpr_spi_id[] = {
74*a0858f0cSPetre Rodan 	{ "mprls0025pa" },
75*a0858f0cSPetre Rodan 	{}
76*a0858f0cSPetre Rodan };
77*a0858f0cSPetre Rodan MODULE_DEVICE_TABLE(spi, mpr_spi_id);
78*a0858f0cSPetre Rodan 
79*a0858f0cSPetre Rodan static struct spi_driver mpr_spi_driver = {
80*a0858f0cSPetre Rodan 	.driver = {
81*a0858f0cSPetre Rodan 		.name = "mprls0025pa",
82*a0858f0cSPetre Rodan 		.of_match_table = mpr_spi_match,
83*a0858f0cSPetre Rodan 	},
84*a0858f0cSPetre Rodan 	.probe = mpr_spi_probe,
85*a0858f0cSPetre Rodan 	.id_table = mpr_spi_id,
86*a0858f0cSPetre Rodan };
87*a0858f0cSPetre Rodan module_spi_driver(mpr_spi_driver);
88*a0858f0cSPetre Rodan 
89*a0858f0cSPetre Rodan MODULE_AUTHOR("Petre Rodan <petre.rodan@subdimension.ro>");
90*a0858f0cSPetre Rodan MODULE_DESCRIPTION("Honeywell MPR pressure sensor spi driver");
91*a0858f0cSPetre Rodan MODULE_LICENSE("GPL");
92*a0858f0cSPetre Rodan MODULE_IMPORT_NS(IIO_HONEYWELL_MPRLS0025PA);
93