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