r8152: fix tx packets accounting
[linux/fpc-iii.git] / drivers / iio / proximity / pulsedlight-lidar-lite-v2.c
blob3141c3c161bb4335cae5bc16f40c72339c474948
1 /*
2 * pulsedlight-lidar-lite-v2.c - Support for PulsedLight LIDAR sensor
4 * Copyright (C) 2015 Matt Ranostay <mranostay@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * TODO: interrupt mode, and signal strength reporting
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/i2c.h>
22 #include <linux/delay.h>
23 #include <linux/module.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
27 #include <linux/iio/buffer.h>
28 #include <linux/iio/trigger.h>
29 #include <linux/iio/triggered_buffer.h>
30 #include <linux/iio/trigger_consumer.h>
32 #define LIDAR_REG_CONTROL 0x00
33 #define LIDAR_REG_CONTROL_ACQUIRE BIT(2)
35 #define LIDAR_REG_STATUS 0x01
36 #define LIDAR_REG_STATUS_INVALID BIT(3)
37 #define LIDAR_REG_STATUS_READY BIT(0)
39 #define LIDAR_REG_DATA_HBYTE 0x0f
40 #define LIDAR_REG_DATA_LBYTE 0x10
41 #define LIDAR_REG_DATA_WORD_READ BIT(7)
43 #define LIDAR_REG_PWR_CONTROL 0x65
45 #define LIDAR_DRV_NAME "lidar"
47 struct lidar_data {
48 struct iio_dev *indio_dev;
49 struct i2c_client *client;
51 int (*xfer)(struct lidar_data *data, u8 reg, u8 *val, int len);
52 int i2c_enabled;
54 u16 buffer[8]; /* 2 byte distance + 8 byte timestamp */
57 static const struct iio_chan_spec lidar_channels[] = {
59 .type = IIO_DISTANCE,
60 .info_mask_separate =
61 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
62 .scan_index = 0,
63 .scan_type = {
64 .sign = 'u',
65 .realbits = 16,
66 .storagebits = 16,
69 IIO_CHAN_SOFT_TIMESTAMP(1),
72 static int lidar_i2c_xfer(struct lidar_data *data, u8 reg, u8 *val, int len)
74 struct i2c_client *client = data->client;
75 struct i2c_msg msg[2];
76 int ret;
78 msg[0].addr = client->addr;
79 msg[0].flags = client->flags | I2C_M_STOP;
80 msg[0].len = 1;
81 msg[0].buf = (char *) &reg;
83 msg[1].addr = client->addr;
84 msg[1].flags = client->flags | I2C_M_RD;
85 msg[1].len = len;
86 msg[1].buf = (char *) val;
88 ret = i2c_transfer(client->adapter, msg, 2);
90 return (ret == 2) ? 0 : -EIO;
93 static int lidar_smbus_xfer(struct lidar_data *data, u8 reg, u8 *val, int len)
95 struct i2c_client *client = data->client;
96 int ret;
99 * Device needs a STOP condition between address write, and data read
100 * so in turn i2c_smbus_read_byte_data cannot be used
103 while (len--) {
104 ret = i2c_smbus_write_byte(client, reg++);
105 if (ret < 0) {
106 dev_err(&client->dev, "cannot write addr value");
107 return ret;
110 ret = i2c_smbus_read_byte(client);
111 if (ret < 0) {
112 dev_err(&client->dev, "cannot read data value");
113 return ret;
116 *(val++) = ret;
119 return 0;
122 static int lidar_read_byte(struct lidar_data *data, u8 reg)
124 int ret;
125 u8 val;
127 ret = data->xfer(data, reg, &val, 1);
128 if (ret < 0)
129 return ret;
131 return val;
134 static inline int lidar_write_control(struct lidar_data *data, int val)
136 return i2c_smbus_write_byte_data(data->client, LIDAR_REG_CONTROL, val);
139 static inline int lidar_write_power(struct lidar_data *data, int val)
141 return i2c_smbus_write_byte_data(data->client,
142 LIDAR_REG_PWR_CONTROL, val);
145 static int lidar_read_measurement(struct lidar_data *data, u16 *reg)
147 int ret = data->xfer(data, LIDAR_REG_DATA_HBYTE |
148 (data->i2c_enabled ? LIDAR_REG_DATA_WORD_READ : 0),
149 (u8 *) reg, 2);
151 if (!ret)
152 *reg = be16_to_cpu(*reg);
154 return ret;
157 static int lidar_get_measurement(struct lidar_data *data, u16 *reg)
159 struct i2c_client *client = data->client;
160 int tries = 10;
161 int ret;
163 pm_runtime_get_sync(&client->dev);
165 /* start sample */
166 ret = lidar_write_control(data, LIDAR_REG_CONTROL_ACQUIRE);
167 if (ret < 0) {
168 dev_err(&client->dev, "cannot send start measurement command");
169 return ret;
172 while (tries--) {
173 usleep_range(1000, 2000);
175 ret = lidar_read_byte(data, LIDAR_REG_STATUS);
176 if (ret < 0)
177 break;
179 /* return -EINVAL since laser is likely pointed out of range */
180 if (ret & LIDAR_REG_STATUS_INVALID) {
181 *reg = 0;
182 ret = -EINVAL;
183 break;
186 /* sample ready to read */
187 if (!(ret & LIDAR_REG_STATUS_READY)) {
188 ret = lidar_read_measurement(data, reg);
189 break;
191 ret = -EIO;
193 pm_runtime_mark_last_busy(&client->dev);
194 pm_runtime_put_autosuspend(&client->dev);
196 return ret;
199 static int lidar_read_raw(struct iio_dev *indio_dev,
200 struct iio_chan_spec const *chan,
201 int *val, int *val2, long mask)
203 struct lidar_data *data = iio_priv(indio_dev);
204 int ret = -EINVAL;
206 switch (mask) {
207 case IIO_CHAN_INFO_RAW: {
208 u16 reg;
210 if (iio_device_claim_direct_mode(indio_dev))
211 return -EBUSY;
213 ret = lidar_get_measurement(data, &reg);
214 if (!ret) {
215 *val = reg;
216 ret = IIO_VAL_INT;
218 iio_device_release_direct_mode(indio_dev);
219 break;
221 case IIO_CHAN_INFO_SCALE:
222 *val = 0;
223 *val2 = 10000;
224 ret = IIO_VAL_INT_PLUS_MICRO;
225 break;
228 return ret;
231 static irqreturn_t lidar_trigger_handler(int irq, void *private)
233 struct iio_poll_func *pf = private;
234 struct iio_dev *indio_dev = pf->indio_dev;
235 struct lidar_data *data = iio_priv(indio_dev);
236 int ret;
238 ret = lidar_get_measurement(data, data->buffer);
239 if (!ret) {
240 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
241 iio_get_time_ns(indio_dev));
242 } else if (ret != -EINVAL) {
243 dev_err(&data->client->dev, "cannot read LIDAR measurement");
246 iio_trigger_notify_done(indio_dev->trig);
248 return IRQ_HANDLED;
251 static const struct iio_info lidar_info = {
252 .driver_module = THIS_MODULE,
253 .read_raw = lidar_read_raw,
256 static int lidar_probe(struct i2c_client *client,
257 const struct i2c_device_id *id)
259 struct lidar_data *data;
260 struct iio_dev *indio_dev;
261 int ret;
263 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
264 if (!indio_dev)
265 return -ENOMEM;
266 data = iio_priv(indio_dev);
268 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
269 data->xfer = lidar_i2c_xfer;
270 data->i2c_enabled = 1;
271 } else if (i2c_check_functionality(client->adapter,
272 I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BYTE))
273 data->xfer = lidar_smbus_xfer;
274 else
275 return -EOPNOTSUPP;
277 indio_dev->info = &lidar_info;
278 indio_dev->name = LIDAR_DRV_NAME;
279 indio_dev->channels = lidar_channels;
280 indio_dev->num_channels = ARRAY_SIZE(lidar_channels);
281 indio_dev->modes = INDIO_DIRECT_MODE;
283 i2c_set_clientdata(client, indio_dev);
285 data->client = client;
286 data->indio_dev = indio_dev;
288 ret = iio_triggered_buffer_setup(indio_dev, NULL,
289 lidar_trigger_handler, NULL);
290 if (ret)
291 return ret;
293 ret = iio_device_register(indio_dev);
294 if (ret)
295 goto error_unreg_buffer;
297 pm_runtime_set_autosuspend_delay(&client->dev, 1000);
298 pm_runtime_use_autosuspend(&client->dev);
300 ret = pm_runtime_set_active(&client->dev);
301 if (ret)
302 goto error_unreg_buffer;
303 pm_runtime_enable(&client->dev);
305 pm_runtime_mark_last_busy(&client->dev);
306 pm_runtime_idle(&client->dev);
308 return 0;
310 error_unreg_buffer:
311 iio_triggered_buffer_cleanup(indio_dev);
313 return ret;
316 static int lidar_remove(struct i2c_client *client)
318 struct iio_dev *indio_dev = i2c_get_clientdata(client);
320 iio_device_unregister(indio_dev);
321 iio_triggered_buffer_cleanup(indio_dev);
323 pm_runtime_disable(&client->dev);
324 pm_runtime_set_suspended(&client->dev);
326 return 0;
329 static const struct i2c_device_id lidar_id[] = {
330 {"lidar-lite-v2", 0},
331 { },
333 MODULE_DEVICE_TABLE(i2c, lidar_id);
335 static const struct of_device_id lidar_dt_ids[] = {
336 { .compatible = "pulsedlight,lidar-lite-v2" },
339 MODULE_DEVICE_TABLE(of, lidar_dt_ids);
341 #ifdef CONFIG_PM
342 static int lidar_pm_runtime_suspend(struct device *dev)
344 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
345 struct lidar_data *data = iio_priv(indio_dev);
347 return lidar_write_power(data, 0x0f);
350 static int lidar_pm_runtime_resume(struct device *dev)
352 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
353 struct lidar_data *data = iio_priv(indio_dev);
354 int ret = lidar_write_power(data, 0);
356 /* regulator and FPGA needs settling time */
357 usleep_range(15000, 20000);
359 return ret;
361 #endif
363 static const struct dev_pm_ops lidar_pm_ops = {
364 SET_RUNTIME_PM_OPS(lidar_pm_runtime_suspend,
365 lidar_pm_runtime_resume, NULL)
368 static struct i2c_driver lidar_driver = {
369 .driver = {
370 .name = LIDAR_DRV_NAME,
371 .of_match_table = of_match_ptr(lidar_dt_ids),
372 .pm = &lidar_pm_ops,
374 .probe = lidar_probe,
375 .remove = lidar_remove,
376 .id_table = lidar_id,
378 module_i2c_driver(lidar_driver);
380 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
381 MODULE_DESCRIPTION("PulsedLight LIDAR sensor");
382 MODULE_LICENSE("GPL");