1 // SPDX-License-Identifier: GPL-2.0
3 * MS5611 pressure and temperature sensor driver (SPI bus)
5 * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
9 #include <linux/delay.h>
10 #include <linux/module.h>
11 #include <linux/spi/spi.h>
12 #include <linux/of_device.h>
16 static int ms5611_spi_reset(struct device
*dev
)
18 u8 cmd
= MS5611_RESET
;
19 struct ms5611_state
*st
= iio_priv(dev_to_iio_dev(dev
));
21 return spi_write_then_read(st
->client
, &cmd
, 1, NULL
, 0);
24 static int ms5611_spi_read_prom_word(struct device
*dev
, int index
, u16
*word
)
27 struct ms5611_state
*st
= iio_priv(dev_to_iio_dev(dev
));
29 ret
= spi_w8r16be(st
->client
, MS5611_READ_PROM_WORD
+ (index
<< 1));
38 static int ms5611_spi_read_adc(struct device
*dev
, s32
*val
)
41 u8 buf
[3] = { MS5611_READ_ADC
};
42 struct ms5611_state
*st
= iio_priv(dev_to_iio_dev(dev
));
44 ret
= spi_write_then_read(st
->client
, buf
, 1, buf
, 3);
48 *val
= (buf
[0] << 16) | (buf
[1] << 8) | buf
[2];
53 static int ms5611_spi_read_adc_temp_and_pressure(struct device
*dev
,
54 s32
*temp
, s32
*pressure
)
57 struct ms5611_state
*st
= iio_priv(dev_to_iio_dev(dev
));
58 const struct ms5611_osr
*osr
= st
->temp_osr
;
61 * Warning: &osr->cmd MUST be aligned on a word boundary since used as
62 * 2nd argument (void*) of spi_write_then_read.
64 ret
= spi_write_then_read(st
->client
, &osr
->cmd
, 1, NULL
, 0);
68 usleep_range(osr
->conv_usec
, osr
->conv_usec
+ (osr
->conv_usec
/ 10UL));
69 ret
= ms5611_spi_read_adc(dev
, temp
);
73 osr
= st
->pressure_osr
;
74 ret
= spi_write_then_read(st
->client
, &osr
->cmd
, 1, NULL
, 0);
78 usleep_range(osr
->conv_usec
, osr
->conv_usec
+ (osr
->conv_usec
/ 10UL));
79 return ms5611_spi_read_adc(dev
, pressure
);
82 static int ms5611_spi_probe(struct spi_device
*spi
)
85 struct ms5611_state
*st
;
86 struct iio_dev
*indio_dev
;
88 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
92 spi_set_drvdata(spi
, indio_dev
);
94 spi
->mode
= SPI_MODE_0
;
95 spi
->max_speed_hz
= 20000000;
96 spi
->bits_per_word
= 8;
101 st
= iio_priv(indio_dev
);
102 st
->reset
= ms5611_spi_reset
;
103 st
->read_prom_word
= ms5611_spi_read_prom_word
;
104 st
->read_adc_temp_and_pressure
= ms5611_spi_read_adc_temp_and_pressure
;
107 return ms5611_probe(indio_dev
, &spi
->dev
, spi_get_device_id(spi
)->name
,
108 spi_get_device_id(spi
)->driver_data
);
111 static int ms5611_spi_remove(struct spi_device
*spi
)
113 return ms5611_remove(spi_get_drvdata(spi
));
116 #if defined(CONFIG_OF)
117 static const struct of_device_id ms5611_spi_matches
[] = {
118 { .compatible
= "meas,ms5611" },
119 { .compatible
= "meas,ms5607" },
122 MODULE_DEVICE_TABLE(of
, ms5611_spi_matches
);
125 static const struct spi_device_id ms5611_id
[] = {
126 { "ms5611", MS5611
},
127 { "ms5607", MS5607
},
130 MODULE_DEVICE_TABLE(spi
, ms5611_id
);
132 static struct spi_driver ms5611_driver
= {
135 .of_match_table
= of_match_ptr(ms5611_spi_matches
)
137 .id_table
= ms5611_id
,
138 .probe
= ms5611_spi_probe
,
139 .remove
= ms5611_spi_remove
,
141 module_spi_driver(ms5611_driver
);
143 MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
144 MODULE_DESCRIPTION("MS5611 spi driver");
145 MODULE_LICENSE("GPL v2");