xref: /linux/drivers/iio/pressure/mprls0025pa_spi.c (revision 889600e21e3be388a6817c2a0dac0411df860751)
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/array_size.h>
12 #include <linux/device.h>
13 #include <linux/errno.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 
mpr_spi_xfer(struct mpr_data * data,const u8 cmd,const u8 pkt_len)21 static int mpr_spi_xfer(struct mpr_data *data, const u8 cmd, const u8 pkt_len)
22 {
23 	struct spi_device *spi = to_spi_device(data->dev);
24 	struct spi_transfer xfers[2] = { };
25 
26 	if (pkt_len > MPR_MEASUREMENT_RD_SIZE)
27 		return -EOVERFLOW;
28 
29 	data->tx_buf[0] = cmd;
30 
31 	/*
32 	 * Dummy transfer with no data, just cause a 2.5us+ delay between the CS assert
33 	 * and the first clock edge as per the datasheet tHDSS timing requirement.
34 	 */
35 	xfers[0].delay.value = 2500;
36 	xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS;
37 
38 	xfers[1].tx_buf = data->tx_buf;
39 	xfers[1].rx_buf = data->rx_buf;
40 	xfers[1].len = pkt_len;
41 
42 	return spi_sync_transfer(spi, xfers, ARRAY_SIZE(xfers));
43 }
44 
45 static const struct mpr_ops mpr_spi_ops = {
46 	.read = mpr_spi_xfer,
47 	.write = mpr_spi_xfer,
48 };
49 
mpr_spi_probe(struct spi_device * spi)50 static int mpr_spi_probe(struct spi_device *spi)
51 {
52 	return mpr_common_probe(&spi->dev, &mpr_spi_ops, spi->irq);
53 }
54 
55 static const struct of_device_id mpr_spi_match[] = {
56 	{ .compatible = "honeywell,mprls0025pa" },
57 	{ }
58 };
59 MODULE_DEVICE_TABLE(of, mpr_spi_match);
60 
61 static const struct spi_device_id mpr_spi_id[] = {
62 	{ .name = "mprls0025pa" },
63 	{ }
64 };
65 MODULE_DEVICE_TABLE(spi, mpr_spi_id);
66 
67 static struct spi_driver mpr_spi_driver = {
68 	.driver = {
69 		.name = "mprls0025pa",
70 		.of_match_table = mpr_spi_match,
71 	},
72 	.probe = mpr_spi_probe,
73 	.id_table = mpr_spi_id,
74 };
75 module_spi_driver(mpr_spi_driver);
76 
77 MODULE_AUTHOR("Petre Rodan <petre.rodan@subdimension.ro>");
78 MODULE_DESCRIPTION("Honeywell MPR pressure sensor spi driver");
79 MODULE_LICENSE("GPL");
80 MODULE_IMPORT_NS("IIO_HONEYWELL_MPRLS0025PA");
81