Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / iio / proximity / mb1232.c
blob166b3e6d7db896fcd7de4e539a2985f57664cc4c
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
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/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 */
30 struct mb1232_data {
31 struct i2c_client *client;
33 struct mutex lock;
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
41 struct completion ranging;
42 int irqnr;
45 static irqreturn_t mb1232_handle_irq(int irq, void *dev_id)
47 struct iio_dev *indio_dev = dev_id;
48 struct mb1232_data *data = iio_priv(indio_dev);
50 complete(&data->ranging);
52 return IRQ_HANDLED;
55 static s16 mb1232_read_distance(struct mb1232_data *data)
57 struct i2c_client *client = data->client;
58 int ret;
59 s16 distance;
60 __be16 buf;
62 mutex_lock(&data->lock);
64 reinit_completion(&data->ranging);
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;
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;
82 } else {
83 /* use simple sleep if announce irq is not connected */
84 msleep(15);
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;
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;
101 mutex_unlock(&data->lock);
103 return distance;
105 error_unlock:
106 mutex_unlock(&data->lock);
108 return ret;
111 static irqreturn_t mb1232_trigger_handler(int irq, void *p)
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);
117 * triggered buffer
118 * 16-bit channel + 48-bit padding + 64-bit timestamp
120 s16 buffer[8] = { 0 };
122 buffer[0] = mb1232_read_distance(data);
123 if (buffer[0] < 0)
124 goto err;
126 iio_push_to_buffers_with_timestamp(indio_dev, buffer, pf->timestamp);
128 err:
129 iio_trigger_notify_done(indio_dev->trig);
130 return IRQ_HANDLED;
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)
137 struct mb1232_data *data = iio_priv(indio_dev);
138 int ret;
140 if (channel->type != IIO_DISTANCE)
141 return -EINVAL;
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;
160 static const struct iio_chan_spec mb1232_channels[] = {
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,
173 IIO_CHAN_SOFT_TIMESTAMP(1),
176 static const struct iio_info mb1232_info = {
177 .read_raw = mb1232_read_raw,
180 static int mb1232_probe(struct i2c_client *client,
181 const struct i2c_device_id *id)
183 struct iio_dev *indio_dev;
184 struct mb1232_data *data;
185 int ret;
186 struct device *dev = &client->dev;
188 if (!i2c_check_functionality(client->adapter,
189 I2C_FUNC_SMBUS_READ_BYTE |
190 I2C_FUNC_SMBUS_WRITE_BYTE))
191 return -ENODEV;
193 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
194 if (!indio_dev)
195 return -ENOMEM;
197 data = iio_priv(indio_dev);
198 i2c_set_clientdata(client, indio_dev);
199 data->client = client;
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);
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 */
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;
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;
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 = {
261 .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);
270 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
271 MODULE_DESCRIPTION("Maxbotix I2CXL-MaxSonar i2c ultrasonic ranger driver");
272 MODULE_LICENSE("GPL");