Matt Ranostay | d6ad805 | 2018-02-17 21:36:46 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 2 | /* |
| 3 | * pulsedlight-lidar-lite-v2.c - Support for PulsedLight LIDAR sensor |
| 4 | * |
Matt Ranostay | d6ad805 | 2018-02-17 21:36:46 -0800 | [diff] [blame] | 5 | * Copyright (C) 2015, 2017-2018 |
| 6 | * Author: Matt Ranostay <matt.ranostay@konsulko.com> |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 7 | * |
Matt Ranostay | 4ac4e08 | 2015-11-15 17:20:05 -0800 | [diff] [blame] | 8 | * TODO: interrupt mode, and signal strength reporting |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/i2c.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/module.h> |
Matt Ranostay | 4ac4e08 | 2015-11-15 17:20:05 -0800 | [diff] [blame] | 16 | #include <linux/pm_runtime.h> |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 17 | #include <linux/iio/iio.h> |
| 18 | #include <linux/iio/sysfs.h> |
| 19 | #include <linux/iio/buffer.h> |
| 20 | #include <linux/iio/trigger.h> |
| 21 | #include <linux/iio/triggered_buffer.h> |
| 22 | #include <linux/iio/trigger_consumer.h> |
| 23 | |
| 24 | #define LIDAR_REG_CONTROL 0x00 |
| 25 | #define LIDAR_REG_CONTROL_ACQUIRE BIT(2) |
| 26 | |
| 27 | #define LIDAR_REG_STATUS 0x01 |
| 28 | #define LIDAR_REG_STATUS_INVALID BIT(3) |
| 29 | #define LIDAR_REG_STATUS_READY BIT(0) |
| 30 | |
Matt Ranostay | 366e656 | 2015-12-01 21:05:22 -0800 | [diff] [blame] | 31 | #define LIDAR_REG_DATA_HBYTE 0x0f |
| 32 | #define LIDAR_REG_DATA_LBYTE 0x10 |
| 33 | #define LIDAR_REG_DATA_WORD_READ BIT(7) |
| 34 | |
Matt Ranostay | 4ac4e08 | 2015-11-15 17:20:05 -0800 | [diff] [blame] | 35 | #define LIDAR_REG_PWR_CONTROL 0x65 |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 36 | |
| 37 | #define LIDAR_DRV_NAME "lidar" |
| 38 | |
| 39 | struct lidar_data { |
| 40 | struct iio_dev *indio_dev; |
| 41 | struct i2c_client *client; |
| 42 | |
Matt Ranostay | 366e656 | 2015-12-01 21:05:22 -0800 | [diff] [blame] | 43 | int (*xfer)(struct lidar_data *data, u8 reg, u8 *val, int len); |
| 44 | int i2c_enabled; |
| 45 | |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 46 | u16 buffer[8]; /* 2 byte distance + 8 byte timestamp */ |
| 47 | }; |
| 48 | |
| 49 | static const struct iio_chan_spec lidar_channels[] = { |
| 50 | { |
| 51 | .type = IIO_DISTANCE, |
| 52 | .info_mask_separate = |
| 53 | BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), |
| 54 | .scan_index = 0, |
| 55 | .scan_type = { |
| 56 | .sign = 'u', |
| 57 | .realbits = 16, |
| 58 | .storagebits = 16, |
| 59 | }, |
| 60 | }, |
| 61 | IIO_CHAN_SOFT_TIMESTAMP(1), |
| 62 | }; |
| 63 | |
Matt Ranostay | 366e656 | 2015-12-01 21:05:22 -0800 | [diff] [blame] | 64 | static int lidar_i2c_xfer(struct lidar_data *data, u8 reg, u8 *val, int len) |
| 65 | { |
| 66 | struct i2c_client *client = data->client; |
| 67 | struct i2c_msg msg[2]; |
| 68 | int ret; |
| 69 | |
| 70 | msg[0].addr = client->addr; |
| 71 | msg[0].flags = client->flags | I2C_M_STOP; |
| 72 | msg[0].len = 1; |
| 73 | msg[0].buf = (char *) ® |
| 74 | |
| 75 | msg[1].addr = client->addr; |
| 76 | msg[1].flags = client->flags | I2C_M_RD; |
| 77 | msg[1].len = len; |
| 78 | msg[1].buf = (char *) val; |
| 79 | |
| 80 | ret = i2c_transfer(client->adapter, msg, 2); |
| 81 | |
Matt Ranostay | 9979e32 | 2015-12-26 20:56:30 -0800 | [diff] [blame] | 82 | return (ret == 2) ? 0 : -EIO; |
Matt Ranostay | 366e656 | 2015-12-01 21:05:22 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static int lidar_smbus_xfer(struct lidar_data *data, u8 reg, u8 *val, int len) |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 86 | { |
| 87 | struct i2c_client *client = data->client; |
| 88 | int ret; |
| 89 | |
| 90 | /* |
| 91 | * Device needs a STOP condition between address write, and data read |
| 92 | * so in turn i2c_smbus_read_byte_data cannot be used |
| 93 | */ |
| 94 | |
Matt Ranostay | 366e656 | 2015-12-01 21:05:22 -0800 | [diff] [blame] | 95 | while (len--) { |
| 96 | ret = i2c_smbus_write_byte(client, reg++); |
| 97 | if (ret < 0) { |
| 98 | dev_err(&client->dev, "cannot write addr value"); |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | ret = i2c_smbus_read_byte(client); |
| 103 | if (ret < 0) { |
| 104 | dev_err(&client->dev, "cannot read data value"); |
| 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | *(val++) = ret; |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Matt Ranostay | 366e656 | 2015-12-01 21:05:22 -0800 | [diff] [blame] | 111 | return 0; |
| 112 | } |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 113 | |
Matt Ranostay | 366e656 | 2015-12-01 21:05:22 -0800 | [diff] [blame] | 114 | static int lidar_read_byte(struct lidar_data *data, u8 reg) |
| 115 | { |
| 116 | int ret; |
| 117 | u8 val; |
| 118 | |
| 119 | ret = data->xfer(data, reg, &val, 1); |
| 120 | if (ret < 0) |
| 121 | return ret; |
| 122 | |
| 123 | return val; |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static inline int lidar_write_control(struct lidar_data *data, int val) |
| 127 | { |
| 128 | return i2c_smbus_write_byte_data(data->client, LIDAR_REG_CONTROL, val); |
| 129 | } |
| 130 | |
Matt Ranostay | 4ac4e08 | 2015-11-15 17:20:05 -0800 | [diff] [blame] | 131 | static inline int lidar_write_power(struct lidar_data *data, int val) |
| 132 | { |
| 133 | return i2c_smbus_write_byte_data(data->client, |
| 134 | LIDAR_REG_PWR_CONTROL, val); |
| 135 | } |
| 136 | |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 137 | static int lidar_read_measurement(struct lidar_data *data, u16 *reg) |
| 138 | { |
Jonathan Cameron | d2080a8 | 2019-10-13 11:26:00 +0100 | [diff] [blame] | 139 | __be16 value; |
Matt Ranostay | 366e656 | 2015-12-01 21:05:22 -0800 | [diff] [blame] | 140 | int ret = data->xfer(data, LIDAR_REG_DATA_HBYTE | |
| 141 | (data->i2c_enabled ? LIDAR_REG_DATA_WORD_READ : 0), |
Jonathan Cameron | d2080a8 | 2019-10-13 11:26:00 +0100 | [diff] [blame] | 142 | (u8 *) &value, 2); |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 143 | |
Matt Ranostay | 366e656 | 2015-12-01 21:05:22 -0800 | [diff] [blame] | 144 | if (!ret) |
Jonathan Cameron | d2080a8 | 2019-10-13 11:26:00 +0100 | [diff] [blame] | 145 | *reg = be16_to_cpu(value); |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 146 | |
Matt Ranostay | 366e656 | 2015-12-01 21:05:22 -0800 | [diff] [blame] | 147 | return ret; |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static int lidar_get_measurement(struct lidar_data *data, u16 *reg) |
| 151 | { |
| 152 | struct i2c_client *client = data->client; |
| 153 | int tries = 10; |
| 154 | int ret; |
| 155 | |
Matt Ranostay | 4ac4e08 | 2015-11-15 17:20:05 -0800 | [diff] [blame] | 156 | pm_runtime_get_sync(&client->dev); |
| 157 | |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 158 | /* start sample */ |
| 159 | ret = lidar_write_control(data, LIDAR_REG_CONTROL_ACQUIRE); |
| 160 | if (ret < 0) { |
| 161 | dev_err(&client->dev, "cannot send start measurement command"); |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | while (tries--) { |
| 166 | usleep_range(1000, 2000); |
| 167 | |
| 168 | ret = lidar_read_byte(data, LIDAR_REG_STATUS); |
| 169 | if (ret < 0) |
| 170 | break; |
| 171 | |
Matt Ranostay | 45a6b82 | 2015-10-26 20:18:23 -0700 | [diff] [blame] | 172 | /* return -EINVAL since laser is likely pointed out of range */ |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 173 | if (ret & LIDAR_REG_STATUS_INVALID) { |
| 174 | *reg = 0; |
Matt Ranostay | 45a6b82 | 2015-10-26 20:18:23 -0700 | [diff] [blame] | 175 | ret = -EINVAL; |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 176 | break; |
| 177 | } |
| 178 | |
| 179 | /* sample ready to read */ |
| 180 | if (!(ret & LIDAR_REG_STATUS_READY)) { |
| 181 | ret = lidar_read_measurement(data, reg); |
| 182 | break; |
| 183 | } |
| 184 | ret = -EIO; |
| 185 | } |
Matt Ranostay | 4ac4e08 | 2015-11-15 17:20:05 -0800 | [diff] [blame] | 186 | pm_runtime_mark_last_busy(&client->dev); |
| 187 | pm_runtime_put_autosuspend(&client->dev); |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 188 | |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | static int lidar_read_raw(struct iio_dev *indio_dev, |
| 193 | struct iio_chan_spec const *chan, |
| 194 | int *val, int *val2, long mask) |
| 195 | { |
| 196 | struct lidar_data *data = iio_priv(indio_dev); |
| 197 | int ret = -EINVAL; |
| 198 | |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 199 | switch (mask) { |
| 200 | case IIO_CHAN_INFO_RAW: { |
| 201 | u16 reg; |
| 202 | |
Matt Ranostay | 9e18265 | 2016-06-07 21:18:04 -0700 | [diff] [blame] | 203 | if (iio_device_claim_direct_mode(indio_dev)) |
| 204 | return -EBUSY; |
| 205 | |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 206 | ret = lidar_get_measurement(data, ®); |
| 207 | if (!ret) { |
| 208 | *val = reg; |
| 209 | ret = IIO_VAL_INT; |
| 210 | } |
Matt Ranostay | 9e18265 | 2016-06-07 21:18:04 -0700 | [diff] [blame] | 211 | iio_device_release_direct_mode(indio_dev); |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 212 | break; |
| 213 | } |
| 214 | case IIO_CHAN_INFO_SCALE: |
| 215 | *val = 0; |
| 216 | *val2 = 10000; |
| 217 | ret = IIO_VAL_INT_PLUS_MICRO; |
| 218 | break; |
| 219 | } |
| 220 | |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | static irqreturn_t lidar_trigger_handler(int irq, void *private) |
| 225 | { |
| 226 | struct iio_poll_func *pf = private; |
| 227 | struct iio_dev *indio_dev = pf->indio_dev; |
| 228 | struct lidar_data *data = iio_priv(indio_dev); |
| 229 | int ret; |
| 230 | |
| 231 | ret = lidar_get_measurement(data, data->buffer); |
| 232 | if (!ret) { |
| 233 | iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 234 | iio_get_time_ns(indio_dev)); |
Matt Ranostay | 45a6b82 | 2015-10-26 20:18:23 -0700 | [diff] [blame] | 235 | } else if (ret != -EINVAL) { |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 236 | dev_err(&data->client->dev, "cannot read LIDAR measurement"); |
| 237 | } |
| 238 | |
| 239 | iio_trigger_notify_done(indio_dev->trig); |
| 240 | |
| 241 | return IRQ_HANDLED; |
| 242 | } |
| 243 | |
| 244 | static const struct iio_info lidar_info = { |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 245 | .read_raw = lidar_read_raw, |
| 246 | }; |
| 247 | |
| 248 | static int lidar_probe(struct i2c_client *client, |
| 249 | const struct i2c_device_id *id) |
| 250 | { |
| 251 | struct lidar_data *data; |
| 252 | struct iio_dev *indio_dev; |
| 253 | int ret; |
| 254 | |
| 255 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 256 | if (!indio_dev) |
| 257 | return -ENOMEM; |
Matt Ranostay | 366e656 | 2015-12-01 21:05:22 -0800 | [diff] [blame] | 258 | data = iio_priv(indio_dev); |
| 259 | |
| 260 | if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 261 | data->xfer = lidar_i2c_xfer; |
| 262 | data->i2c_enabled = 1; |
| 263 | } else if (i2c_check_functionality(client->adapter, |
| 264 | I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BYTE)) |
| 265 | data->xfer = lidar_smbus_xfer; |
| 266 | else |
Matt Ranostay | f8d9d3b | 2016-02-26 22:13:49 -0800 | [diff] [blame] | 267 | return -EOPNOTSUPP; |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 268 | |
| 269 | indio_dev->info = &lidar_info; |
| 270 | indio_dev->name = LIDAR_DRV_NAME; |
| 271 | indio_dev->channels = lidar_channels; |
| 272 | indio_dev->num_channels = ARRAY_SIZE(lidar_channels); |
Lars-Peter Clausen | b2027d1 | 2017-02-11 13:34:20 +0100 | [diff] [blame] | 273 | indio_dev->dev.parent = &client->dev; |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 274 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 275 | |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 276 | i2c_set_clientdata(client, indio_dev); |
| 277 | |
| 278 | data->client = client; |
| 279 | data->indio_dev = indio_dev; |
| 280 | |
| 281 | ret = iio_triggered_buffer_setup(indio_dev, NULL, |
| 282 | lidar_trigger_handler, NULL); |
| 283 | if (ret) |
| 284 | return ret; |
| 285 | |
| 286 | ret = iio_device_register(indio_dev); |
| 287 | if (ret) |
| 288 | goto error_unreg_buffer; |
| 289 | |
Matt Ranostay | 4ac4e08 | 2015-11-15 17:20:05 -0800 | [diff] [blame] | 290 | pm_runtime_set_autosuspend_delay(&client->dev, 1000); |
| 291 | pm_runtime_use_autosuspend(&client->dev); |
| 292 | |
| 293 | ret = pm_runtime_set_active(&client->dev); |
| 294 | if (ret) |
| 295 | goto error_unreg_buffer; |
| 296 | pm_runtime_enable(&client->dev); |
Matt Ranostay | 4ac4e08 | 2015-11-15 17:20:05 -0800 | [diff] [blame] | 297 | pm_runtime_idle(&client->dev); |
| 298 | |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 299 | return 0; |
| 300 | |
| 301 | error_unreg_buffer: |
| 302 | iio_triggered_buffer_cleanup(indio_dev); |
| 303 | |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | static int lidar_remove(struct i2c_client *client) |
| 308 | { |
| 309 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 310 | |
| 311 | iio_device_unregister(indio_dev); |
| 312 | iio_triggered_buffer_cleanup(indio_dev); |
| 313 | |
Matt Ranostay | 4ac4e08 | 2015-11-15 17:20:05 -0800 | [diff] [blame] | 314 | pm_runtime_disable(&client->dev); |
| 315 | pm_runtime_set_suspended(&client->dev); |
| 316 | |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static const struct i2c_device_id lidar_id[] = { |
| 321 | {"lidar-lite-v2", 0}, |
Matt Ranostay | b257c1a | 2016-12-28 21:13:06 -0800 | [diff] [blame] | 322 | {"lidar-lite-v3", 0}, |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 323 | { }, |
| 324 | }; |
| 325 | MODULE_DEVICE_TABLE(i2c, lidar_id); |
| 326 | |
| 327 | static const struct of_device_id lidar_dt_ids[] = { |
| 328 | { .compatible = "pulsedlight,lidar-lite-v2" }, |
Matt Ranostay | b257c1a | 2016-12-28 21:13:06 -0800 | [diff] [blame] | 329 | { .compatible = "grmn,lidar-lite-v3" }, |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 330 | { } |
| 331 | }; |
Matt Ranostay | 80cf2b5 | 2015-09-22 23:22:03 -0700 | [diff] [blame] | 332 | MODULE_DEVICE_TABLE(of, lidar_dt_ids); |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 333 | |
Matt Ranostay | 4ac4e08 | 2015-11-15 17:20:05 -0800 | [diff] [blame] | 334 | #ifdef CONFIG_PM |
| 335 | static int lidar_pm_runtime_suspend(struct device *dev) |
| 336 | { |
| 337 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 338 | struct lidar_data *data = iio_priv(indio_dev); |
| 339 | |
| 340 | return lidar_write_power(data, 0x0f); |
| 341 | } |
| 342 | |
| 343 | static int lidar_pm_runtime_resume(struct device *dev) |
| 344 | { |
| 345 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 346 | struct lidar_data *data = iio_priv(indio_dev); |
| 347 | int ret = lidar_write_power(data, 0); |
| 348 | |
| 349 | /* regulator and FPGA needs settling time */ |
| 350 | usleep_range(15000, 20000); |
| 351 | |
| 352 | return ret; |
| 353 | } |
| 354 | #endif |
| 355 | |
| 356 | static const struct dev_pm_ops lidar_pm_ops = { |
| 357 | SET_RUNTIME_PM_OPS(lidar_pm_runtime_suspend, |
| 358 | lidar_pm_runtime_resume, NULL) |
| 359 | }; |
| 360 | |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 361 | static struct i2c_driver lidar_driver = { |
| 362 | .driver = { |
| 363 | .name = LIDAR_DRV_NAME, |
| 364 | .of_match_table = of_match_ptr(lidar_dt_ids), |
Matt Ranostay | 4ac4e08 | 2015-11-15 17:20:05 -0800 | [diff] [blame] | 365 | .pm = &lidar_pm_ops, |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 366 | }, |
| 367 | .probe = lidar_probe, |
| 368 | .remove = lidar_remove, |
| 369 | .id_table = lidar_id, |
| 370 | }; |
| 371 | module_i2c_driver(lidar_driver); |
| 372 | |
Matt Ranostay | d6ad805 | 2018-02-17 21:36:46 -0800 | [diff] [blame] | 373 | MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>"); |
Matt Ranostay | cb119d5 | 2015-08-18 07:40:38 -0700 | [diff] [blame] | 374 | MODULE_DESCRIPTION("PulsedLight LIDAR sensor"); |
| 375 | MODULE_LICENSE("GPL"); |