Input: xpad - add support for Xbox1 PDP Camo series gamepad
[linux/fpc-iii.git] / drivers / media / rc / ir-rx51.c
blob82fb6f2ca01194f3ad593059fa7c84d13481f590
1 /*
2 * Copyright (C) 2008 Nokia Corporation
4 * Based on lirc_serial.c
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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.
16 #include <linux/clk.h>
17 #include <linux/module.h>
18 #include <linux/interrupt.h>
19 #include <linux/uaccess.h>
20 #include <linux/platform_device.h>
21 #include <linux/sched.h>
22 #include <linux/wait.h>
23 #include <linux/pwm.h>
24 #include <linux/of.h>
25 #include <linux/hrtimer.h>
27 #include <media/lirc.h>
28 #include <media/lirc_dev.h>
29 #include <linux/platform_data/media/ir-rx51.h>
31 #define LIRC_RX51_DRIVER_FEATURES (LIRC_CAN_SET_SEND_DUTY_CYCLE | \
32 LIRC_CAN_SET_SEND_CARRIER | \
33 LIRC_CAN_SEND_PULSE)
35 #define DRIVER_NAME "lirc_rx51"
37 #define WBUF_LEN 256
39 struct lirc_rx51 {
40 struct pwm_device *pwm;
41 struct hrtimer timer;
42 struct device *dev;
43 struct lirc_rx51_platform_data *pdata;
44 wait_queue_head_t wqueue;
46 unsigned int freq; /* carrier frequency */
47 unsigned int duty_cycle; /* carrier duty cycle */
48 int wbuf[WBUF_LEN];
49 int wbuf_index;
50 unsigned long device_is_open;
53 static inline void lirc_rx51_on(struct lirc_rx51 *lirc_rx51)
55 pwm_enable(lirc_rx51->pwm);
58 static inline void lirc_rx51_off(struct lirc_rx51 *lirc_rx51)
60 pwm_disable(lirc_rx51->pwm);
63 static int init_timing_params(struct lirc_rx51 *lirc_rx51)
65 struct pwm_device *pwm = lirc_rx51->pwm;
66 int duty, period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, lirc_rx51->freq);
68 duty = DIV_ROUND_CLOSEST(lirc_rx51->duty_cycle * period, 100);
70 pwm_config(pwm, duty, period);
72 return 0;
75 static enum hrtimer_restart lirc_rx51_timer_cb(struct hrtimer *timer)
77 struct lirc_rx51 *lirc_rx51 =
78 container_of(timer, struct lirc_rx51, timer);
79 ktime_t now;
81 if (lirc_rx51->wbuf_index < 0) {
82 dev_err_ratelimited(lirc_rx51->dev,
83 "BUG wbuf_index has value of %i\n",
84 lirc_rx51->wbuf_index);
85 goto end;
89 * If we happen to hit an odd latency spike, loop through the
90 * pulses until we catch up.
92 do {
93 u64 ns;
95 if (lirc_rx51->wbuf_index >= WBUF_LEN)
96 goto end;
97 if (lirc_rx51->wbuf[lirc_rx51->wbuf_index] == -1)
98 goto end;
100 if (lirc_rx51->wbuf_index % 2)
101 lirc_rx51_off(lirc_rx51);
102 else
103 lirc_rx51_on(lirc_rx51);
105 ns = 1000 * lirc_rx51->wbuf[lirc_rx51->wbuf_index];
106 hrtimer_add_expires_ns(timer, ns);
108 lirc_rx51->wbuf_index++;
110 now = timer->base->get_time();
112 } while (hrtimer_get_expires_tv64(timer) < now.tv64);
114 return HRTIMER_RESTART;
115 end:
116 /* Stop TX here */
117 lirc_rx51_off(lirc_rx51);
118 lirc_rx51->wbuf_index = -1;
120 wake_up_interruptible(&lirc_rx51->wqueue);
122 return HRTIMER_NORESTART;
125 static ssize_t lirc_rx51_write(struct file *file, const char *buf,
126 size_t n, loff_t *ppos)
128 int count, i;
129 struct lirc_rx51 *lirc_rx51 = file->private_data;
131 if (n % sizeof(int))
132 return -EINVAL;
134 count = n / sizeof(int);
135 if ((count > WBUF_LEN) || (count % 2 == 0))
136 return -EINVAL;
138 /* Wait any pending transfers to finish */
139 wait_event_interruptible(lirc_rx51->wqueue, lirc_rx51->wbuf_index < 0);
141 if (copy_from_user(lirc_rx51->wbuf, buf, n))
142 return -EFAULT;
144 /* Sanity check the input pulses */
145 for (i = 0; i < count; i++)
146 if (lirc_rx51->wbuf[i] < 0)
147 return -EINVAL;
149 init_timing_params(lirc_rx51);
150 if (count < WBUF_LEN)
151 lirc_rx51->wbuf[count] = -1; /* Insert termination mark */
154 * Adjust latency requirements so the device doesn't go in too
155 * deep sleep states
157 lirc_rx51->pdata->set_max_mpu_wakeup_lat(lirc_rx51->dev, 50);
159 lirc_rx51_on(lirc_rx51);
160 lirc_rx51->wbuf_index = 1;
161 hrtimer_start(&lirc_rx51->timer,
162 ns_to_ktime(1000 * lirc_rx51->wbuf[0]),
163 HRTIMER_MODE_REL);
165 * Don't return back to the userspace until the transfer has
166 * finished
168 wait_event_interruptible(lirc_rx51->wqueue, lirc_rx51->wbuf_index < 0);
170 /* We can sleep again */
171 lirc_rx51->pdata->set_max_mpu_wakeup_lat(lirc_rx51->dev, -1);
173 return n;
176 static long lirc_rx51_ioctl(struct file *filep,
177 unsigned int cmd, unsigned long arg)
179 int result;
180 unsigned long value;
181 unsigned int ivalue;
182 struct lirc_rx51 *lirc_rx51 = filep->private_data;
184 switch (cmd) {
185 case LIRC_GET_SEND_MODE:
186 result = put_user(LIRC_MODE_PULSE, (unsigned long *)arg);
187 if (result)
188 return result;
189 break;
191 case LIRC_SET_SEND_MODE:
192 result = get_user(value, (unsigned long *)arg);
193 if (result)
194 return result;
196 /* only LIRC_MODE_PULSE supported */
197 if (value != LIRC_MODE_PULSE)
198 return -ENOSYS;
199 break;
201 case LIRC_GET_REC_MODE:
202 result = put_user(0, (unsigned long *) arg);
203 if (result)
204 return result;
205 break;
207 case LIRC_GET_LENGTH:
208 return -ENOSYS;
209 break;
211 case LIRC_SET_SEND_DUTY_CYCLE:
212 result = get_user(ivalue, (unsigned int *) arg);
213 if (result)
214 return result;
216 if (ivalue <= 0 || ivalue > 100) {
217 dev_err(lirc_rx51->dev, ": invalid duty cycle %d\n",
218 ivalue);
219 return -EINVAL;
222 lirc_rx51->duty_cycle = ivalue;
223 break;
225 case LIRC_SET_SEND_CARRIER:
226 result = get_user(ivalue, (unsigned int *) arg);
227 if (result)
228 return result;
230 if (ivalue > 500000 || ivalue < 20000) {
231 dev_err(lirc_rx51->dev, ": invalid carrier freq %d\n",
232 ivalue);
233 return -EINVAL;
236 lirc_rx51->freq = ivalue;
237 break;
239 case LIRC_GET_FEATURES:
240 result = put_user(LIRC_RX51_DRIVER_FEATURES,
241 (unsigned long *) arg);
242 if (result)
243 return result;
244 break;
246 default:
247 return -ENOIOCTLCMD;
250 return 0;
253 static int lirc_rx51_open(struct inode *inode, struct file *file)
255 struct lirc_rx51 *lirc_rx51 = lirc_get_pdata(file);
256 BUG_ON(!lirc_rx51);
258 file->private_data = lirc_rx51;
260 if (test_and_set_bit(1, &lirc_rx51->device_is_open))
261 return -EBUSY;
263 lirc_rx51->pwm = pwm_get(lirc_rx51->dev, NULL);
264 if (IS_ERR(lirc_rx51->pwm)) {
265 int res = PTR_ERR(lirc_rx51->pwm);
267 dev_err(lirc_rx51->dev, "pwm_get failed: %d\n", res);
268 return res;
271 return 0;
274 static int lirc_rx51_release(struct inode *inode, struct file *file)
276 struct lirc_rx51 *lirc_rx51 = file->private_data;
278 hrtimer_cancel(&lirc_rx51->timer);
279 lirc_rx51_off(lirc_rx51);
280 pwm_put(lirc_rx51->pwm);
282 clear_bit(1, &lirc_rx51->device_is_open);
284 return 0;
287 static struct lirc_rx51 lirc_rx51 = {
288 .duty_cycle = 50,
289 .wbuf_index = -1,
292 static const struct file_operations lirc_fops = {
293 .owner = THIS_MODULE,
294 .write = lirc_rx51_write,
295 .unlocked_ioctl = lirc_rx51_ioctl,
296 .read = lirc_dev_fop_read,
297 .poll = lirc_dev_fop_poll,
298 .open = lirc_rx51_open,
299 .release = lirc_rx51_release,
302 static struct lirc_driver lirc_rx51_driver = {
303 .name = DRIVER_NAME,
304 .minor = -1,
305 .code_length = 1,
306 .data = &lirc_rx51,
307 .fops = &lirc_fops,
308 .owner = THIS_MODULE,
311 #ifdef CONFIG_PM
313 static int lirc_rx51_suspend(struct platform_device *dev, pm_message_t state)
316 * In case the device is still open, do not suspend. Normally
317 * this should not be a problem as lircd only keeps the device
318 * open only for short periods of time. We also don't want to
319 * get involved with race conditions that might happen if we
320 * were in a middle of a transmit. Thus, we defer any suspend
321 * actions until transmit has completed.
323 if (test_and_set_bit(1, &lirc_rx51.device_is_open))
324 return -EAGAIN;
326 clear_bit(1, &lirc_rx51.device_is_open);
328 return 0;
331 static int lirc_rx51_resume(struct platform_device *dev)
333 return 0;
336 #else
338 #define lirc_rx51_suspend NULL
339 #define lirc_rx51_resume NULL
341 #endif /* CONFIG_PM */
343 static int lirc_rx51_probe(struct platform_device *dev)
345 struct pwm_device *pwm;
347 lirc_rx51_driver.features = LIRC_RX51_DRIVER_FEATURES;
348 lirc_rx51.pdata = dev->dev.platform_data;
350 if (!lirc_rx51.pdata) {
351 dev_err(&dev->dev, "Platform Data is missing\n");
352 return -ENXIO;
355 pwm = pwm_get(&dev->dev, NULL);
356 if (IS_ERR(pwm)) {
357 int err = PTR_ERR(pwm);
359 if (err != -EPROBE_DEFER)
360 dev_err(&dev->dev, "pwm_get failed: %d\n", err);
361 return err;
364 /* Use default, in case userspace does not set the carrier */
365 lirc_rx51.freq = DIV_ROUND_CLOSEST(pwm_get_period(pwm), NSEC_PER_SEC);
366 pwm_put(pwm);
368 hrtimer_init(&lirc_rx51.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
369 lirc_rx51.timer.function = lirc_rx51_timer_cb;
371 lirc_rx51.dev = &dev->dev;
372 lirc_rx51_driver.dev = &dev->dev;
373 lirc_rx51_driver.minor = lirc_register_driver(&lirc_rx51_driver);
374 init_waitqueue_head(&lirc_rx51.wqueue);
376 if (lirc_rx51_driver.minor < 0) {
377 dev_err(lirc_rx51.dev, ": lirc_register_driver failed: %d\n",
378 lirc_rx51_driver.minor);
379 return lirc_rx51_driver.minor;
382 return 0;
385 static int lirc_rx51_remove(struct platform_device *dev)
387 return lirc_unregister_driver(lirc_rx51_driver.minor);
390 static const struct of_device_id lirc_rx51_match[] = {
392 .compatible = "nokia,n900-ir",
396 MODULE_DEVICE_TABLE(of, lirc_rx51_match);
398 struct platform_driver lirc_rx51_platform_driver = {
399 .probe = lirc_rx51_probe,
400 .remove = lirc_rx51_remove,
401 .suspend = lirc_rx51_suspend,
402 .resume = lirc_rx51_resume,
403 .driver = {
404 .name = DRIVER_NAME,
405 .of_match_table = of_match_ptr(lirc_rx51_match),
408 module_platform_driver(lirc_rx51_platform_driver);
410 MODULE_DESCRIPTION("LIRC TX driver for Nokia RX51");
411 MODULE_AUTHOR("Nokia Corporation");
412 MODULE_LICENSE("GPL");