1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * MS5611 pressure and temperature sensor driver (SPI bus)
4  *
5  * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
6  *
7  */
8 
9 #include <linux/delay.h>
10 #include <linux/module.h>
11 #include <linux/spi/spi.h>
12 #include <linux/mod_devicetable.h>
13 
14 #include <asm/unaligned.h>
15 
16 #include "ms5611.h"
17 
ms5611_spi_reset(struct device * dev)18 static int ms5611_spi_reset(struct device *dev)
19 {
20 	u8 cmd = MS5611_RESET;
21 	struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
22 
23 	return spi_write_then_read(st->client, &cmd, 1, NULL, 0);
24 }
25 
ms5611_spi_read_prom_word(struct device * dev,int index,u16 * word)26 static int ms5611_spi_read_prom_word(struct device *dev, int index, u16 *word)
27 {
28 	int ret;
29 	struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
30 
31 	ret = spi_w8r16be(st->client, MS5611_READ_PROM_WORD + (index << 1));
32 	if (ret < 0)
33 		return ret;
34 
35 	*word = ret;
36 
37 	return 0;
38 }
39 
ms5611_spi_read_adc(struct device * dev,s32 * val)40 static int ms5611_spi_read_adc(struct device *dev, s32 *val)
41 {
42 	int ret;
43 	u8 buf[3] = { MS5611_READ_ADC };
44 	struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
45 
46 	ret = spi_write_then_read(st->client, buf, 1, buf, 3);
47 	if (ret < 0)
48 		return ret;
49 
50 	*val = get_unaligned_be24(&buf[0]);
51 
52 	return 0;
53 }
54 
ms5611_spi_read_adc_temp_and_pressure(struct device * dev,s32 * temp,s32 * pressure)55 static int ms5611_spi_read_adc_temp_and_pressure(struct device *dev,
56 						 s32 *temp, s32 *pressure)
57 {
58 	int ret;
59 	struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
60 	const struct ms5611_osr *osr = st->temp_osr;
61 
62 	/*
63 	 * Warning: &osr->cmd MUST be aligned on a word boundary since used as
64 	 * 2nd argument (void*) of spi_write_then_read.
65 	 */
66 	ret = spi_write_then_read(st->client, &osr->cmd, 1, NULL, 0);
67 	if (ret < 0)
68 		return ret;
69 
70 	usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL));
71 	ret = ms5611_spi_read_adc(dev, temp);
72 	if (ret < 0)
73 		return ret;
74 
75 	osr = st->pressure_osr;
76 	ret = spi_write_then_read(st->client, &osr->cmd, 1, NULL, 0);
77 	if (ret < 0)
78 		return ret;
79 
80 	usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL));
81 	return ms5611_spi_read_adc(dev, pressure);
82 }
83 
ms5611_spi_probe(struct spi_device * spi)84 static int ms5611_spi_probe(struct spi_device *spi)
85 {
86 	int ret;
87 	struct ms5611_state *st;
88 	struct iio_dev *indio_dev;
89 
90 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
91 	if (!indio_dev)
92 		return -ENOMEM;
93 
94 	spi_set_drvdata(spi, indio_dev);
95 
96 	spi->mode = SPI_MODE_0;
97 	spi->max_speed_hz = 20000000;
98 	spi->bits_per_word = 8;
99 	ret = spi_setup(spi);
100 	if (ret < 0)
101 		return ret;
102 
103 	st = iio_priv(indio_dev);
104 	st->reset = ms5611_spi_reset;
105 	st->read_prom_word = ms5611_spi_read_prom_word;
106 	st->read_adc_temp_and_pressure = ms5611_spi_read_adc_temp_and_pressure;
107 	st->client = spi;
108 
109 	return ms5611_probe(indio_dev, &spi->dev, spi_get_device_id(spi)->name,
110 			    spi_get_device_id(spi)->driver_data);
111 }
112 
ms5611_spi_remove(struct spi_device * spi)113 static int ms5611_spi_remove(struct spi_device *spi)
114 {
115 	ms5611_remove(spi_get_drvdata(spi));
116 
117 	return 0;
118 }
119 
120 static const struct of_device_id ms5611_spi_matches[] = {
121 	{ .compatible = "meas,ms5611" },
122 	{ .compatible = "meas,ms5607" },
123 	{ }
124 };
125 MODULE_DEVICE_TABLE(of, ms5611_spi_matches);
126 
127 static const struct spi_device_id ms5611_id[] = {
128 	{ "ms5611", MS5611 },
129 	{ "ms5607", MS5607 },
130 	{ }
131 };
132 MODULE_DEVICE_TABLE(spi, ms5611_id);
133 
134 static struct spi_driver ms5611_driver = {
135 	.driver = {
136 		.name = "ms5611",
137 		.of_match_table = ms5611_spi_matches
138 	},
139 	.id_table = ms5611_id,
140 	.probe = ms5611_spi_probe,
141 	.remove = ms5611_spi_remove,
142 };
143 module_spi_driver(ms5611_driver);
144 
145 MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
146 MODULE_DESCRIPTION("MS5611 spi driver");
147 MODULE_LICENSE("GPL v2");
148