Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / iio / pressure / ms5611_spi.c
blob87181963a3e3db36c8ecdceb1a9dc2742eacb297
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * MS5611 pressure and temperature sensor driver (SPI bus)
5 * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
7 */
9 #include <linux/delay.h>
10 #include <linux/module.h>
11 #include <linux/spi/spi.h>
12 #include <linux/mod_devicetable.h>
14 #include <linux/unaligned.h>
16 #include "ms5611.h"
18 static int ms5611_spi_reset(struct ms5611_state *st)
20 u8 cmd = MS5611_RESET;
22 return spi_write_then_read(st->client, &cmd, 1, NULL, 0);
25 static int ms5611_spi_read_prom_word(struct ms5611_state *st, int index,
26 u16 *word)
28 int ret;
30 ret = spi_w8r16be(st->client, MS5611_READ_PROM_WORD + (index << 1));
31 if (ret < 0)
32 return ret;
34 *word = ret;
36 return 0;
39 static int ms5611_spi_read_adc(struct ms5611_state *st, s32 *val)
41 int ret;
42 u8 buf[3] = { MS5611_READ_ADC };
44 ret = spi_write_then_read(st->client, buf, 1, buf, 3);
45 if (ret < 0)
46 return ret;
48 *val = get_unaligned_be24(&buf[0]);
50 return 0;
53 static int ms5611_spi_read_adc_temp_and_pressure(struct ms5611_state *st,
54 s32 *temp, s32 *pressure)
56 int ret;
57 const struct ms5611_osr *osr = st->temp_osr;
60 * Warning: &osr->cmd MUST be aligned on a word boundary since used as
61 * 2nd argument (void*) of spi_write_then_read.
63 ret = spi_write_then_read(st->client, &osr->cmd, 1, NULL, 0);
64 if (ret < 0)
65 return ret;
67 usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL));
68 ret = ms5611_spi_read_adc(st, temp);
69 if (ret < 0)
70 return ret;
72 osr = st->pressure_osr;
73 ret = spi_write_then_read(st->client, &osr->cmd, 1, NULL, 0);
74 if (ret < 0)
75 return ret;
77 usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL));
78 return ms5611_spi_read_adc(st, pressure);
81 static int ms5611_spi_probe(struct spi_device *spi)
83 int ret;
84 struct ms5611_state *st;
85 struct iio_dev *indio_dev;
87 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
88 if (!indio_dev)
89 return -ENOMEM;
91 spi_set_drvdata(spi, indio_dev);
93 spi->mode = SPI_MODE_0;
94 spi->max_speed_hz = min(spi->max_speed_hz, 20000000U);
95 spi->bits_per_word = 8;
96 ret = spi_setup(spi);
97 if (ret < 0)
98 return ret;
100 st = iio_priv(indio_dev);
101 st->reset = ms5611_spi_reset;
102 st->read_prom_word = ms5611_spi_read_prom_word;
103 st->read_adc_temp_and_pressure = ms5611_spi_read_adc_temp_and_pressure;
104 st->client = spi;
106 return ms5611_probe(indio_dev, &spi->dev, spi_get_device_id(spi)->name,
107 spi_get_device_id(spi)->driver_data);
110 static const struct of_device_id ms5611_spi_matches[] = {
111 { .compatible = "meas,ms5611" },
112 { .compatible = "meas,ms5607" },
115 MODULE_DEVICE_TABLE(of, ms5611_spi_matches);
117 static const struct spi_device_id ms5611_id[] = {
118 { "ms5611", MS5611 },
119 { "ms5607", MS5607 },
122 MODULE_DEVICE_TABLE(spi, ms5611_id);
124 static struct spi_driver ms5611_driver = {
125 .driver = {
126 .name = "ms5611",
127 .of_match_table = ms5611_spi_matches
129 .id_table = ms5611_id,
130 .probe = ms5611_spi_probe,
132 module_spi_driver(ms5611_driver);
134 MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
135 MODULE_DESCRIPTION("MS5611 spi driver");
136 MODULE_LICENSE("GPL v2");
137 MODULE_IMPORT_NS(IIO_MS5611);