File Index
Files and directories
Sign in
FILE

mb1232.c

File preview with syntax highlighting.

linux-6.19.14/drivers/iio/proximity/mb1232.c C
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * mb1232.c - Support for MaxBotix I2CXL-MaxSonar-EZ series ultrasonic
  4 *   ranger with i2c interface
  5 * actually tested with mb1232 type
  6 *
  7 * Copyright (c) 2019 Andreas Klinger <ak@it-klinger.de>
  8 *
  9 * For details about the device see:
 10 * https://www.maxbotix.com/documents/I2CXL-MaxSonar-EZ_Datasheet.pdf
 11 */
 12
 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>
 20
 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>
 26
 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 */
 31
 32struct mb1232_data {
 33	struct i2c_client	*client;
 34
 35	struct mutex		lock;
 36
 37	/*
 38	 * optionally a gpio can be used to announce when ranging has
 39	 * finished
 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
 42	 */
 43	struct completion	ranging;
 44	int			irqnr;
 45};
 46
 47static irqreturn_t mb1232_handle_irq(int irq, void *dev_id)
 48{
 49	struct iio_dev *indio_dev = dev_id;
 50	struct mb1232_data *data = iio_priv(indio_dev);
 51
 52	complete(&data->ranging);
 53
 54	return IRQ_HANDLED;
 55}
 56
 57static s16 mb1232_read_distance(struct mb1232_data *data)
 58{
 59	struct i2c_client *client = data->client;
 60	int ret;
 61	s16 distance;
 62	__be16 buf;
 63
 64	mutex_lock(&data->lock);
 65
 66	reinit_completion(&data->ranging);
 67
 68	ret = i2c_smbus_write_byte(client, MB1232_RANGE_COMMAND);
 69	if (ret < 0) {
 70		dev_err(&client->dev, "write command - err: %d\n", ret);
 71		goto error_unlock;
 72	}
 73
 74	if (data->irqnr > 0) {
 75		/* it cannot take more than 100 ms */
 76		ret = wait_for_completion_killable_timeout(&data->ranging,
 77									HZ/10);
 78		if (ret < 0)
 79			goto error_unlock;
 80		else if (ret == 0) {
 81			ret = -ETIMEDOUT;
 82			goto error_unlock;
 83		}
 84	} else {
 85		/* use simple sleep if announce irq is not connected */
 86		msleep(15);
 87	}
 88
 89	ret = i2c_master_recv(client, (char *)&buf, sizeof(buf));
 90	if (ret < 0) {
 91		dev_err(&client->dev, "i2c_master_recv: ret=%d\n", ret);
 92		goto error_unlock;
 93	}
 94
 95	distance = __be16_to_cpu(buf);
 96	/* check for not returning misleading error codes */
 97	if (distance < 0) {
 98		dev_err(&client->dev, "distance=%d\n", distance);
 99		ret = -EINVAL;
100		goto error_unlock;
101	}
102
103	mutex_unlock(&data->lock);
104
105	return distance;
106
107error_unlock:
108	mutex_unlock(&data->lock);
109
110	return ret;
111}
112
113static irqreturn_t mb1232_trigger_handler(int irq, void *p)
114{
115	struct iio_poll_func *pf = p;
116	struct iio_dev *indio_dev = pf->indio_dev;
117	struct mb1232_data *data = iio_priv(indio_dev);
118	struct {
119		s16 distance;
120		aligned_s64 ts;
121	} scan = { };
122
123	scan.distance = mb1232_read_distance(data);
124	if (scan.distance < 0)
125		goto err;
126
127	iio_push_to_buffers_with_ts(indio_dev, &scan, sizeof(scan),
128				    pf->timestamp);
129
130err:
131	iio_trigger_notify_done(indio_dev->trig);
132	return IRQ_HANDLED;
133}
134
135static int mb1232_read_raw(struct iio_dev *indio_dev,
136			    struct iio_chan_spec const *channel, int *val,
137			    int *val2, long mask)
138{
139	struct mb1232_data *data = iio_priv(indio_dev);
140	int ret;
141
142	if (channel->type != IIO_DISTANCE)
143		return -EINVAL;
144
145	switch (mask) {
146	case IIO_CHAN_INFO_RAW:
147		ret = mb1232_read_distance(data);
148		if (ret < 0)
149			return ret;
150		*val = ret;
151		return IIO_VAL_INT;
152	case IIO_CHAN_INFO_SCALE:
153		/* 1 LSB is 1 cm */
154		*val = 0;
155		*val2 = 10000;
156		return IIO_VAL_INT_PLUS_MICRO;
157	default:
158		return -EINVAL;
159	}
160}
161
162static const struct iio_chan_spec mb1232_channels[] = {
163	{
164		.type = IIO_DISTANCE,
165		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
166				      BIT(IIO_CHAN_INFO_SCALE),
167		.scan_index = 0,
168		.scan_type = {
169			.sign = 's',
170			.realbits = 16,
171			.storagebits = 16,
172			.endianness = IIO_CPU,
173		},
174	},
175	IIO_CHAN_SOFT_TIMESTAMP(1),
176};
177
178static const struct iio_info mb1232_info = {
179	.read_raw = mb1232_read_raw,
180};
181
182static int mb1232_probe(struct i2c_client *client)
183{
184	const struct i2c_device_id *id = i2c_client_get_device_id(client);
185	struct iio_dev *indio_dev;
186	struct mb1232_data *data;
187	int ret;
188	struct device *dev = &client->dev;
189
190	if (!i2c_check_functionality(client->adapter,
191					I2C_FUNC_SMBUS_READ_BYTE |
192					I2C_FUNC_SMBUS_WRITE_BYTE))
193		return -ENODEV;
194
195	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
196	if (!indio_dev)
197		return -ENOMEM;
198
199	data = iio_priv(indio_dev);
200	i2c_set_clientdata(client, indio_dev);
201	data->client = client;
202
203	indio_dev->info = &mb1232_info;
204	indio_dev->name = id->name;
205	indio_dev->modes = INDIO_DIRECT_MODE;
206	indio_dev->channels = mb1232_channels;
207	indio_dev->num_channels = ARRAY_SIZE(mb1232_channels);
208
209	mutex_init(&data->lock);
210
211	init_completion(&data->ranging);
212
213	data->irqnr = fwnode_irq_get(dev_fwnode(&client->dev), 0);
214	if (data->irqnr > 0) {
215		ret = devm_request_irq(dev, data->irqnr, mb1232_handle_irq,
216				IRQF_TRIGGER_FALLING, id->name, indio_dev);
217		if (ret < 0) {
218			dev_err(dev, "request_irq: %d\n", ret);
219			return ret;
220		}
221	}
222
223	ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
224			iio_pollfunc_store_time, mb1232_trigger_handler, NULL);
225	if (ret < 0) {
226		dev_err(dev, "setup of iio triggered buffer failed\n");
227		return ret;
228	}
229
230	return devm_iio_device_register(dev, indio_dev);
231}
232
233static const struct of_device_id of_mb1232_match[] = {
234	{ .compatible = "maxbotix,mb1202", },
235	{ .compatible = "maxbotix,mb1212", },
236	{ .compatible = "maxbotix,mb1222", },
237	{ .compatible = "maxbotix,mb1232", },
238	{ .compatible = "maxbotix,mb1242", },
239	{ .compatible = "maxbotix,mb7040", },
240	{ .compatible = "maxbotix,mb7137", },
241	{ }
242};
243
244MODULE_DEVICE_TABLE(of, of_mb1232_match);
245
246static const struct i2c_device_id mb1232_id[] = {
247	{ "maxbotix-mb1202", },
248	{ "maxbotix-mb1212", },
249	{ "maxbotix-mb1222", },
250	{ "maxbotix-mb1232", },
251	{ "maxbotix-mb1242", },
252	{ "maxbotix-mb7040", },
253	{ "maxbotix-mb7137", },
254	{ }
255};
256MODULE_DEVICE_TABLE(i2c, mb1232_id);
257
258static struct i2c_driver mb1232_driver = {
259	.driver = {
260		.name	= "maxbotix-mb1232",
261		.of_match_table	= of_mb1232_match,
262	},
263	.probe = mb1232_probe,
264	.id_table = mb1232_id,
265};
266module_i2c_driver(mb1232_driver);
267
268MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
269MODULE_DESCRIPTION("Maxbotix I2CXL-MaxSonar i2c ultrasonic ranger driver");
270MODULE_LICENSE("GPL");
~/ Read-only file preview.