GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / iio / pressure / ms5611_spi.c
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 ms5611_state *st)
20 {
21         u8 cmd = MS5611_RESET;
22
23         return spi_write_then_read(st->client, &cmd, 1, NULL, 0);
24 }
25
26 static int ms5611_spi_read_prom_word(struct ms5611_state *st, int index,
27                                      u16 *word)
28 {
29         int ret;
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 ms5611_state *st, s32 *val)
41 {
42         int ret;
43         u8 buf[3] = { MS5611_READ_ADC };
44
45         ret = spi_write_then_read(st->client, buf, 1, buf, 3);
46         if (ret < 0)
47                 return ret;
48
49         *val = (buf[0] << 16) | (buf[1] << 8) | buf[2];
50
51         return 0;
52 }
53
54 static int ms5611_spi_read_adc_temp_and_pressure(struct ms5611_state *st,
55                                                  s32 *temp, s32 *pressure)
56 {
57         int ret;
58         const struct ms5611_osr *osr = st->temp_osr;
59
60         /*
61          * Warning: &osr->cmd MUST be aligned on a word boundary since used as
62          * 2nd argument (void*) of spi_write_then_read.
63          */
64         ret = spi_write_then_read(st->client, &osr->cmd, 1, NULL, 0);
65         if (ret < 0)
66                 return ret;
67
68         usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL));
69         ret = ms5611_spi_read_adc(st, temp);
70         if (ret < 0)
71                 return ret;
72
73         osr = st->pressure_osr;
74         ret = spi_write_then_read(st->client, &osr->cmd, 1, NULL, 0);
75         if (ret < 0)
76                 return ret;
77
78         usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL));
79         return ms5611_spi_read_adc(st, pressure);
80 }
81
82 static int ms5611_spi_probe(struct spi_device *spi)
83 {
84         int ret;
85         struct ms5611_state *st;
86         struct iio_dev *indio_dev;
87
88         indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
89         if (!indio_dev)
90                 return -ENOMEM;
91
92         spi_set_drvdata(spi, indio_dev);
93
94         spi->mode = SPI_MODE_0;
95         spi->max_speed_hz = min(spi->max_speed_hz, 20000000U);
96         spi->bits_per_word = 8;
97         ret = spi_setup(spi);
98         if (ret < 0)
99                 return ret;
100
101         st = iio_priv(indio_dev);
102         st->reset = ms5611_spi_reset;
103         st->read_prom_word = ms5611_spi_read_prom_word;
104         st->read_adc_temp_and_pressure = ms5611_spi_read_adc_temp_and_pressure;
105         st->client = spi;
106
107         return ms5611_probe(indio_dev, &spi->dev, spi_get_device_id(spi)->name,
108                             spi_get_device_id(spi)->driver_data);
109 }
110
111 static int ms5611_spi_remove(struct spi_device *spi)
112 {
113         return ms5611_remove(spi_get_drvdata(spi));
114 }
115
116 #if defined(CONFIG_OF)
117 static const struct of_device_id ms5611_spi_matches[] = {
118         { .compatible = "meas,ms5611" },
119         { .compatible = "ms5611" },
120         { .compatible = "meas,ms5607" },
121         { .compatible = "ms5607" },
122         { }
123 };
124 MODULE_DEVICE_TABLE(of, ms5611_spi_matches);
125 #endif
126
127 static const struct spi_device_id ms5611_id[] = {
128         { "ms5611", MS5611 },
129         { "ms5607", MS5607 },
130         { }
131 };
132 MODULE_DEVICE_TABLE(spi, ms5611_id);
133
134 static struct spi_driver ms5611_driver = {
135         .driver = {
136                 .name = "ms5611",
137                 .of_match_table = of_match_ptr(ms5611_spi_matches)
138         },
139         .id_table = ms5611_id,
140         .probe = ms5611_spi_probe,
141         .remove = ms5611_spi_remove,
142 };
143 module_spi_driver(ms5611_driver);
144
145 MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
146 MODULE_DESCRIPTION("MS5611 spi driver");
147 MODULE_LICENSE("GPL v2");