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