2 * drivers/misc/usb_cam_gpio.c
4 * Copyright (C) 2012-2016 Rockchip Co.,Ltd.
5 * Author: Bin Yang <yangbin@rock-chips.com>
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
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.
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/miscdevice.h>
21 #include <linux/gpio.h>
22 #include <linux/uaccess.h>
23 #include <linux/atomic.h>
24 #include <linux/delay.h>
25 #include <linux/input.h>
26 #include <linux/freezer.h>
27 #include <linux/of_gpio.h>
29 static struct class *usb_cam_gpio_class;
33 struct device sys_dev;
35 struct gpio_desc *gpio_cam_hd;
36 struct gpio_desc *gpio_cam_ir;
39 static ssize_t hd_camera_on_show(struct device *sys_dev,
40 struct device_attribute *attr,
43 struct usb_cam_gpio *gpiod = container_of(sys_dev, struct usb_cam_gpio,
46 return sprintf(buf, "%d\n", gpiod_get_value(gpiod->gpio_cam_hd));
49 static ssize_t hd_camera_on_store(struct device *sys_dev,
50 struct device_attribute *attr,
51 const char *buf, size_t count)
53 struct usb_cam_gpio *gpiod = container_of(sys_dev, struct usb_cam_gpio,
58 ret = kstrtoint(buf, 0, &val);
63 gpiod_set_value(gpiod->gpio_cam_hd, val);
67 static DEVICE_ATTR_RW(hd_camera_on);
69 static ssize_t ir_camera_on_show(struct device *sys_dev,
70 struct device_attribute *attr,
73 struct usb_cam_gpio *gpiod = container_of(sys_dev,
74 struct usb_cam_gpio, sys_dev);
76 return sprintf(buf, "%d\n", gpiod_get_value(gpiod->gpio_cam_ir));
79 static ssize_t ir_camera_on_store(struct device *sys_dev,
80 struct device_attribute *attr,
81 const char *buf, size_t count)
83 struct usb_cam_gpio *gpiod = container_of(sys_dev,
84 struct usb_cam_gpio, sys_dev);
88 ret = kstrtoint(buf, 0, &val);
93 gpiod_set_value(gpiod->gpio_cam_ir, val);
97 static DEVICE_ATTR_RW(ir_camera_on);
99 static struct attribute *usb_cam_gpio_attrs[] = {
100 &dev_attr_hd_camera_on.attr,
101 &dev_attr_ir_camera_on.attr,
104 ATTRIBUTE_GROUPS(usb_cam_gpio);
106 static int usb_cam_gpio_device_register(struct usb_cam_gpio *gpiod)
109 struct device *dev = &gpiod->sys_dev;
110 const char *name = {"usb_camera_on"};
112 dev->class = usb_cam_gpio_class;
113 dev_set_name(dev, "%s", name);
114 dev_set_drvdata(dev, gpiod);
115 ret = device_register(dev);
120 static int usb_cam_gpio_probe(struct platform_device *pdev)
122 struct usb_cam_gpio *gpiod;
125 usb_cam_gpio_class = class_create(THIS_MODULE, "usb_cam_gpio");
126 if (IS_ERR(usb_cam_gpio_class)) {
127 pr_err("create uvc_detection class failed (%ld)\n",
128 PTR_ERR(usb_cam_gpio_class));
129 return PTR_ERR(usb_cam_gpio_class);
131 usb_cam_gpio_class->dev_groups = usb_cam_gpio_groups;
133 gpiod = devm_kzalloc(&pdev->dev, sizeof(*gpiod), GFP_KERNEL);
137 gpiod->dev = &pdev->dev;
139 gpiod->gpio_cam_hd = devm_gpiod_get_optional(gpiod->dev,
140 "hd-cam", GPIOD_OUT_LOW);
141 if (IS_ERR(gpiod->gpio_cam_hd)) {
142 dev_warn(gpiod->dev, "Could not get hd-cam-gpios!\n");
143 gpiod->gpio_cam_hd = NULL;
146 gpiod->gpio_cam_ir = devm_gpiod_get_optional(gpiod->dev,
147 "ir-cam", GPIOD_OUT_HIGH);
148 if (IS_ERR(gpiod->gpio_cam_ir)) {
149 dev_warn(gpiod->dev, "Could not get ir-cam-gpios!\n");
150 gpiod->gpio_cam_ir = NULL;
153 ret = usb_cam_gpio_device_register(gpiod);
155 dev_err(gpiod->dev, "usb_cam_gpio device register fail\n");
159 dev_info(gpiod->dev, "usb_cam_gpio_probe success\n");
164 static const struct of_device_id usb_cam_gpio_match[] = {
165 { .compatible = "usb-cam-gpio" },
169 static int usb_cam_gpio_remove(struct platform_device *pdev)
171 if (!IS_ERR(usb_cam_gpio_class))
172 class_destroy(usb_cam_gpio_class);
177 static struct platform_driver usb_cam_gpio_driver = {
178 .probe = usb_cam_gpio_probe,
179 .remove = usb_cam_gpio_remove,
181 .name = "usb_cam_gpio",
182 .owner = THIS_MODULE,
183 .of_match_table = usb_cam_gpio_match,
187 module_platform_driver(usb_cam_gpio_driver);
189 MODULE_ALIAS("platform:usb_cam_gpio");
190 MODULE_AUTHOR("Bin Yang <yangbin@rock-chips.com>");
191 MODULE_LICENSE("GPL v2");
192 MODULE_DESCRIPTION("usb camera gpio driver");