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>
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 | \
35 #define DRIVER_NAME "lirc_rx51"
40 struct pwm_device
*pwm
;
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 */
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
);
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
);
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
);
89 * If we happen to hit an odd latency spike, loop through the
90 * pulses until we catch up.
95 if (lirc_rx51
->wbuf_index
>= WBUF_LEN
)
97 if (lirc_rx51
->wbuf
[lirc_rx51
->wbuf_index
] == -1)
100 if (lirc_rx51
->wbuf_index
% 2)
101 lirc_rx51_off(lirc_rx51
);
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
;
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
)
129 struct lirc_rx51
*lirc_rx51
= file
->private_data
;
134 count
= n
/ sizeof(int);
135 if ((count
> WBUF_LEN
) || (count
% 2 == 0))
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
))
144 /* Sanity check the input pulses */
145 for (i
= 0; i
< count
; i
++)
146 if (lirc_rx51
->wbuf
[i
] < 0)
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
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]),
165 * Don't return back to the userspace until the transfer has
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);
176 static long lirc_rx51_ioctl(struct file
*filep
,
177 unsigned int cmd
, unsigned long arg
)
182 struct lirc_rx51
*lirc_rx51
= filep
->private_data
;
185 case LIRC_GET_SEND_MODE
:
186 result
= put_user(LIRC_MODE_PULSE
, (unsigned long *)arg
);
191 case LIRC_SET_SEND_MODE
:
192 result
= get_user(value
, (unsigned long *)arg
);
196 /* only LIRC_MODE_PULSE supported */
197 if (value
!= LIRC_MODE_PULSE
)
201 case LIRC_GET_REC_MODE
:
202 result
= put_user(0, (unsigned long *) arg
);
207 case LIRC_GET_LENGTH
:
211 case LIRC_SET_SEND_DUTY_CYCLE
:
212 result
= get_user(ivalue
, (unsigned int *) arg
);
216 if (ivalue
<= 0 || ivalue
> 100) {
217 dev_err(lirc_rx51
->dev
, ": invalid duty cycle %d\n",
222 lirc_rx51
->duty_cycle
= ivalue
;
225 case LIRC_SET_SEND_CARRIER
:
226 result
= get_user(ivalue
, (unsigned int *) arg
);
230 if (ivalue
> 500000 || ivalue
< 20000) {
231 dev_err(lirc_rx51
->dev
, ": invalid carrier freq %d\n",
236 lirc_rx51
->freq
= ivalue
;
239 case LIRC_GET_FEATURES
:
240 result
= put_user(LIRC_RX51_DRIVER_FEATURES
,
241 (unsigned long *) arg
);
253 static int lirc_rx51_open(struct inode
*inode
, struct file
*file
)
255 struct lirc_rx51
*lirc_rx51
= lirc_get_pdata(file
);
258 file
->private_data
= lirc_rx51
;
260 if (test_and_set_bit(1, &lirc_rx51
->device_is_open
))
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
);
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
);
287 static struct lirc_rx51 lirc_rx51
= {
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
= {
308 .owner
= THIS_MODULE
,
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
))
326 clear_bit(1, &lirc_rx51
.device_is_open
);
331 static int lirc_rx51_resume(struct platform_device
*dev
)
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");
355 pwm
= pwm_get(&dev
->dev
, NULL
);
357 int err
= PTR_ERR(pwm
);
359 if (err
!= -EPROBE_DEFER
)
360 dev_err(&dev
->dev
, "pwm_get failed: %d\n", 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
);
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
;
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
,
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");