]>
Commit | Line | Data |
---|---|---|
cb119d53 MR |
1 | /* |
2 | * pulsedlight-lidar-lite-v2.c - Support for PulsedLight LIDAR sensor | |
3 | * | |
4 | * Copyright (C) 2015 Matt Ranostay <mranostay@gmail.com> | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
4ac4e086 | 16 | * TODO: interrupt mode, and signal strength reporting |
cb119d53 MR |
17 | */ |
18 | ||
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> | |
4ac4e086 | 24 | #include <linux/pm_runtime.h> |
cb119d53 MR |
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> | |
31 | ||
32 | #define LIDAR_REG_CONTROL 0x00 | |
33 | #define LIDAR_REG_CONTROL_ACQUIRE BIT(2) | |
34 | ||
35 | #define LIDAR_REG_STATUS 0x01 | |
36 | #define LIDAR_REG_STATUS_INVALID BIT(3) | |
37 | #define LIDAR_REG_STATUS_READY BIT(0) | |
38 | ||
366e6563 MR |
39 | #define LIDAR_REG_DATA_HBYTE 0x0f |
40 | #define LIDAR_REG_DATA_LBYTE 0x10 | |
41 | #define LIDAR_REG_DATA_WORD_READ BIT(7) | |
42 | ||
4ac4e086 | 43 | #define LIDAR_REG_PWR_CONTROL 0x65 |
cb119d53 MR |
44 | |
45 | #define LIDAR_DRV_NAME "lidar" | |
46 | ||
47 | struct lidar_data { | |
48 | struct iio_dev *indio_dev; | |
49 | struct i2c_client *client; | |
50 | ||
366e6563 MR |
51 | int (*xfer)(struct lidar_data *data, u8 reg, u8 *val, int len); |
52 | int i2c_enabled; | |
53 | ||
cb119d53 MR |
54 | u16 buffer[8]; /* 2 byte distance + 8 byte timestamp */ |
55 | }; | |
56 | ||
57 | static const struct iio_chan_spec lidar_channels[] = { | |
58 | { | |
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, | |
67 | }, | |
68 | }, | |
69 | IIO_CHAN_SOFT_TIMESTAMP(1), | |
70 | }; | |
71 | ||
366e6563 MR |
72 | static int lidar_i2c_xfer(struct lidar_data *data, u8 reg, u8 *val, int len) |
73 | { | |
74 | struct i2c_client *client = data->client; | |
75 | struct i2c_msg msg[2]; | |
76 | int ret; | |
77 | ||
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 *) ® | |
82 | ||
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; | |
87 | ||
88 | ret = i2c_transfer(client->adapter, msg, 2); | |
89 | ||
9979e320 | 90 | return (ret == 2) ? 0 : -EIO; |
366e6563 MR |
91 | } |
92 | ||
93 | static int lidar_smbus_xfer(struct lidar_data *data, u8 reg, u8 *val, int len) | |
cb119d53 MR |
94 | { |
95 | struct i2c_client *client = data->client; | |
96 | int ret; | |
97 | ||
98 | /* | |
99 | * Device needs a STOP condition between address write, and data read | |
100 | * so in turn i2c_smbus_read_byte_data cannot be used | |
101 | */ | |
102 | ||
366e6563 MR |
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; | |
108 | } | |
109 | ||
110 | ret = i2c_smbus_read_byte(client); | |
111 | if (ret < 0) { | |
112 | dev_err(&client->dev, "cannot read data value"); | |
113 | return ret; | |
114 | } | |
115 | ||
116 | *(val++) = ret; | |
cb119d53 MR |
117 | } |
118 | ||
366e6563 MR |
119 | return 0; |
120 | } | |
121 | ||
122 | static int lidar_read_byte(struct lidar_data *data, u8 reg) | |
123 | { | |
124 | int ret; | |
125 | u8 val; | |
126 | ||
127 | ret = data->xfer(data, reg, &val, 1); | |
cb119d53 | 128 | if (ret < 0) |
366e6563 | 129 | return ret; |
cb119d53 | 130 | |
366e6563 | 131 | return val; |
cb119d53 MR |
132 | } |
133 | ||
134 | static inline int lidar_write_control(struct lidar_data *data, int val) | |
135 | { | |
136 | return i2c_smbus_write_byte_data(data->client, LIDAR_REG_CONTROL, val); | |
137 | } | |
138 | ||
4ac4e086 MR |
139 | static inline int lidar_write_power(struct lidar_data *data, int val) |
140 | { | |
141 | return i2c_smbus_write_byte_data(data->client, | |
142 | LIDAR_REG_PWR_CONTROL, val); | |
143 | } | |
144 | ||
cb119d53 MR |
145 | static int lidar_read_measurement(struct lidar_data *data, u16 *reg) |
146 | { | |
366e6563 MR |
147 | int ret = data->xfer(data, LIDAR_REG_DATA_HBYTE | |
148 | (data->i2c_enabled ? LIDAR_REG_DATA_WORD_READ : 0), | |
149 | (u8 *) reg, 2); | |
cb119d53 | 150 | |
366e6563 MR |
151 | if (!ret) |
152 | *reg = be16_to_cpu(*reg); | |
cb119d53 | 153 | |
366e6563 | 154 | return ret; |
cb119d53 MR |
155 | } |
156 | ||
157 | static int lidar_get_measurement(struct lidar_data *data, u16 *reg) | |
158 | { | |
159 | struct i2c_client *client = data->client; | |
160 | int tries = 10; | |
161 | int ret; | |
162 | ||
4ac4e086 MR |
163 | pm_runtime_get_sync(&client->dev); |
164 | ||
cb119d53 MR |
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; | |
170 | } | |
171 | ||
172 | while (tries--) { | |
173 | usleep_range(1000, 2000); | |
174 | ||
175 | ret = lidar_read_byte(data, LIDAR_REG_STATUS); | |
176 | if (ret < 0) | |
177 | break; | |
178 | ||
45a6b821 | 179 | /* return -EINVAL since laser is likely pointed out of range */ |
cb119d53 MR |
180 | if (ret & LIDAR_REG_STATUS_INVALID) { |
181 | *reg = 0; | |
45a6b821 | 182 | ret = -EINVAL; |
cb119d53 MR |
183 | break; |
184 | } | |
185 | ||
186 | /* sample ready to read */ | |
187 | if (!(ret & LIDAR_REG_STATUS_READY)) { | |
188 | ret = lidar_read_measurement(data, reg); | |
189 | break; | |
190 | } | |
191 | ret = -EIO; | |
192 | } | |
4ac4e086 MR |
193 | pm_runtime_mark_last_busy(&client->dev); |
194 | pm_runtime_put_autosuspend(&client->dev); | |
cb119d53 MR |
195 | |
196 | return ret; | |
197 | } | |
198 | ||
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) | |
202 | { | |
203 | struct lidar_data *data = iio_priv(indio_dev); | |
204 | int ret = -EINVAL; | |
205 | ||
cb119d53 MR |
206 | switch (mask) { |
207 | case IIO_CHAN_INFO_RAW: { | |
208 | u16 reg; | |
209 | ||
9e182654 MR |
210 | if (iio_device_claim_direct_mode(indio_dev)) |
211 | return -EBUSY; | |
212 | ||
cb119d53 MR |
213 | ret = lidar_get_measurement(data, ®); |
214 | if (!ret) { | |
215 | *val = reg; | |
216 | ret = IIO_VAL_INT; | |
217 | } | |
9e182654 | 218 | iio_device_release_direct_mode(indio_dev); |
cb119d53 MR |
219 | break; |
220 | } | |
221 | case IIO_CHAN_INFO_SCALE: | |
222 | *val = 0; | |
223 | *val2 = 10000; | |
224 | ret = IIO_VAL_INT_PLUS_MICRO; | |
225 | break; | |
226 | } | |
227 | ||
cb119d53 MR |
228 | return ret; |
229 | } | |
230 | ||
231 | static irqreturn_t lidar_trigger_handler(int irq, void *private) | |
232 | { | |
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; | |
237 | ||
238 | ret = lidar_get_measurement(data, data->buffer); | |
239 | if (!ret) { | |
240 | iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, | |
bc2b7dab | 241 | iio_get_time_ns(indio_dev)); |
45a6b821 | 242 | } else if (ret != -EINVAL) { |
cb119d53 MR |
243 | dev_err(&data->client->dev, "cannot read LIDAR measurement"); |
244 | } | |
245 | ||
246 | iio_trigger_notify_done(indio_dev->trig); | |
247 | ||
248 | return IRQ_HANDLED; | |
249 | } | |
250 | ||
251 | static const struct iio_info lidar_info = { | |
252 | .driver_module = THIS_MODULE, | |
253 | .read_raw = lidar_read_raw, | |
254 | }; | |
255 | ||
256 | static int lidar_probe(struct i2c_client *client, | |
257 | const struct i2c_device_id *id) | |
258 | { | |
259 | struct lidar_data *data; | |
260 | struct iio_dev *indio_dev; | |
261 | int ret; | |
262 | ||
263 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); | |
264 | if (!indio_dev) | |
265 | return -ENOMEM; | |
366e6563 MR |
266 | data = iio_priv(indio_dev); |
267 | ||
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 | |
f8d9d3b4 | 275 | return -EOPNOTSUPP; |
cb119d53 MR |
276 | |
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; | |
282 | ||
cb119d53 MR |
283 | i2c_set_clientdata(client, indio_dev); |
284 | ||
285 | data->client = client; | |
286 | data->indio_dev = indio_dev; | |
287 | ||
288 | ret = iio_triggered_buffer_setup(indio_dev, NULL, | |
289 | lidar_trigger_handler, NULL); | |
290 | if (ret) | |
291 | return ret; | |
292 | ||
293 | ret = iio_device_register(indio_dev); | |
294 | if (ret) | |
295 | goto error_unreg_buffer; | |
296 | ||
4ac4e086 MR |
297 | pm_runtime_set_autosuspend_delay(&client->dev, 1000); |
298 | pm_runtime_use_autosuspend(&client->dev); | |
299 | ||
300 | ret = pm_runtime_set_active(&client->dev); | |
301 | if (ret) | |
302 | goto error_unreg_buffer; | |
303 | pm_runtime_enable(&client->dev); | |
4ac4e086 MR |
304 | pm_runtime_idle(&client->dev); |
305 | ||
cb119d53 MR |
306 | return 0; |
307 | ||
308 | error_unreg_buffer: | |
309 | iio_triggered_buffer_cleanup(indio_dev); | |
310 | ||
311 | return ret; | |
312 | } | |
313 | ||
314 | static int lidar_remove(struct i2c_client *client) | |
315 | { | |
316 | struct iio_dev *indio_dev = i2c_get_clientdata(client); | |
317 | ||
318 | iio_device_unregister(indio_dev); | |
319 | iio_triggered_buffer_cleanup(indio_dev); | |
320 | ||
4ac4e086 MR |
321 | pm_runtime_disable(&client->dev); |
322 | pm_runtime_set_suspended(&client->dev); | |
323 | ||
cb119d53 MR |
324 | return 0; |
325 | } | |
326 | ||
327 | static const struct i2c_device_id lidar_id[] = { | |
328 | {"lidar-lite-v2", 0}, | |
329 | { }, | |
330 | }; | |
331 | MODULE_DEVICE_TABLE(i2c, lidar_id); | |
332 | ||
333 | static const struct of_device_id lidar_dt_ids[] = { | |
334 | { .compatible = "pulsedlight,lidar-lite-v2" }, | |
335 | { } | |
336 | }; | |
80cf2b5c | 337 | MODULE_DEVICE_TABLE(of, lidar_dt_ids); |
cb119d53 | 338 | |
4ac4e086 MR |
339 | #ifdef CONFIG_PM |
340 | static int lidar_pm_runtime_suspend(struct device *dev) | |
341 | { | |
342 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); | |
343 | struct lidar_data *data = iio_priv(indio_dev); | |
344 | ||
345 | return lidar_write_power(data, 0x0f); | |
346 | } | |
347 | ||
348 | static int lidar_pm_runtime_resume(struct device *dev) | |
349 | { | |
350 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); | |
351 | struct lidar_data *data = iio_priv(indio_dev); | |
352 | int ret = lidar_write_power(data, 0); | |
353 | ||
354 | /* regulator and FPGA needs settling time */ | |
355 | usleep_range(15000, 20000); | |
356 | ||
357 | return ret; | |
358 | } | |
359 | #endif | |
360 | ||
361 | static const struct dev_pm_ops lidar_pm_ops = { | |
362 | SET_RUNTIME_PM_OPS(lidar_pm_runtime_suspend, | |
363 | lidar_pm_runtime_resume, NULL) | |
364 | }; | |
365 | ||
cb119d53 MR |
366 | static struct i2c_driver lidar_driver = { |
367 | .driver = { | |
368 | .name = LIDAR_DRV_NAME, | |
369 | .of_match_table = of_match_ptr(lidar_dt_ids), | |
4ac4e086 | 370 | .pm = &lidar_pm_ops, |
cb119d53 MR |
371 | }, |
372 | .probe = lidar_probe, | |
373 | .remove = lidar_remove, | |
374 | .id_table = lidar_id, | |
375 | }; | |
376 | module_i2c_driver(lidar_driver); | |
377 | ||
378 | MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>"); | |
379 | MODULE_DESCRIPTION("PulsedLight LIDAR sensor"); | |
380 | MODULE_LICENSE("GPL"); |