GNU Linux-libre 4.19.314-gnu1
[releases.git] / drivers / iio / pressure / ms5611_i2c.c
1 /*
2  * MS5611 pressure and temperature sensor driver (I2C 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  * 7-bit I2C slave addresses:
11  *
12  * 0x77 (CSB pin low)
13  * 0x76 (CSB pin high)
14  *
15  */
16
17 #include <linux/delay.h>
18 #include <linux/i2c.h>
19 #include <linux/module.h>
20 #include <linux/of_device.h>
21
22 #include "ms5611.h"
23
24 static int ms5611_i2c_reset(struct ms5611_state *st)
25 {
26         return i2c_smbus_write_byte(st->client, MS5611_RESET);
27 }
28
29 static int ms5611_i2c_read_prom_word(struct ms5611_state *st, int index,
30                                      u16 *word)
31 {
32         int ret;
33
34         ret = i2c_smbus_read_word_swapped(st->client,
35                         MS5611_READ_PROM_WORD + (index << 1));
36         if (ret < 0)
37                 return ret;
38
39         *word = ret;
40
41         return 0;
42 }
43
44 static int ms5611_i2c_read_adc(struct ms5611_state *st, s32 *val)
45 {
46         int ret;
47         u8 buf[3];
48
49         ret = i2c_smbus_read_i2c_block_data(st->client, MS5611_READ_ADC,
50                                             3, buf);
51         if (ret < 0)
52                 return ret;
53
54         *val = (buf[0] << 16) | (buf[1] << 8) | buf[2];
55
56         return 0;
57 }
58
59 static int ms5611_i2c_read_adc_temp_and_pressure(struct ms5611_state *st,
60                                                  s32 *temp, s32 *pressure)
61 {
62         int ret;
63         const struct ms5611_osr *osr = st->temp_osr;
64
65         ret = i2c_smbus_write_byte(st->client, osr->cmd);
66         if (ret < 0)
67                 return ret;
68
69         usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL));
70         ret = ms5611_i2c_read_adc(st, temp);
71         if (ret < 0)
72                 return ret;
73
74         osr = st->pressure_osr;
75         ret = i2c_smbus_write_byte(st->client, osr->cmd);
76         if (ret < 0)
77                 return ret;
78
79         usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL));
80         return ms5611_i2c_read_adc(st, pressure);
81 }
82
83 static int ms5611_i2c_probe(struct i2c_client *client,
84                             const struct i2c_device_id *id)
85 {
86         struct ms5611_state *st;
87         struct iio_dev *indio_dev;
88
89         if (!i2c_check_functionality(client->adapter,
90                                      I2C_FUNC_SMBUS_WRITE_BYTE |
91                                      I2C_FUNC_SMBUS_READ_WORD_DATA |
92                                      I2C_FUNC_SMBUS_READ_I2C_BLOCK))
93                 return -EOPNOTSUPP;
94
95         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
96         if (!indio_dev)
97                 return -ENOMEM;
98
99         st = iio_priv(indio_dev);
100         i2c_set_clientdata(client, indio_dev);
101         st->reset = ms5611_i2c_reset;
102         st->read_prom_word = ms5611_i2c_read_prom_word;
103         st->read_adc_temp_and_pressure = ms5611_i2c_read_adc_temp_and_pressure;
104         st->client = client;
105
106         return ms5611_probe(indio_dev, &client->dev, id->name, id->driver_data);
107 }
108
109 static int ms5611_i2c_remove(struct i2c_client *client)
110 {
111         return ms5611_remove(i2c_get_clientdata(client));
112 }
113
114 #if defined(CONFIG_OF)
115 static const struct of_device_id ms5611_i2c_matches[] = {
116         { .compatible = "meas,ms5611" },
117         { .compatible = "ms5611" },
118         { .compatible = "meas,ms5607" },
119         { .compatible = "ms5607" },
120         { }
121 };
122 MODULE_DEVICE_TABLE(of, ms5611_i2c_matches);
123 #endif
124
125 static const struct i2c_device_id ms5611_id[] = {
126         { "ms5611", MS5611 },
127         { "ms5607", MS5607 },
128         { }
129 };
130 MODULE_DEVICE_TABLE(i2c, ms5611_id);
131
132 static struct i2c_driver ms5611_driver = {
133         .driver = {
134                 .name = "ms5611",
135                 .of_match_table = of_match_ptr(ms5611_i2c_matches)
136         },
137         .id_table = ms5611_id,
138         .probe = ms5611_i2c_probe,
139         .remove = ms5611_i2c_remove,
140 };
141 module_i2c_driver(ms5611_driver);
142
143 MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
144 MODULE_DESCRIPTION("MS5611 i2c driver");
145 MODULE_LICENSE("GPL v2");