hc
2024-08-12 233ab1bd4c5697f5cdec94e60206e8c6ac609b4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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 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 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");