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> 15 #include <linux/of_device.h> 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 { 59 u8 cmd; 60 int ret; 61 struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev)); 62 63 cmd = MS5611_START_TEMP_CONV; 64 ret = spi_write_then_read(st->client, &cmd, 1, NULL, 0); 65 if (ret < 0) 66 return ret; 67 68 usleep_range(MS5611_CONV_TIME_MIN, MS5611_CONV_TIME_MAX); 69 70 ret = ms5611_spi_read_adc(dev, temp); 71 if (ret < 0) 72 return ret; 73 74 cmd = MS5611_START_PRESSURE_CONV; 75 ret = spi_write_then_read(st->client, &cmd, 1, NULL, 0); 76 if (ret < 0) 77 return ret; 78 79 usleep_range(MS5611_CONV_TIME_MIN, MS5611_CONV_TIME_MAX); 80 81 return ms5611_spi_read_adc(dev, pressure); 82 } 83 84 static int ms5611_spi_probe(struct spi_device *spi) 85 { 86 int ret; 87 struct ms5611_state *st; 88 struct iio_dev *indio_dev; 89 90 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 91 if (!indio_dev) 92 return -ENOMEM; 93 94 spi_set_drvdata(spi, indio_dev); 95 96 spi->mode = SPI_MODE_0; 97 spi->max_speed_hz = 20000000; 98 spi->bits_per_word = 8; 99 ret = spi_setup(spi); 100 if (ret < 0) 101 return ret; 102 103 st = iio_priv(indio_dev); 104 st->reset = ms5611_spi_reset; 105 st->read_prom_word = ms5611_spi_read_prom_word; 106 st->read_adc_temp_and_pressure = ms5611_spi_read_adc_temp_and_pressure; 107 st->client = spi; 108 109 return ms5611_probe(indio_dev, &spi->dev, spi_get_device_id(spi)->name, 110 spi_get_device_id(spi)->driver_data); 111 } 112 113 static int ms5611_spi_remove(struct spi_device *spi) 114 { 115 return ms5611_remove(spi_get_drvdata(spi)); 116 } 117 118 #if defined(CONFIG_OF) 119 static const struct of_device_id ms5611_spi_matches[] = { 120 { .compatible = "meas,ms5611" }, 121 { .compatible = "ms5611" }, 122 { .compatible = "meas,ms5607" }, 123 { .compatible = "ms5607" }, 124 { } 125 }; 126 MODULE_DEVICE_TABLE(of, ms5611_spi_matches); 127 #endif 128 129 static const struct spi_device_id ms5611_id[] = { 130 { "ms5611", MS5611 }, 131 { "ms5607", MS5607 }, 132 { } 133 }; 134 MODULE_DEVICE_TABLE(spi, ms5611_id); 135 136 static struct spi_driver ms5611_driver = { 137 .driver = { 138 .name = "ms5611", 139 .of_match_table = of_match_ptr(ms5611_spi_matches) 140 }, 141 .id_table = ms5611_id, 142 .probe = ms5611_spi_probe, 143 .remove = ms5611_spi_remove, 144 }; 145 module_spi_driver(ms5611_driver); 146 147 MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>"); 148 MODULE_DESCRIPTION("MS5611 spi driver"); 149 MODULE_LICENSE("GPL v2"); 150