misc: add Aschip PIR Sensor drivers
We can adjust the sensibility of the Aschip's PIR sensor via pulse gpio,
and the timing should be precise.
For example:
min: echo 4 > /proc/pir/sensibility
max: echo 100 > /proc/pir/sensibility
Signed-off-by: Ziyuan Xu <xzy.xu@rock-chips.com>
Change-Id: Id3e644fc38d9be24f97351df665e27fc4ff9d545
This commit is contained in:
@@ -528,6 +528,13 @@ config UID_SYS_STATS_DEBUG
|
||||
help
|
||||
Per TASK based io statistics exported to /proc/uid_io
|
||||
|
||||
config PIR_ASCHIP
|
||||
bool "Aschip PIR(Passive Infrared Sensor)"
|
||||
default n
|
||||
help
|
||||
Provides a driver to control the sensibility of the Aschip PIR detection
|
||||
sensor.
|
||||
|
||||
source "drivers/misc/c2port/Kconfig"
|
||||
source "drivers/misc/eeprom/Kconfig"
|
||||
source "drivers/misc/cb710/Kconfig"
|
||||
|
||||
@@ -59,3 +59,4 @@ obj-$(CONFIG_PCI_ENDPOINT_TEST) += pci_endpoint_test.o
|
||||
obj-$(CONFIG_OCXL) += ocxl/
|
||||
obj-$(CONFIG_MISC_RTSX) += cardreader/
|
||||
obj-$(CONFIG_UID_SYS_STATS) += uid_sys_stats.o
|
||||
obj-$(CONFIG_PIR_ASCHIP) += pir-aschip.o
|
||||
160
drivers/misc/pir-aschip.c
Normal file
160
drivers/misc/pir-aschip.c
Normal file
@@ -0,0 +1,160 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
/*
|
||||
* Copyright (c) 2020 Rockchip Electronics Co. Ltd.
|
||||
*
|
||||
*/
|
||||
#include <linux/delay.h>
|
||||
#include <linux/gpio/consumer.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/of_address.h>
|
||||
#include <linux/platform_device.h>
|
||||
#include <linux/seq_file.h>
|
||||
#include <linux/proc_fs.h>
|
||||
|
||||
struct aschip_pir_drv_data {
|
||||
struct device *dev;
|
||||
struct gpio_desc *pulse_gpio;
|
||||
struct proc_dir_entry *procfs;
|
||||
};
|
||||
|
||||
static int pir_aschip_set_sensibility(struct aschip_pir_drv_data *drv_data,
|
||||
uint32_t sensibility)
|
||||
{
|
||||
if (sensibility > 100 || sensibility < 4) {
|
||||
dev_err(drv_data->dev, "sensibilit should be [4, 100]\n");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
gpiod_set_value(drv_data->pulse_gpio, 1);
|
||||
usleep_range(1000, 2000);
|
||||
gpiod_set_value(drv_data->pulse_gpio, 0);
|
||||
usleep_range(50000, 51000);
|
||||
gpiod_set_value(drv_data->pulse_gpio, 1);
|
||||
usleep_range(sensibility * 1000, sensibility * 1000 + 1);
|
||||
gpiod_set_value(drv_data->pulse_gpio, 0);
|
||||
usleep_range(50000, 51000);
|
||||
gpiod_set_value(drv_data->pulse_gpio, 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_PROC_FS
|
||||
static int pir_aschip_sensibility_show(struct seq_file *s, void *v)
|
||||
{
|
||||
seq_puts(s, "set sensibility [4, 100] for PIR :\n");
|
||||
seq_puts(s, " echo [nr] > /proc/pir/sensibility\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int pir_aschip_sensibility_open(struct inode *inode, struct file *file)
|
||||
{
|
||||
return single_open(file, pir_aschip_sensibility_show, PDE_DATA(inode));
|
||||
}
|
||||
|
||||
static ssize_t pir_aschip_sensibility_write(struct file *filp,
|
||||
const char __user *buf,
|
||||
size_t cnt, loff_t *ppos)
|
||||
{
|
||||
struct aschip_pir_drv_data *drv_data = ((struct seq_file *)filp->private_data)->private;
|
||||
unsigned int val;
|
||||
int ret;
|
||||
|
||||
ret = kstrtouint_from_user(buf, cnt, 0, &val);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
ret = pir_aschip_set_sensibility(drv_data, val);
|
||||
return ret ? ret : cnt;
|
||||
}
|
||||
|
||||
static const struct file_operations pir_aschip_sensibility_fops = {
|
||||
.open = pir_aschip_sensibility_open,
|
||||
.read = seq_read,
|
||||
.llseek = seq_lseek,
|
||||
.release = single_release,
|
||||
.write = pir_aschip_sensibility_write,
|
||||
};
|
||||
|
||||
static int __init pir_aschip_create_procfs(struct aschip_pir_drv_data *drv_data)
|
||||
{
|
||||
struct proc_dir_entry *ent;
|
||||
|
||||
drv_data->procfs = proc_mkdir("pir", NULL);
|
||||
if (!drv_data->procfs)
|
||||
return -ENOMEM;
|
||||
|
||||
ent = proc_create_data("sensibility", 0644, drv_data->procfs,
|
||||
&pir_aschip_sensibility_fops, drv_data);
|
||||
if (!ent)
|
||||
goto fail;
|
||||
|
||||
return 0;
|
||||
fail:
|
||||
proc_remove(drv_data->procfs);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
static void __exit pir_aschip_proc_release(struct aschip_pir_drv_data *drv_data)
|
||||
{
|
||||
remove_proc_entry("sensibility", NULL);
|
||||
proc_remove(drv_data->procfs);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int pir_aschip_probe(struct platform_device *pdev)
|
||||
{
|
||||
struct aschip_pir_drv_data *drv_data;
|
||||
struct device *dev = &pdev->dev;
|
||||
|
||||
drv_data = devm_kzalloc(dev, sizeof(*drv_data), GFP_KERNEL);
|
||||
if (!drv_data)
|
||||
return -ENOMEM;
|
||||
|
||||
drv_data->pulse_gpio = devm_gpiod_get(dev, "pulse", GPIOD_OUT_HIGH);
|
||||
|
||||
if (IS_ERR(drv_data->pulse_gpio)) {
|
||||
dev_err(dev, "Failed to get pwdn-gpios\n");
|
||||
return PTR_ERR(drv_data->pulse_gpio);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_PROC_FS
|
||||
pir_aschip_create_procfs(drv_data);
|
||||
#endif
|
||||
drv_data->dev = dev;
|
||||
dev_set_drvdata(dev, drv_data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int pir_aschip_remove(struct platform_device *pdev)
|
||||
{
|
||||
#ifdef CONFIG_PROC_FS
|
||||
struct aschip_pir_drv_data *drv_data;
|
||||
|
||||
drv_data = dev_get_drvdata(&pdev->dev);
|
||||
pir_aschip_proc_release(drv_data);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct of_device_id pir_aschip_rockchip_match[] = {
|
||||
{ .compatible = "aschip,pir" },
|
||||
{},
|
||||
};
|
||||
|
||||
MODULE_DEVICE_TABLE(of, pir_aschip_rockchip_match);
|
||||
|
||||
static struct platform_driver pir_aschip_pltfm_driver = {
|
||||
.probe = pir_aschip_probe,
|
||||
.remove = pir_aschip_remove,
|
||||
.driver = {
|
||||
.name = "pir_aschip_aschip",
|
||||
.of_match_table = pir_aschip_rockchip_match,
|
||||
},
|
||||
};
|
||||
|
||||
module_platform_driver(pir_aschip_pltfm_driver);
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
MODULE_AUTHOR("Ziyuan Xu");
|
||||
MODULE_DESCRIPTION("Aschip PIR sensibility control driver");
|
||||
Reference in New Issue
Block a user