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