summaryrefslogtreecommitdiff
path: root/drivers/misc/usb_cam_gpio.c
blob: 15930c366e693a0df11c29fdc7209aa444f09814 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * drivers/misc/usb_cam_gpio.c
 *
 * Copyright (C) 2012-2016 Rockchip Co.,Ltd.
 * Author: Bin Yang <yangbin@rock-chips.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/miscdevice.h>
#include <linux/gpio.h>
#include <linux/uaccess.h>
#include <linux/atomic.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/freezer.h>
#include <linux/of_gpio.h>

static struct class *usb_cam_gpio_class;

struct usb_cam_gpio {
	struct device *dev;
	struct device sys_dev;

	struct gpio_desc *gpio_cam_hd;
	struct gpio_desc *gpio_cam_ir;
};

static ssize_t hd_camera_on_show(struct device *sys_dev,
				 struct device_attribute *attr,
				 char *buf)
{
	struct usb_cam_gpio *gpiod = container_of(sys_dev, struct usb_cam_gpio,
						  sys_dev);

	return sprintf(buf, "%d\n", gpiod_get_value(gpiod->gpio_cam_hd));
}

static ssize_t hd_camera_on_store(struct device *sys_dev,
				  struct device_attribute *attr,
				  const char *buf, size_t count)
{
	struct usb_cam_gpio *gpiod = container_of(sys_dev, struct usb_cam_gpio,
						  sys_dev);
	int val = 0;
	int ret = 0;

	ret = kstrtoint(buf, 0, &val);
	if (ret < 0)
		return ret;
	if (val)
		val = 1;
	gpiod_set_value(gpiod->gpio_cam_hd, val);

	return count;
}
static DEVICE_ATTR_RW(hd_camera_on);

static ssize_t ir_camera_on_show(struct device *sys_dev,
				 struct device_attribute *attr,
				 char *buf)
{
	struct usb_cam_gpio *gpiod = container_of(sys_dev,
						  struct usb_cam_gpio, sys_dev);

	return sprintf(buf, "%d\n", gpiod_get_value(gpiod->gpio_cam_ir));
}

static ssize_t ir_camera_on_store(struct device *sys_dev,
				  struct device_attribute *attr,
				  const char *buf, size_t count)
{
	struct usb_cam_gpio *gpiod = container_of(sys_dev,
						  struct usb_cam_gpio, sys_dev);
	int val;
	int ret;

	ret = kstrtoint(buf, 0, &val);
	if (ret < 0)
		return ret;
	if (val)
		val = 1;
	gpiod_set_value(gpiod->gpio_cam_ir, val);

	return count;
}
static DEVICE_ATTR_RW(ir_camera_on);

static struct attribute *usb_cam_gpio_attrs[] = {
	&dev_attr_hd_camera_on.attr,
	&dev_attr_ir_camera_on.attr,
	NULL,
};
ATTRIBUTE_GROUPS(usb_cam_gpio);

static int usb_cam_gpio_device_register(struct usb_cam_gpio *gpiod)
{
	int ret;
	struct device *dev = &gpiod->sys_dev;
	const char *name = {"usb_camera_on"};

	dev->class = usb_cam_gpio_class;
	dev_set_name(dev, "%s", name);
	dev_set_drvdata(dev, gpiod);
	ret = device_register(dev);

	return ret;
}

static int usb_cam_gpio_probe(struct platform_device *pdev)
{
	struct usb_cam_gpio *gpiod;
	int ret = 0;

	usb_cam_gpio_class = class_create(THIS_MODULE, "usb_cam_gpio");
	if (IS_ERR(usb_cam_gpio_class)) {
		pr_err("create uvc_detection class failed (%ld)\n",
		       PTR_ERR(usb_cam_gpio_class));
		return PTR_ERR(usb_cam_gpio_class);
	}
	usb_cam_gpio_class->dev_groups = usb_cam_gpio_groups;

	gpiod = devm_kzalloc(&pdev->dev, sizeof(*gpiod), GFP_KERNEL);
	if (!gpiod)
		return -ENOMEM;

	gpiod->dev = &pdev->dev;

	gpiod->gpio_cam_hd = devm_gpiod_get_optional(gpiod->dev,
						     "hd-cam", GPIOD_OUT_LOW);
	if (IS_ERR(gpiod->gpio_cam_hd)) {
		dev_warn(gpiod->dev, "Could not get hd-cam-gpios!\n");
		gpiod->gpio_cam_hd = NULL;
	}

	gpiod->gpio_cam_ir = devm_gpiod_get_optional(gpiod->dev,
						     "ir-cam", GPIOD_OUT_HIGH);
	if (IS_ERR(gpiod->gpio_cam_ir)) {
		dev_warn(gpiod->dev, "Could not get ir-cam-gpios!\n");
		gpiod->gpio_cam_ir = NULL;
	}

	ret = usb_cam_gpio_device_register(gpiod);
	if (ret < 0) {
		dev_err(gpiod->dev, "usb_cam_gpio device register fail\n");
		return ret;
	}

	dev_info(gpiod->dev, "usb_cam_gpio_probe success\n");

	return 0;
}

static const struct of_device_id usb_cam_gpio_match[] = {
	{ .compatible = "usb-cam-gpio" },
	{ /* Sentinel */ }
};

static int usb_cam_gpio_remove(struct platform_device *pdev)
{
	if (!IS_ERR(usb_cam_gpio_class))
		class_destroy(usb_cam_gpio_class);

	return 0;
}

static struct platform_driver usb_cam_gpio_driver = {
	.probe = usb_cam_gpio_probe,
	.remove = usb_cam_gpio_remove,
	.driver = {
		.name = "usb_cam_gpio",
		.owner = THIS_MODULE,
		.of_match_table	= usb_cam_gpio_match,
	},
};

module_platform_driver(usb_cam_gpio_driver);

MODULE_ALIAS("platform:usb_cam_gpio");
MODULE_AUTHOR("Bin Yang <yangbin@rock-chips.com>");
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("usb camera gpio driver");