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