| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * MS5611 pressure and temperature sensor driver (SPI bus) |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com> |
|---|
| 5 | | - * |
|---|
| 6 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 7 | | - * it under the terms of the GNU General Public License version 2 as |
|---|
| 8 | | - * published by the Free Software Foundation. |
|---|
| 9 | 6 | * |
|---|
| 10 | 7 | */ |
|---|
| 11 | 8 | |
|---|
| 12 | 9 | #include <linux/delay.h> |
|---|
| 13 | 10 | #include <linux/module.h> |
|---|
| 14 | 11 | #include <linux/spi/spi.h> |
|---|
| 15 | | -#include <linux/of_device.h> |
|---|
| 12 | +#include <linux/mod_devicetable.h> |
|---|
| 13 | + |
|---|
| 14 | +#include <asm/unaligned.h> |
|---|
| 16 | 15 | |
|---|
| 17 | 16 | #include "ms5611.h" |
|---|
| 18 | 17 | |
|---|
| 19 | | -static int ms5611_spi_reset(struct device *dev) |
|---|
| 18 | +static int ms5611_spi_reset(struct ms5611_state *st) |
|---|
| 20 | 19 | { |
|---|
| 21 | 20 | u8 cmd = MS5611_RESET; |
|---|
| 22 | | - struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev)); |
|---|
| 23 | 21 | |
|---|
| 24 | 22 | return spi_write_then_read(st->client, &cmd, 1, NULL, 0); |
|---|
| 25 | 23 | } |
|---|
| 26 | 24 | |
|---|
| 27 | | -static int ms5611_spi_read_prom_word(struct device *dev, int index, u16 *word) |
|---|
| 25 | +static int ms5611_spi_read_prom_word(struct ms5611_state *st, int index, |
|---|
| 26 | + u16 *word) |
|---|
| 28 | 27 | { |
|---|
| 29 | 28 | int ret; |
|---|
| 30 | | - struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev)); |
|---|
| 31 | 29 | |
|---|
| 32 | 30 | ret = spi_w8r16be(st->client, MS5611_READ_PROM_WORD + (index << 1)); |
|---|
| 33 | 31 | if (ret < 0) |
|---|
| .. | .. |
|---|
| 38 | 36 | return 0; |
|---|
| 39 | 37 | } |
|---|
| 40 | 38 | |
|---|
| 41 | | -static int ms5611_spi_read_adc(struct device *dev, s32 *val) |
|---|
| 39 | +static int ms5611_spi_read_adc(struct ms5611_state *st, s32 *val) |
|---|
| 42 | 40 | { |
|---|
| 43 | 41 | int ret; |
|---|
| 44 | 42 | u8 buf[3] = { MS5611_READ_ADC }; |
|---|
| 45 | | - struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev)); |
|---|
| 46 | 43 | |
|---|
| 47 | 44 | ret = spi_write_then_read(st->client, buf, 1, buf, 3); |
|---|
| 48 | 45 | if (ret < 0) |
|---|
| 49 | 46 | return ret; |
|---|
| 50 | 47 | |
|---|
| 51 | | - *val = (buf[0] << 16) | (buf[1] << 8) | buf[2]; |
|---|
| 48 | + *val = get_unaligned_be24(&buf[0]); |
|---|
| 52 | 49 | |
|---|
| 53 | 50 | return 0; |
|---|
| 54 | 51 | } |
|---|
| 55 | 52 | |
|---|
| 56 | | -static int ms5611_spi_read_adc_temp_and_pressure(struct device *dev, |
|---|
| 53 | +static int ms5611_spi_read_adc_temp_and_pressure(struct ms5611_state *st, |
|---|
| 57 | 54 | s32 *temp, s32 *pressure) |
|---|
| 58 | 55 | { |
|---|
| 59 | 56 | int ret; |
|---|
| 60 | | - struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev)); |
|---|
| 61 | 57 | const struct ms5611_osr *osr = st->temp_osr; |
|---|
| 62 | 58 | |
|---|
| 63 | 59 | /* |
|---|
| .. | .. |
|---|
| 69 | 65 | return ret; |
|---|
| 70 | 66 | |
|---|
| 71 | 67 | usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL)); |
|---|
| 72 | | - ret = ms5611_spi_read_adc(dev, temp); |
|---|
| 68 | + ret = ms5611_spi_read_adc(st, temp); |
|---|
| 73 | 69 | if (ret < 0) |
|---|
| 74 | 70 | return ret; |
|---|
| 75 | 71 | |
|---|
| .. | .. |
|---|
| 79 | 75 | return ret; |
|---|
| 80 | 76 | |
|---|
| 81 | 77 | usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL)); |
|---|
| 82 | | - return ms5611_spi_read_adc(dev, pressure); |
|---|
| 78 | + return ms5611_spi_read_adc(st, pressure); |
|---|
| 83 | 79 | } |
|---|
| 84 | 80 | |
|---|
| 85 | 81 | static int ms5611_spi_probe(struct spi_device *spi) |
|---|
| .. | .. |
|---|
| 95 | 91 | spi_set_drvdata(spi, indio_dev); |
|---|
| 96 | 92 | |
|---|
| 97 | 93 | spi->mode = SPI_MODE_0; |
|---|
| 98 | | - spi->max_speed_hz = 20000000; |
|---|
| 94 | + spi->max_speed_hz = min(spi->max_speed_hz, 20000000U); |
|---|
| 99 | 95 | spi->bits_per_word = 8; |
|---|
| 100 | 96 | ret = spi_setup(spi); |
|---|
| 101 | 97 | if (ret < 0) |
|---|
| .. | .. |
|---|
| 116 | 112 | return ms5611_remove(spi_get_drvdata(spi)); |
|---|
| 117 | 113 | } |
|---|
| 118 | 114 | |
|---|
| 119 | | -#if defined(CONFIG_OF) |
|---|
| 120 | 115 | static const struct of_device_id ms5611_spi_matches[] = { |
|---|
| 121 | 116 | { .compatible = "meas,ms5611" }, |
|---|
| 122 | | - { .compatible = "ms5611" }, |
|---|
| 123 | 117 | { .compatible = "meas,ms5607" }, |
|---|
| 124 | | - { .compatible = "ms5607" }, |
|---|
| 125 | 118 | { } |
|---|
| 126 | 119 | }; |
|---|
| 127 | 120 | MODULE_DEVICE_TABLE(of, ms5611_spi_matches); |
|---|
| 128 | | -#endif |
|---|
| 129 | 121 | |
|---|
| 130 | 122 | static const struct spi_device_id ms5611_id[] = { |
|---|
| 131 | 123 | { "ms5611", MS5611 }, |
|---|
| .. | .. |
|---|
| 137 | 129 | static struct spi_driver ms5611_driver = { |
|---|
| 138 | 130 | .driver = { |
|---|
| 139 | 131 | .name = "ms5611", |
|---|
| 140 | | - .of_match_table = of_match_ptr(ms5611_spi_matches) |
|---|
| 132 | + .of_match_table = ms5611_spi_matches |
|---|
| 141 | 133 | }, |
|---|
| 142 | 134 | .id_table = ms5611_id, |
|---|
| 143 | 135 | .probe = ms5611_spi_probe, |
|---|