video: rockchip: hdmi: sync to develop-3.10
[firefly-linux-kernel-4.4.55.git] / drivers / video / rockchip / hdmi / rockchip-hdmi-cec.c
1 #include <linux/kernel.h>
2 #include <linux/slab.h>
3 #include <linux/workqueue.h>
4 #include <linux/delay.h>
5 #include <linux/module.h>
6 #include <linux/kernel.h>
7 #include <linux/errno.h>
8 #include <linux/string.h>
9 #include <linux/workqueue.h>
10 #include <linux/firmware.h>
11 #include "rockchip-hdmi-cec.h"
12 #include "linux/ioctl.h"
13 #include "linux/pagemap.h"
14
15 static struct cec_device *cec_dev;
16
17 static int cecreadframe(struct cec_framedata *frame)
18 {
19         if (frame == NULL || !cec_dev ||
20             cec_dev->readframe == NULL || !cec_dev->enable)
21                 return -1;
22         else
23                 return cec_dev->readframe(cec_dev->hdmi, frame);
24 }
25
26 static int cecsendframe(struct cec_framedata *frame)
27 {
28         if (frame == NULL || !cec_dev || cec_dev->readframe == NULL)
29                 return -1;
30         else
31                 return cec_dev->sendframe(cec_dev->hdmi, frame);
32 }
33
34 static void cecworkfunc(struct work_struct *work)
35 {
36         struct cec_delayed_work *cec_w =
37                 container_of(work, struct cec_delayed_work, work.work);
38         struct cecframelist *list_node;
39
40         switch (cec_w->event) {
41         case EVENT_ENUMERATE:
42                 break;
43         case EVENT_RX_FRAME:
44                 list_node = kmalloc(sizeof(*list_node), GFP_KERNEL);
45                 if (!list_node)
46                         return;
47                 cecreadframe(&list_node->cecframe);
48                 if (cec_dev->enable) {
49                         mutex_lock(&cec_dev->cec_lock);
50                         list_add_tail(&(list_node->framelist),
51                                       &cec_dev->ceclist);
52                         sysfs_notify(&cec_dev->device.this_device->kobj,
53                                      NULL, "stat");
54                         mutex_unlock(&cec_dev->cec_lock);
55                 } else {
56                         kfree(list_node);
57                 }
58                 break;
59         default:
60                 break;
61         }
62
63         kfree(cec_w->data);
64         kfree(cec_w);
65 }
66
67 void rockchip_hdmi_cec_submit_work(int event, int delay, void *data)
68 {
69         struct cec_delayed_work *work;
70
71         CECDBG("%s event %04x delay %d\n", __func__, event, delay);
72
73         if (!cec_dev)
74                 return;
75
76         work = kmalloc(sizeof(*work), GFP_ATOMIC);
77
78         if (work) {
79                 INIT_DELAYED_WORK(&work->work, cecworkfunc);
80                 work->event = event;
81                 work->data = data;
82                 queue_delayed_work(cec_dev->workqueue,
83                                    &work->work,
84                                    msecs_to_jiffies(delay));
85         } else {
86                 CECDBG(KERN_WARNING "CEC: Cannot allocate memory\n");
87         }
88 }
89
90 void rockchip_hdmi_cec_set_pa(int devpa)
91 {
92         struct list_head *pos, *n;
93
94         if (cec_dev) {
95                 cec_dev->address_phy = devpa;
96                 pr_info("%s %x\n", __func__, devpa);
97                 /*when hdmi hpd , ceclist will be reset*/
98                 mutex_lock(&cec_dev->cec_lock);
99                 if (!list_empty(&cec_dev->ceclist)) {
100                         list_for_each_safe(pos, n, &cec_dev->ceclist) {
101                                 list_del(pos);
102                                 kfree(pos);
103                         }
104                 }
105                 INIT_LIST_HEAD(&cec_dev->ceclist);
106                 sysfs_notify(&cec_dev->device.this_device->kobj, NULL, "stat");
107                 mutex_unlock(&cec_dev->cec_lock);
108         }
109 }
110
111 static ssize_t  cec_enable_show(struct device *dev,
112                                 struct device_attribute *attr, char *buf)
113 {
114         return snprintf(buf, PAGE_SIZE, "%d\n", cec_dev->enable);
115 }
116
117 static ssize_t cec_enable_store(struct device *dev,
118                                 struct device_attribute *attr,
119                                 const char *buf, size_t count)
120 {
121         int ret;
122
123         ret = kstrtoint(buf, 0, &(cec_dev->enable));
124         return count;
125 }
126
127 static ssize_t  cec_phy_show(struct device *dev,
128                              struct device_attribute *attr, char *buf)
129 {
130         return snprintf(buf, PAGE_SIZE, "0x%x\n", cec_dev->address_phy);
131 }
132
133 static ssize_t cec_phy_store(struct device *dev,
134                              struct device_attribute *attr,
135                          const char *buf, size_t count)
136 {
137         int ret;
138
139         ret = kstrtoint(buf, 0, &(cec_dev->address_phy));
140         return count;
141 }
142
143 static ssize_t  cec_logic_show(struct device *dev,
144                                struct device_attribute *attr, char *buf)
145 {
146         return snprintf(buf, PAGE_SIZE, "0x%02x\n", cec_dev->address_logic);
147 }
148
149 static ssize_t cec_logic_store(struct device *dev,
150                                struct device_attribute *attr,
151                                const char *buf, size_t count)
152 {
153         int ret;
154
155         ret = kstrtoint(buf, 0, &(cec_dev->address_logic));
156         return count;
157 }
158
159 static ssize_t  cec_state_show(struct device *dev,
160                                struct device_attribute *attr, char *buf)
161 {
162         int stat;
163
164         mutex_lock(&cec_dev->cec_lock);
165         if (!cec_dev->address_phy)
166                 stat = 0;
167         else if (list_empty(&cec_dev->ceclist))
168                 stat = 1;
169         else
170                 stat = 2;
171         mutex_unlock(&cec_dev->cec_lock);
172         return snprintf(buf, PAGE_SIZE, "%d\n", stat);
173 }
174
175 static struct device_attribute cec_attrs[] = {
176         __ATTR(logic, 0666, cec_logic_show, cec_logic_store),
177         __ATTR(phy, 0666, cec_phy_show, cec_phy_store),
178         __ATTR(enable, 0666, cec_enable_show, cec_enable_store),
179         __ATTR(stat, S_IRUGO, cec_state_show, NULL),
180 };
181
182 static long cec_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
183 {
184         int ret;
185         void __user *argp;
186         struct cec_framedata cecsendtemp;
187         struct cecframelist *listemp;
188
189         argp = (void __user *)arg;
190         switch (cmd) {
191         case HDMI_IOCTL_CECSETLA:
192                 ret = copy_from_user(&cec_dev->address_logic,
193                                      argp, sizeof(int));
194                 if (cec_dev->setceclogicaddr)
195                         cec_dev->setceclogicaddr(cec_dev->hdmi,
196                                                  cec_dev->address_logic);
197                 break;
198         case HDMI_IOCTL_CECSEND:
199                 ret = copy_from_user(&cecsendtemp, argp,
200                                      sizeof(struct cec_framedata));
201                 ret = cecsendframe(&cecsendtemp);
202                 cecsendtemp.returnval = ret;
203                 ret = copy_to_user(argp, &cecsendtemp,
204                                    sizeof(struct cec_framedata));
205                 break;
206         case HDMI_IOCTL_CECENAB:
207                 ret = copy_from_user(&cec_dev->enable, argp, sizeof(int));
208                 break;
209         case HDMI_IOCTL_CECPHY:
210                 ret = copy_to_user(argp, &(cec_dev->address_phy), sizeof(int));
211                 break;
212         case HDMI_IOCTL_CECLOGIC:
213                 ret = copy_to_user(argp, &(cec_dev->address_logic),
214                                    sizeof(int));
215                 break;
216         case HDMI_IOCTL_CECREAD:
217                 mutex_lock(&cec_dev->cec_lock);
218                 if (!list_empty(&cec_dev->ceclist)) {
219                         listemp = list_entry(cec_dev->ceclist.next,
220                                              struct cecframelist, framelist);
221                         ret = copy_to_user(argp, &listemp->cecframe,
222                                            sizeof(struct cec_framedata));
223                         list_del(&listemp->framelist);
224                         kfree(listemp);
225                 }
226                 mutex_unlock(&cec_dev->cec_lock);
227                 break;
228         case HDMI_IOCTL_CECCLEARLA:
229                 break;
230         case HDMI_IOCTL_CECWAKESTATE:
231                 ret = copy_to_user(argp, &(cec_dev->hdmi->sleep), sizeof(int));
232                 break;
233
234         default:
235                 break;
236         }
237         return 0;
238 }
239
240 static const struct file_operations cec_fops = {
241         .owner          = THIS_MODULE,
242         .compat_ioctl   = cec_ioctl,
243         .unlocked_ioctl = cec_ioctl,
244 };
245
246 int rockchip_hdmi_cec_init(struct hdmi *hdmi,
247                            int (*sendframe)(struct hdmi *,
248                                             struct cec_framedata *),
249                            int (*readframe)(struct hdmi *,
250                                             struct cec_framedata *),
251                            void (*setceclogicaddr)(struct hdmi *, int))
252 {
253         int ret, i;
254
255         cec_dev = kmalloc(sizeof(*cec_dev), GFP_KERNEL);
256         if (!cec_dev)
257                 return -ENOMEM;
258
259         memset(cec_dev, 0, sizeof(struct cec_device));
260         mutex_init(&cec_dev->cec_lock);
261         INIT_LIST_HEAD(&cec_dev->ceclist);
262         cec_dev->hdmi = hdmi;
263         cec_dev->enable = 1;
264         cec_dev->sendframe = sendframe;
265         cec_dev->readframe = readframe;
266         cec_dev->setceclogicaddr = setceclogicaddr;
267         cec_dev->workqueue = create_singlethread_workqueue("hdmi-cec");
268         if (cec_dev->workqueue == NULL) {
269                 pr_err("HDMI CEC: create workqueue failed.\n");
270                 return -1;
271         }
272         cec_dev->device.minor = MISC_DYNAMIC_MINOR;
273         cec_dev->device.name = "cec";
274         cec_dev->device.mode = 0666;
275         cec_dev->device.fops = &cec_fops;
276         if (misc_register(&cec_dev->device)) {
277                 pr_err("CEC: Could not add cec misc driver\n");
278                 goto error;
279         }
280         for (i = 0; i < ARRAY_SIZE(cec_attrs); i++) {
281                 ret = device_create_file(cec_dev->device.this_device,
282                                          &cec_attrs[i]);
283                 if (ret) {
284                         pr_err("CEC: Could not add sys file\n");
285                         goto error1;
286                 }
287         }
288         return 0;
289
290 error1:
291         misc_deregister(&cec_dev->device);
292 error:
293         return -EINVAL;
294 }