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
7 * Copyright (c) 2019 Andreas Klinger <ak@it-klinger.de>
9 * For details about the device see:
10 * https://www.maxbotix.com/documents/I2CXL-MaxSonar-EZ_Datasheet.pdf
13 #include <linux/bitops.h>
14 #include <linux/err.h>
15 #include <linux/i2c.h>
16 #include <linux/delay.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/module.h>
19 #include <linux/property.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/trigger_consumer.h>
25 #include <linux/iio/triggered_buffer.h>
27 /* registers of MaxSonar device */
28 #define MB1232_RANGE_COMMAND 0x51 /* Command for reading range */
29 #define MB1232_ADDR_UNLOCK_1 0xAA /* Command 1 for changing address */
30 #define MB1232_ADDR_UNLOCK_2 0xA5 /* Command 2 for changing address */
33 struct i2c_client
*client
;
38 * optionally a gpio can be used to announce when ranging has
40 * since we are just using the falling trigger of it we request
41 * only the interrupt for announcing when data is ready to be read
43 struct completion ranging
;
45 /* Ensure correct alignment of data to push to IIO buffer */
52 static irqreturn_t
mb1232_handle_irq(int irq
, void *dev_id
)
54 struct iio_dev
*indio_dev
= dev_id
;
55 struct mb1232_data
*data
= iio_priv(indio_dev
);
57 complete(&data
->ranging
);
62 static s16
mb1232_read_distance(struct mb1232_data
*data
)
64 struct i2c_client
*client
= data
->client
;
69 mutex_lock(&data
->lock
);
71 reinit_completion(&data
->ranging
);
73 ret
= i2c_smbus_write_byte(client
, MB1232_RANGE_COMMAND
);
75 dev_err(&client
->dev
, "write command - err: %d\n", ret
);
79 if (data
->irqnr
> 0) {
80 /* it cannot take more than 100 ms */
81 ret
= wait_for_completion_killable_timeout(&data
->ranging
,
90 /* use simple sleep if announce irq is not connected */
94 ret
= i2c_master_recv(client
, (char *)&buf
, sizeof(buf
));
96 dev_err(&client
->dev
, "i2c_master_recv: ret=%d\n", ret
);
100 distance
= __be16_to_cpu(buf
);
101 /* check for not returning misleading error codes */
103 dev_err(&client
->dev
, "distance=%d\n", distance
);
108 mutex_unlock(&data
->lock
);
113 mutex_unlock(&data
->lock
);
118 static irqreturn_t
mb1232_trigger_handler(int irq
, void *p
)
120 struct iio_poll_func
*pf
= p
;
121 struct iio_dev
*indio_dev
= pf
->indio_dev
;
122 struct mb1232_data
*data
= iio_priv(indio_dev
);
124 data
->scan
.distance
= mb1232_read_distance(data
);
125 if (data
->scan
.distance
< 0)
128 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->scan
,
132 iio_trigger_notify_done(indio_dev
->trig
);
136 static int mb1232_read_raw(struct iio_dev
*indio_dev
,
137 struct iio_chan_spec
const *channel
, int *val
,
138 int *val2
, long mask
)
140 struct mb1232_data
*data
= iio_priv(indio_dev
);
143 if (channel
->type
!= IIO_DISTANCE
)
147 case IIO_CHAN_INFO_RAW
:
148 ret
= mb1232_read_distance(data
);
153 case IIO_CHAN_INFO_SCALE
:
157 return IIO_VAL_INT_PLUS_MICRO
;
163 static const struct iio_chan_spec mb1232_channels
[] = {
165 .type
= IIO_DISTANCE
,
166 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
167 BIT(IIO_CHAN_INFO_SCALE
),
173 .endianness
= IIO_CPU
,
176 IIO_CHAN_SOFT_TIMESTAMP(1),
179 static const struct iio_info mb1232_info
= {
180 .read_raw
= mb1232_read_raw
,
183 static int mb1232_probe(struct i2c_client
*client
)
185 const struct i2c_device_id
*id
= i2c_client_get_device_id(client
);
186 struct iio_dev
*indio_dev
;
187 struct mb1232_data
*data
;
189 struct device
*dev
= &client
->dev
;
191 if (!i2c_check_functionality(client
->adapter
,
192 I2C_FUNC_SMBUS_READ_BYTE
|
193 I2C_FUNC_SMBUS_WRITE_BYTE
))
196 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
200 data
= iio_priv(indio_dev
);
201 i2c_set_clientdata(client
, indio_dev
);
202 data
->client
= client
;
204 indio_dev
->info
= &mb1232_info
;
205 indio_dev
->name
= id
->name
;
206 indio_dev
->modes
= INDIO_DIRECT_MODE
;
207 indio_dev
->channels
= mb1232_channels
;
208 indio_dev
->num_channels
= ARRAY_SIZE(mb1232_channels
);
210 mutex_init(&data
->lock
);
212 init_completion(&data
->ranging
);
214 data
->irqnr
= fwnode_irq_get(dev_fwnode(&client
->dev
), 0);
215 if (data
->irqnr
> 0) {
216 ret
= devm_request_irq(dev
, data
->irqnr
, mb1232_handle_irq
,
217 IRQF_TRIGGER_FALLING
, id
->name
, indio_dev
);
219 dev_err(dev
, "request_irq: %d\n", ret
);
224 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
,
225 iio_pollfunc_store_time
, mb1232_trigger_handler
, NULL
);
227 dev_err(dev
, "setup of iio triggered buffer failed\n");
231 return devm_iio_device_register(dev
, indio_dev
);
234 static const struct of_device_id of_mb1232_match
[] = {
235 { .compatible
= "maxbotix,mb1202", },
236 { .compatible
= "maxbotix,mb1212", },
237 { .compatible
= "maxbotix,mb1222", },
238 { .compatible
= "maxbotix,mb1232", },
239 { .compatible
= "maxbotix,mb1242", },
240 { .compatible
= "maxbotix,mb7040", },
241 { .compatible
= "maxbotix,mb7137", },
245 MODULE_DEVICE_TABLE(of
, of_mb1232_match
);
247 static const struct i2c_device_id mb1232_id
[] = {
248 { "maxbotix-mb1202", },
249 { "maxbotix-mb1212", },
250 { "maxbotix-mb1222", },
251 { "maxbotix-mb1232", },
252 { "maxbotix-mb1242", },
253 { "maxbotix-mb7040", },
254 { "maxbotix-mb7137", },
257 MODULE_DEVICE_TABLE(i2c
, mb1232_id
);
259 static struct i2c_driver mb1232_driver
= {
261 .name
= "maxbotix-mb1232",
262 .of_match_table
= of_mb1232_match
,
264 .probe
= mb1232_probe
,
265 .id_table
= mb1232_id
,
267 module_i2c_driver(mb1232_driver
);
269 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
270 MODULE_DESCRIPTION("Maxbotix I2CXL-MaxSonar i2c ultrasonic ranger driver");
271 MODULE_LICENSE("GPL");