Revert "unicode: Don't special case ignorable code points"
[linux.git] / drivers / iio / proximity / mb1232.c
blob614e65cb9d428419dc310c28c3ab06809dcc5eed
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/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 */
32 struct mb1232_data {
33 struct i2c_client *client;
35 struct mutex lock;
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
43 struct completion ranging;
44 int irqnr;
45 /* Ensure correct alignment of data to push to IIO buffer */
46 struct {
47 s16 distance;
48 s64 ts __aligned(8);
49 } scan;
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);
59 return IRQ_HANDLED;
62 static s16 mb1232_read_distance(struct mb1232_data *data)
64 struct i2c_client *client = data->client;
65 int ret;
66 s16 distance;
67 __be16 buf;
69 mutex_lock(&data->lock);
71 reinit_completion(&data->ranging);
73 ret = i2c_smbus_write_byte(client, MB1232_RANGE_COMMAND);
74 if (ret < 0) {
75 dev_err(&client->dev, "write command - err: %d\n", ret);
76 goto error_unlock;
79 if (data->irqnr > 0) {
80 /* it cannot take more than 100 ms */
81 ret = wait_for_completion_killable_timeout(&data->ranging,
82 HZ/10);
83 if (ret < 0)
84 goto error_unlock;
85 else if (ret == 0) {
86 ret = -ETIMEDOUT;
87 goto error_unlock;
89 } else {
90 /* use simple sleep if announce irq is not connected */
91 msleep(15);
94 ret = i2c_master_recv(client, (char *)&buf, sizeof(buf));
95 if (ret < 0) {
96 dev_err(&client->dev, "i2c_master_recv: ret=%d\n", ret);
97 goto error_unlock;
100 distance = __be16_to_cpu(buf);
101 /* check for not returning misleading error codes */
102 if (distance < 0) {
103 dev_err(&client->dev, "distance=%d\n", distance);
104 ret = -EINVAL;
105 goto error_unlock;
108 mutex_unlock(&data->lock);
110 return distance;
112 error_unlock:
113 mutex_unlock(&data->lock);
115 return ret;
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)
126 goto err;
128 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
129 pf->timestamp);
131 err:
132 iio_trigger_notify_done(indio_dev->trig);
133 return IRQ_HANDLED;
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);
141 int ret;
143 if (channel->type != IIO_DISTANCE)
144 return -EINVAL;
146 switch (mask) {
147 case IIO_CHAN_INFO_RAW:
148 ret = mb1232_read_distance(data);
149 if (ret < 0)
150 return ret;
151 *val = ret;
152 return IIO_VAL_INT;
153 case IIO_CHAN_INFO_SCALE:
154 /* 1 LSB is 1 cm */
155 *val = 0;
156 *val2 = 10000;
157 return IIO_VAL_INT_PLUS_MICRO;
158 default:
159 return -EINVAL;
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),
168 .scan_index = 0,
169 .scan_type = {
170 .sign = 's',
171 .realbits = 16,
172 .storagebits = 16,
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;
188 int ret;
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))
194 return -ENODEV;
196 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
197 if (!indio_dev)
198 return -ENOMEM;
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);
218 if (ret < 0) {
219 dev_err(dev, "request_irq: %d\n", ret);
220 return ret;
224 ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
225 iio_pollfunc_store_time, mb1232_trigger_handler, NULL);
226 if (ret < 0) {
227 dev_err(dev, "setup of iio triggered buffer failed\n");
228 return ret;
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 = {
260 .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");