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 
16 #include "ms5611.h"
17 
18 static int ms5611_spi_reset(struct device *dev)
19 {
20 	u8 cmd = MS5611_RESET;
21 	struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
22 
23 	return spi_write_then_read(st->client, &cmd, 1, NULL, 0);
24 }
25 
26 static int ms5611_spi_read_prom_word(struct device *dev, int index, u16 *word)
27 {
28 	int ret;
29 	struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
30 
31 	ret = spi_w8r16be(st->client, MS5611_READ_PROM_WORD + (index << 1));
32 	if (ret < 0)
33 		return ret;
34 
35 	*word = ret;
36 
37 	return 0;
38 }
39 
40 static int ms5611_spi_read_adc(struct device *dev, s32 *val)
41 {
42 	int ret;
43 	u8 buf[3] = { MS5611_READ_ADC };
44 	struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
45 
46 	ret = spi_write_then_read(st->client, buf, 1, buf, 3);
47 	if (ret < 0)
48 		return ret;
49 
50 	*val = (buf[0] << 16) | (buf[1] << 8) | buf[2];
51 
52 	return 0;
53 }
54 
55 static int ms5611_spi_read_adc_temp_and_pressure(struct device *dev,
56 						 s32 *temp, s32 *pressure)
57 {
58 	u8 cmd;
59 	int ret;
60 	struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
61 
62 	cmd = MS5611_START_TEMP_CONV;
63 	ret = spi_write_then_read(st->client, &cmd, 1, NULL, 0);
64 	if (ret < 0)
65 		return ret;
66 
67 	usleep_range(MS5611_CONV_TIME_MIN, MS5611_CONV_TIME_MAX);
68 
69 	ret = ms5611_spi_read_adc(dev, temp);
70 	if (ret < 0)
71 		return ret;
72 
73 	cmd = MS5611_START_PRESSURE_CONV;
74 	ret = spi_write_then_read(st->client, &cmd, 1, NULL, 0);
75 	if (ret < 0)
76 		return ret;
77 
78 	usleep_range(MS5611_CONV_TIME_MIN, MS5611_CONV_TIME_MAX);
79 
80 	return ms5611_spi_read_adc(dev, pressure);
81 }
82 
83 static int ms5611_spi_probe(struct spi_device *spi)
84 {
85 	int ret;
86 	struct ms5611_state *st;
87 	struct iio_dev *indio_dev;
88 
89 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
90 	if (!indio_dev)
91 		return -ENOMEM;
92 
93 	spi->mode = SPI_MODE_0;
94 	spi->max_speed_hz = 20000000;
95 	spi->bits_per_word = 8;
96 	ret = spi_setup(spi);
97 	if (ret < 0)
98 		return ret;
99 
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;
105 
106 	return ms5611_probe(indio_dev, &spi->dev,
107 			    spi_get_device_id(spi)->driver_data);
108 }
109 
110 static const struct spi_device_id ms5611_id[] = {
111 	{ "ms5611", MS5611 },
112 	{ "ms5607", MS5607 },
113 	{ }
114 };
115 MODULE_DEVICE_TABLE(spi, ms5611_id);
116 
117 static struct spi_driver ms5611_driver = {
118 	.driver = {
119 		.name = "ms5611",
120 		.owner = THIS_MODULE,
121 	},
122 	.id_table = ms5611_id,
123 	.probe = ms5611_spi_probe,
124 };
125 module_spi_driver(ms5611_driver);
126 
127 MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
128 MODULE_DESCRIPTION("MS5611 spi driver");
129 MODULE_LICENSE("GPL v2");
130