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

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of linux/drivers/iio/pressure/ms5611_spi.c