srf04.c (664b0bae0b87f69bc9deb098f5e0158b9cf18e04) | srf04.c (bb208037ae45eb891765f0cff4198e9f58da7bd8) |
---|---|
1/* 2 * SRF04: ultrasonic sensor for distance measuring by using GPIOs 3 * 4 * Copyright (c) 2017 Andreas Klinger <ak@it-klinger.de> 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 unchanged lines hidden (view full) --- 18 * 19 * the measurement cycle as timing diagram looks like: 20 * 21 * +---+ 22 * GPIO | | 23 * trig: --+ +------------------------------------------------------ 24 * ^ ^ 25 * |<->| | 1/* 2 * SRF04: ultrasonic sensor for distance measuring by using GPIOs 3 * 4 * Copyright (c) 2017 Andreas Klinger <ak@it-klinger.de> 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 unchanged lines hidden (view full) --- 18 * 19 * the measurement cycle as timing diagram looks like: 20 * 21 * +---+ 22 * GPIO | | 23 * trig: --+ +------------------------------------------------------ 24 * ^ ^ 25 * |<->| |
26 * udelay(10) | 26 * udelay(trigger_pulse_us) |
27 * 28 * ultra +-+ +-+ +-+ 29 * sonic | | | | | | 30 * burst: ---------+ +-+ +-+ +----------------------------------------- 31 * . 32 * ultra . +-+ +-+ +-+ 33 * sonic . | | | | | | 34 * echo: ----------------------------------+ +-+ +-+ +---------------- --- 8 unchanged lines hidden (view full) --- 43 * pulse time measured 44 * --> one round trip of ultra sonic waves 45 */ 46#include <linux/err.h> 47#include <linux/gpio/consumer.h> 48#include <linux/kernel.h> 49#include <linux/module.h> 50#include <linux/of.h> | 27 * 28 * ultra +-+ +-+ +-+ 29 * sonic | | | | | | 30 * burst: ---------+ +-+ +-+ +----------------------------------------- 31 * . 32 * ultra . +-+ +-+ +-+ 33 * sonic . | | | | | | 34 * echo: ----------------------------------+ +-+ +-+ +---------------- --- 8 unchanged lines hidden (view full) --- 43 * pulse time measured 44 * --> one round trip of ultra sonic waves 45 */ 46#include <linux/err.h> 47#include <linux/gpio/consumer.h> 48#include <linux/kernel.h> 49#include <linux/module.h> 50#include <linux/of.h> |
51#include <linux/of_device.h> |
|
51#include <linux/platform_device.h> 52#include <linux/property.h> 53#include <linux/sched.h> 54#include <linux/interrupt.h> 55#include <linux/delay.h> 56#include <linux/iio/iio.h> 57#include <linux/iio/sysfs.h> 58 | 52#include <linux/platform_device.h> 53#include <linux/property.h> 54#include <linux/sched.h> 55#include <linux/interrupt.h> 56#include <linux/delay.h> 57#include <linux/iio/iio.h> 58#include <linux/iio/sysfs.h> 59 |
60struct srf04_cfg { 61 unsigned long trigger_pulse_us; 62}; 63 |
|
59struct srf04_data { 60 struct device *dev; 61 struct gpio_desc *gpiod_trig; 62 struct gpio_desc *gpiod_echo; 63 struct mutex lock; 64 int irqnr; 65 ktime_t ts_rising; 66 ktime_t ts_falling; 67 struct completion rising; 68 struct completion falling; | 64struct srf04_data { 65 struct device *dev; 66 struct gpio_desc *gpiod_trig; 67 struct gpio_desc *gpiod_echo; 68 struct mutex lock; 69 int irqnr; 70 ktime_t ts_rising; 71 ktime_t ts_falling; 72 struct completion rising; 73 struct completion falling; |
74 const struct srf04_cfg *cfg; |
|
69}; 70 | 75}; 76 |
77static const struct srf04_cfg srf04_cfg = { 78 .trigger_pulse_us = 10, 79}; 80 81static const struct srf04_cfg mb_lv_cfg = { 82 .trigger_pulse_us = 20, 83}; 84 |
|
71static irqreturn_t srf04_handle_irq(int irq, void *dev_id) 72{ 73 struct iio_dev *indio_dev = dev_id; 74 struct srf04_data *data = iio_priv(indio_dev); 75 ktime_t now = ktime_get(); 76 77 if (gpiod_get_value(data->gpiod_echo)) { 78 data->ts_rising = now; --- 18 unchanged lines hidden (view full) --- 97 * ==> lock against concurrent reading calls 98 */ 99 mutex_lock(&data->lock); 100 101 reinit_completion(&data->rising); 102 reinit_completion(&data->falling); 103 104 gpiod_set_value(data->gpiod_trig, 1); | 85static irqreturn_t srf04_handle_irq(int irq, void *dev_id) 86{ 87 struct iio_dev *indio_dev = dev_id; 88 struct srf04_data *data = iio_priv(indio_dev); 89 ktime_t now = ktime_get(); 90 91 if (gpiod_get_value(data->gpiod_echo)) { 92 data->ts_rising = now; --- 18 unchanged lines hidden (view full) --- 111 * ==> lock against concurrent reading calls 112 */ 113 mutex_lock(&data->lock); 114 115 reinit_completion(&data->rising); 116 reinit_completion(&data->falling); 117 118 gpiod_set_value(data->gpiod_trig, 1); |
105 udelay(10); | 119 udelay(data->cfg->trigger_pulse_us); |
106 gpiod_set_value(data->gpiod_trig, 0); 107 108 /* it cannot take more than 20 ms */ 109 ret = wait_for_completion_killable_timeout(&data->rising, HZ/50); 110 if (ret < 0) { 111 mutex_unlock(&data->lock); 112 return ret; 113 } else if (ret == 0) { --- 96 unchanged lines hidden (view full) --- 210 { 211 .type = IIO_DISTANCE, 212 .info_mask_separate = 213 BIT(IIO_CHAN_INFO_RAW) | 214 BIT(IIO_CHAN_INFO_SCALE), 215 }, 216}; 217 | 120 gpiod_set_value(data->gpiod_trig, 0); 121 122 /* it cannot take more than 20 ms */ 123 ret = wait_for_completion_killable_timeout(&data->rising, HZ/50); 124 if (ret < 0) { 125 mutex_unlock(&data->lock); 126 return ret; 127 } else if (ret == 0) { --- 96 unchanged lines hidden (view full) --- 224 { 225 .type = IIO_DISTANCE, 226 .info_mask_separate = 227 BIT(IIO_CHAN_INFO_RAW) | 228 BIT(IIO_CHAN_INFO_SCALE), 229 }, 230}; 231 |
232static const struct of_device_id of_srf04_match[] = { 233 { .compatible = "devantech,srf04", .data = &srf04_cfg}, 234 { .compatible = "maxbotix,mb1000", .data = &mb_lv_cfg}, 235 { .compatible = "maxbotix,mb1010", .data = &mb_lv_cfg}, 236 { .compatible = "maxbotix,mb1020", .data = &mb_lv_cfg}, 237 { .compatible = "maxbotix,mb1030", .data = &mb_lv_cfg}, 238 { .compatible = "maxbotix,mb1040", .data = &mb_lv_cfg}, 239 {}, 240}; 241 242MODULE_DEVICE_TABLE(of, of_srf04_match); 243 |
|
218static int srf04_probe(struct platform_device *pdev) 219{ 220 struct device *dev = &pdev->dev; 221 struct srf04_data *data; 222 struct iio_dev *indio_dev; 223 int ret; 224 225 indio_dev = devm_iio_device_alloc(dev, sizeof(struct srf04_data)); 226 if (!indio_dev) { 227 dev_err(dev, "failed to allocate IIO device\n"); 228 return -ENOMEM; 229 } 230 231 data = iio_priv(indio_dev); 232 data->dev = dev; | 244static int srf04_probe(struct platform_device *pdev) 245{ 246 struct device *dev = &pdev->dev; 247 struct srf04_data *data; 248 struct iio_dev *indio_dev; 249 int ret; 250 251 indio_dev = devm_iio_device_alloc(dev, sizeof(struct srf04_data)); 252 if (!indio_dev) { 253 dev_err(dev, "failed to allocate IIO device\n"); 254 return -ENOMEM; 255 } 256 257 data = iio_priv(indio_dev); 258 data->dev = dev; |
259 data->cfg = of_match_device(of_srf04_match, dev)->data; |
|
233 234 mutex_init(&data->lock); 235 init_completion(&data->rising); 236 init_completion(&data->falling); 237 238 data->gpiod_trig = devm_gpiod_get(dev, "trig", GPIOD_OUT_LOW); 239 if (IS_ERR(data->gpiod_trig)) { 240 dev_err(dev, "failed to get trig-gpios: err=%ld\n", --- 34 unchanged lines hidden (view full) --- 275 indio_dev->info = &srf04_iio_info; 276 indio_dev->modes = INDIO_DIRECT_MODE; 277 indio_dev->channels = srf04_chan_spec; 278 indio_dev->num_channels = ARRAY_SIZE(srf04_chan_spec); 279 280 return devm_iio_device_register(dev, indio_dev); 281} 282 | 260 261 mutex_init(&data->lock); 262 init_completion(&data->rising); 263 init_completion(&data->falling); 264 265 data->gpiod_trig = devm_gpiod_get(dev, "trig", GPIOD_OUT_LOW); 266 if (IS_ERR(data->gpiod_trig)) { 267 dev_err(dev, "failed to get trig-gpios: err=%ld\n", --- 34 unchanged lines hidden (view full) --- 302 indio_dev->info = &srf04_iio_info; 303 indio_dev->modes = INDIO_DIRECT_MODE; 304 indio_dev->channels = srf04_chan_spec; 305 indio_dev->num_channels = ARRAY_SIZE(srf04_chan_spec); 306 307 return devm_iio_device_register(dev, indio_dev); 308} 309 |
283static const struct of_device_id of_srf04_match[] = { 284 { .compatible = "devantech,srf04", }, 285 {}, 286}; 287 288MODULE_DEVICE_TABLE(of, of_srf04_match); 289 | |
290static struct platform_driver srf04_driver = { 291 .probe = srf04_probe, 292 .driver = { 293 .name = "srf04-gpio", 294 .of_match_table = of_srf04_match, 295 }, 296}; 297 298module_platform_driver(srf04_driver); 299 300MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>"); 301MODULE_DESCRIPTION("SRF04 ultrasonic sensor for distance measuring using GPIOs"); 302MODULE_LICENSE("GPL"); 303MODULE_ALIAS("platform:srf04"); | 310static struct platform_driver srf04_driver = { 311 .probe = srf04_probe, 312 .driver = { 313 .name = "srf04-gpio", 314 .of_match_table = of_srf04_match, 315 }, 316}; 317 318module_platform_driver(srf04_driver); 319 320MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>"); 321MODULE_DESCRIPTION("SRF04 ultrasonic sensor for distance measuring using GPIOs"); 322MODULE_LICENSE("GPL"); 323MODULE_ALIAS("platform:srf04"); |