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