1 // SPDX-License-Identifier: GPL-2.0+
3 * mb1232.c - Support for MaxBotix I2CXL-MaxSonar-EZ series ultrasonic
4 * ranger with i2c interface
5 * actually tested with mb1232 type
9 * For details about the device see:
10 * https://www.maxbotix.com/documents/I2CXL-MaxSonar-EZ_Datasheet.pdf
13 #include <linux/err.h>
14 #include <linux/i2c.h>
15 #include <linux/of_irq.h>
16 #include <linux/delay.h>
17 #include <linux/module.h>
18 #include <linux/bitops.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/buffer.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/triggered_buffer.h>
25 /* registers of MaxSonar device */
26 #define MB1232_RANGE_COMMAND 0x51 /* Command for reading range */
27 #define MB1232_ADDR_UNLOCK_1 0xAA /* Command 1 for changing address */
28 #define MB1232_ADDR_UNLOCK_2 0xA5 /* Command 2 for changing address */
31 struct i2c_client *client;
36 * optionally a gpio can be used to announce when ranging has
38 * since we are just using the falling trigger of it we request
39 * only the interrupt for announcing when data is ready to be read
41 struct completion ranging;
45 static irqreturn_t mb1232_handle_irq(int irq, void *dev_id)
47 struct iio_dev *indio_dev = dev_id;
48 struct mb1232_data *data = iio_priv(indio_dev);
50 complete(&data->ranging);
55 static s16 mb1232_read_distance(struct mb1232_data *data)
57 struct i2c_client *client = data->client;
62 mutex_lock(&data->lock);
64 reinit_completion(&data->ranging);
66 ret = i2c_smbus_write_byte(client, MB1232_RANGE_COMMAND);
68 dev_err(&client->dev, "write command - err: %d\n", ret);
72 if (data->irqnr >= 0) {
73 /* it cannot take more than 100 ms */
74 ret = wait_for_completion_killable_timeout(&data->ranging,
83 /* use simple sleep if announce irq is not connected */
87 ret = i2c_master_recv(client, (char *)&buf, sizeof(buf));
89 dev_err(&client->dev, "i2c_master_recv: ret=%d\n", ret);
93 distance = __be16_to_cpu(buf);
94 /* check for not returning misleading error codes */
96 dev_err(&client->dev, "distance=%d\n", distance);
101 mutex_unlock(&data->lock);
106 mutex_unlock(&data->lock);
111 static irqreturn_t mb1232_trigger_handler(int irq, void *p)
113 struct iio_poll_func *pf = p;
114 struct iio_dev *indio_dev = pf->indio_dev;
115 struct mb1232_data *data = iio_priv(indio_dev);
118 * 16-bit channel + 48-bit padding + 64-bit timestamp
120 s16 buffer[8] = { 0 };
122 buffer[0] = mb1232_read_distance(data);
126 iio_push_to_buffers_with_timestamp(indio_dev, buffer, pf->timestamp);
129 iio_trigger_notify_done(indio_dev->trig);
133 static int mb1232_read_raw(struct iio_dev *indio_dev,
134 struct iio_chan_spec const *channel, int *val,
135 int *val2, long mask)
137 struct mb1232_data *data = iio_priv(indio_dev);
140 if (channel->type != IIO_DISTANCE)
144 case IIO_CHAN_INFO_RAW:
145 ret = mb1232_read_distance(data);
150 case IIO_CHAN_INFO_SCALE:
154 return IIO_VAL_INT_PLUS_MICRO;
160 static const struct iio_chan_spec mb1232_channels[] = {
162 .type = IIO_DISTANCE,
163 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
164 BIT(IIO_CHAN_INFO_SCALE),
170 .endianness = IIO_CPU,
173 IIO_CHAN_SOFT_TIMESTAMP(1),
176 static const struct iio_info mb1232_info = {
177 .read_raw = mb1232_read_raw,
180 static int mb1232_probe(struct i2c_client *client,
181 const struct i2c_device_id *id)
183 struct iio_dev *indio_dev;
184 struct mb1232_data *data;
186 struct device *dev = &client->dev;
188 if (!i2c_check_functionality(client->adapter,
189 I2C_FUNC_SMBUS_READ_BYTE |
190 I2C_FUNC_SMBUS_WRITE_BYTE))
193 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
197 data = iio_priv(indio_dev);
198 i2c_set_clientdata(client, indio_dev);
199 data->client = client;
201 indio_dev->info = &mb1232_info;
202 indio_dev->name = id->name;
203 indio_dev->dev.parent = dev;
204 indio_dev->modes = INDIO_DIRECT_MODE;
205 indio_dev->channels = mb1232_channels;
206 indio_dev->num_channels = ARRAY_SIZE(mb1232_channels);
208 mutex_init(&data->lock);
210 init_completion(&data->ranging);
212 data->irqnr = irq_of_parse_and_map(dev->of_node, 0);
213 if (data->irqnr <= 0) {
214 /* usage of interrupt is optional */
217 ret = devm_request_irq(dev, data->irqnr, mb1232_handle_irq,
218 IRQF_TRIGGER_FALLING, id->name, indio_dev);
220 dev_err(dev, "request_irq: %d\n", ret);
225 ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
226 iio_pollfunc_store_time, mb1232_trigger_handler, NULL);
228 dev_err(dev, "setup of iio triggered buffer failed\n");
232 return devm_iio_device_register(dev, indio_dev);
235 static const struct of_device_id of_mb1232_match[] = {
236 { .compatible = "maxbotix,mb1202", },
237 { .compatible = "maxbotix,mb1212", },
238 { .compatible = "maxbotix,mb1222", },
239 { .compatible = "maxbotix,mb1232", },
240 { .compatible = "maxbotix,mb1242", },
241 { .compatible = "maxbotix,mb7040", },
242 { .compatible = "maxbotix,mb7137", },
246 MODULE_DEVICE_TABLE(of, of_mb1232_match);
248 static const struct i2c_device_id mb1232_id[] = {
249 { "maxbotix-mb1202", },
250 { "maxbotix-mb1212", },
251 { "maxbotix-mb1222", },
252 { "maxbotix-mb1232", },
253 { "maxbotix-mb1242", },
254 { "maxbotix-mb7040", },
255 { "maxbotix-mb7137", },
258 MODULE_DEVICE_TABLE(i2c, mb1232_id);
260 static struct i2c_driver mb1232_driver = {
262 .name = "maxbotix-mb1232",
263 .of_match_table = of_mb1232_match,
265 .probe = mb1232_probe,
266 .id_table = mb1232_id,
268 module_i2c_driver(mb1232_driver);
271 MODULE_DESCRIPTION("Maxbotix I2CXL-MaxSonar i2c ultrasonic ranger driver");
272 MODULE_LICENSE("GPL");