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"
15 static struct cec_device *cec_dev;
17 static int cecreadframe(struct cec_framedata *frame)
19 if (frame == NULL || !cec_dev ||
20 cec_dev->readframe == NULL || !cec_dev->enable)
23 return cec_dev->readframe(cec_dev->hdmi, frame);
26 static int cecsendframe(struct cec_framedata *frame)
28 if (frame == NULL || !cec_dev || cec_dev->readframe == NULL)
31 return cec_dev->sendframe(cec_dev->hdmi, frame);
34 static void cecworkfunc(struct work_struct *work)
36 struct cec_delayed_work *cec_w =
37 container_of(work, struct cec_delayed_work, work.work);
38 struct cecframelist *list_node;
40 switch (cec_w->event) {
44 list_node = kmalloc(sizeof(*list_node), GFP_KERNEL);
47 cecreadframe(&list_node->cecframe);
48 if (cec_dev->enable) {
49 mutex_lock(&cec_dev->cec_lock);
50 list_add_tail(&(list_node->framelist),
52 sysfs_notify(&cec_dev->device.this_device->kobj,
54 mutex_unlock(&cec_dev->cec_lock);
67 void rockchip_hdmi_cec_submit_work(int event, int delay, void *data)
69 struct cec_delayed_work *work;
71 CECDBG("%s event %04x delay %d\n", __func__, event, delay);
76 work = kmalloc(sizeof(*work), GFP_ATOMIC);
79 INIT_DELAYED_WORK(&work->work, cecworkfunc);
82 queue_delayed_work(cec_dev->workqueue,
84 msecs_to_jiffies(delay));
86 CECDBG(KERN_WARNING "CEC: Cannot allocate memory\n");
90 void rockchip_hdmi_cec_set_pa(int devpa)
92 struct list_head *pos, *n;
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) {
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);
111 static ssize_t cec_enable_show(struct device *dev,
112 struct device_attribute *attr, char *buf)
114 return snprintf(buf, PAGE_SIZE, "%d\n", cec_dev->enable);
117 static ssize_t cec_enable_store(struct device *dev,
118 struct device_attribute *attr,
119 const char *buf, size_t count)
123 ret = kstrtoint(buf, 0, &(cec_dev->enable));
127 static ssize_t cec_phy_show(struct device *dev,
128 struct device_attribute *attr, char *buf)
130 return snprintf(buf, PAGE_SIZE, "0x%x\n", cec_dev->address_phy);
133 static ssize_t cec_phy_store(struct device *dev,
134 struct device_attribute *attr,
135 const char *buf, size_t count)
139 ret = kstrtoint(buf, 0, &(cec_dev->address_phy));
143 static ssize_t cec_logic_show(struct device *dev,
144 struct device_attribute *attr, char *buf)
146 return snprintf(buf, PAGE_SIZE, "0x%02x\n", cec_dev->address_logic);
149 static ssize_t cec_logic_store(struct device *dev,
150 struct device_attribute *attr,
151 const char *buf, size_t count)
155 ret = kstrtoint(buf, 0, &(cec_dev->address_logic));
159 static ssize_t cec_state_show(struct device *dev,
160 struct device_attribute *attr, char *buf)
164 mutex_lock(&cec_dev->cec_lock);
165 if (!cec_dev->address_phy)
167 else if (list_empty(&cec_dev->ceclist))
171 mutex_unlock(&cec_dev->cec_lock);
172 return snprintf(buf, PAGE_SIZE, "%d\n", stat);
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),
182 static long cec_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
186 struct cec_framedata cecsendtemp;
187 struct cecframelist *listemp;
189 argp = (void __user *)arg;
191 case HDMI_IOCTL_CECSETLA:
192 ret = copy_from_user(&cec_dev->address_logic,
194 if (cec_dev->setceclogicaddr)
195 cec_dev->setceclogicaddr(cec_dev->hdmi,
196 cec_dev->address_logic);
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));
206 case HDMI_IOCTL_CECENAB:
207 ret = copy_from_user(&cec_dev->enable, argp, sizeof(int));
209 case HDMI_IOCTL_CECPHY:
210 ret = copy_to_user(argp, &(cec_dev->address_phy), sizeof(int));
212 case HDMI_IOCTL_CECLOGIC:
213 ret = copy_to_user(argp, &(cec_dev->address_logic),
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);
226 mutex_unlock(&cec_dev->cec_lock);
228 case HDMI_IOCTL_CECCLEARLA:
230 case HDMI_IOCTL_CECWAKESTATE:
231 ret = copy_to_user(argp, &(cec_dev->hdmi->sleep), sizeof(int));
240 static const struct file_operations cec_fops = {
241 .owner = THIS_MODULE,
242 .compat_ioctl = cec_ioctl,
243 .unlocked_ioctl = cec_ioctl,
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))
255 cec_dev = kmalloc(sizeof(*cec_dev), GFP_KERNEL);
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;
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");
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");
280 for (i = 0; i < ARRAY_SIZE(cec_attrs); i++) {
281 ret = device_create_file(cec_dev->device.this_device,
284 pr_err("CEC: Could not add sys file\n");
291 misc_deregister(&cec_dev->device);