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;
43 /* Ensure correct alignment of data to push to IIO buffer */
50 static irqreturn_t mb1232_handle_irq(int irq, void *dev_id)
52 struct iio_dev *indio_dev = dev_id;
53 struct mb1232_data *data = iio_priv(indio_dev);
55 complete(&data->ranging);
60 static s16 mb1232_read_distance(struct mb1232_data *data)
62 struct i2c_client *client = data->client;
67 mutex_lock(&data->lock);
69 reinit_completion(&data->ranging);
71 ret = i2c_smbus_write_byte(client, MB1232_RANGE_COMMAND);
73 dev_err(&client->dev, "write command - err: %d\n", ret);
77 if (data->irqnr >= 0) {
78 /* it cannot take more than 100 ms */
79 ret = wait_for_completion_killable_timeout(&data->ranging,
88 /* use simple sleep if announce irq is not connected */
92 ret = i2c_master_recv(client, (char *)&buf, sizeof(buf));
94 dev_err(&client->dev, "i2c_master_recv: ret=%d\n", ret);
98 distance = __be16_to_cpu(buf);
99 /* check for not returning misleading error codes */
101 dev_err(&client->dev, "distance=%d\n", distance);
106 mutex_unlock(&data->lock);
111 mutex_unlock(&data->lock);
116 static irqreturn_t mb1232_trigger_handler(int irq, void *p)
118 struct iio_poll_func *pf = p;
119 struct iio_dev *indio_dev = pf->indio_dev;
120 struct mb1232_data *data = iio_priv(indio_dev);
122 data->scan.distance = mb1232_read_distance(data);
123 if (data->scan.distance < 0)
126 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
130 iio_trigger_notify_done(indio_dev->trig);
134 static int mb1232_read_raw(struct iio_dev *indio_dev,
135 struct iio_chan_spec const *channel, int *val,
136 int *val2, long mask)
138 struct mb1232_data *data = iio_priv(indio_dev);
141 if (channel->type != IIO_DISTANCE)
145 case IIO_CHAN_INFO_RAW:
146 ret = mb1232_read_distance(data);
151 case IIO_CHAN_INFO_SCALE:
155 return IIO_VAL_INT_PLUS_MICRO;
161 static const struct iio_chan_spec mb1232_channels[] = {
163 .type = IIO_DISTANCE,
164 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
165 BIT(IIO_CHAN_INFO_SCALE),
171 .endianness = IIO_CPU,
174 IIO_CHAN_SOFT_TIMESTAMP(1),
177 static const struct iio_info mb1232_info = {
178 .read_raw = mb1232_read_raw,
181 static int mb1232_probe(struct i2c_client *client,
182 const struct i2c_device_id *id)
184 struct iio_dev *indio_dev;
185 struct mb1232_data *data;
187 struct device *dev = &client->dev;
189 if (!i2c_check_functionality(client->adapter,
190 I2C_FUNC_SMBUS_READ_BYTE |
191 I2C_FUNC_SMBUS_WRITE_BYTE))
194 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
198 data = iio_priv(indio_dev);
199 i2c_set_clientdata(client, indio_dev);
200 data->client = client;
202 indio_dev->info = &mb1232_info;
203 indio_dev->name = id->name;
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");