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