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