misc: add usb camera pd pin control driver
[firefly-linux-kernel-4.4.55.git] / drivers / misc / usb_cam_gpio.c
1 /*
2  * drivers/misc/usb_cam_gpio.c
3  *
4  * Copyright (C) 2012-2016 Rockchip Co.,Ltd.
5  * Author: Bin Yang <yangbin@rock-chips.com>
6  *
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.
10  *
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.
15  *
16  */
17
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>
28
29 static struct class *usb_cam_gpio_class;
30
31 struct usb_cam_gpio {
32         struct device *dev;
33         struct device sys_dev;
34
35         struct gpio_desc *gpio_cam_hd;
36         struct gpio_desc *gpio_cam_ir;
37 };
38
39 static ssize_t hd_camera_on_show(struct device *sys_dev,
40                                  struct device_attribute *attr,
41                                  char *buf)
42 {
43         struct usb_cam_gpio *gpiod = container_of(sys_dev, struct usb_cam_gpio,
44                                                   sys_dev);
45
46         return sprintf(buf, "%d\n", gpiod_get_value(gpiod->gpio_cam_hd));
47 }
48
49 static ssize_t hd_camera_on_store(struct device *sys_dev,
50                                   struct device_attribute *attr,
51                                   const char *buf, size_t count)
52 {
53         struct usb_cam_gpio *gpiod = container_of(sys_dev, struct usb_cam_gpio,
54                                                   sys_dev);
55         int val = 0;
56         int ret = 0;
57
58         ret = kstrtoint(buf, 0, &val);
59         if (ret < 0)
60                 return ret;
61         if (val)
62                 val = 1;
63         gpiod_set_value(gpiod->gpio_cam_hd, val);
64
65         return count;
66 }
67 static DEVICE_ATTR_RW(hd_camera_on);
68
69 static ssize_t ir_camera_on_show(struct device *sys_dev,
70                                  struct device_attribute *attr,
71                                  char *buf)
72 {
73         struct usb_cam_gpio *gpiod = container_of(sys_dev,
74                                                   struct usb_cam_gpio, sys_dev);
75
76         return sprintf(buf, "%d\n", gpiod_get_value(gpiod->gpio_cam_ir));
77 }
78
79 static ssize_t ir_camera_on_store(struct device *sys_dev,
80                                   struct device_attribute *attr,
81                                   const char *buf, size_t count)
82 {
83         struct usb_cam_gpio *gpiod = container_of(sys_dev,
84                                                   struct usb_cam_gpio, sys_dev);
85         int val;
86         int ret;
87
88         ret = kstrtoint(buf, 0, &val);
89         if (ret < 0)
90                 return ret;
91         if (val)
92                 val = 1;
93         gpiod_set_value(gpiod->gpio_cam_ir, val);
94
95         return count;
96 }
97 static DEVICE_ATTR_RW(ir_camera_on);
98
99 static struct attribute *usb_cam_gpio_attrs[] = {
100         &dev_attr_hd_camera_on.attr,
101         &dev_attr_ir_camera_on.attr,
102         NULL,
103 };
104 ATTRIBUTE_GROUPS(usb_cam_gpio);
105
106 static int usb_cam_gpio_device_register(struct usb_cam_gpio *gpiod)
107 {
108         int ret;
109         struct device *dev = &gpiod->sys_dev;
110         const char *name = {"usb_camera_on"};
111
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);
116
117         return ret;
118 }
119
120 static int usb_cam_gpio_probe(struct platform_device *pdev)
121 {
122         struct usb_cam_gpio *gpiod;
123         int ret = 0;
124
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);
130         }
131         usb_cam_gpio_class->dev_groups = usb_cam_gpio_groups;
132
133         gpiod = devm_kzalloc(&pdev->dev, sizeof(*gpiod), GFP_KERNEL);
134         if (!gpiod)
135                 return -ENOMEM;
136
137         gpiod->dev = &pdev->dev;
138
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;
144         }
145
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;
151         }
152
153         ret = usb_cam_gpio_device_register(gpiod);
154         if (ret < 0) {
155                 dev_err(gpiod->dev, "usb_cam_gpio device register fail\n");
156                 return ret;
157         }
158
159         dev_info(gpiod->dev, "usb_cam_gpio_probe success\n");
160
161         return 0;
162 }
163
164 static const struct of_device_id usb_cam_gpio_match[] = {
165         { .compatible = "usb-cam-gpio" },
166         { /* Sentinel */ }
167 };
168
169 static int usb_cam_gpio_remove(struct platform_device *pdev)
170 {
171         if (!IS_ERR(usb_cam_gpio_class))
172                 class_destroy(usb_cam_gpio_class);
173
174         return 0;
175 }
176
177 static struct platform_driver usb_cam_gpio_driver = {
178         .probe = usb_cam_gpio_probe,
179         .remove = usb_cam_gpio_remove,
180         .driver = {
181                 .name = "usb_cam_gpio",
182                 .owner = THIS_MODULE,
183                 .of_match_table = usb_cam_gpio_match,
184         },
185 };
186
187 module_platform_driver(usb_cam_gpio_driver);
188
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");