Version:  2.0.40 2.2.26 2.4.37 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8

Linux/drivers/media/rc/ir-rx51.c

  1 /*
  2  *  Copyright (C) 2008 Nokia Corporation
  3  *
  4  *  Based on lirc_serial.c
  5  *
  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.
 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 #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>
 26 
 27 #include <media/lirc.h>
 28 #include <media/lirc_dev.h>
 29 #include <linux/platform_data/media/ir-rx51.h>
 30 
 31 #define LIRC_RX51_DRIVER_FEATURES (LIRC_CAN_SET_SEND_DUTY_CYCLE |       \
 32                                    LIRC_CAN_SET_SEND_CARRIER |          \
 33                                    LIRC_CAN_SEND_PULSE)
 34 
 35 #define DRIVER_NAME "lirc_rx51"
 36 
 37 #define WBUF_LEN 256
 38 
 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;
 45 
 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;
 51 };
 52 
 53 static inline void lirc_rx51_on(struct lirc_rx51 *lirc_rx51)
 54 {
 55         pwm_enable(lirc_rx51->pwm);
 56 }
 57 
 58 static inline void lirc_rx51_off(struct lirc_rx51 *lirc_rx51)
 59 {
 60         pwm_disable(lirc_rx51->pwm);
 61 }
 62 
 63 static int init_timing_params(struct lirc_rx51 *lirc_rx51)
 64 {
 65         struct pwm_device *pwm = lirc_rx51->pwm;
 66         int duty, period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, lirc_rx51->freq);
 67 
 68         duty = DIV_ROUND_CLOSEST(lirc_rx51->duty_cycle * period, 100);
 69 
 70         pwm_config(pwm, duty, period);
 71 
 72         return 0;
 73 }
 74 
 75 static enum hrtimer_restart lirc_rx51_timer_cb(struct hrtimer *timer)
 76 {
 77         struct lirc_rx51 *lirc_rx51 =
 78                         container_of(timer, struct lirc_rx51, timer);
 79         ktime_t now;
 80 
 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;
 86         }
 87 
 88         /*
 89          * If we happen to hit an odd latency spike, loop through the
 90          * pulses until we catch up.
 91          */
 92         do {
 93                 u64 ns;
 94 
 95                 if (lirc_rx51->wbuf_index >= WBUF_LEN)
 96                         goto end;
 97                 if (lirc_rx51->wbuf[lirc_rx51->wbuf_index] == -1)
 98                         goto end;
 99 
100                 if (lirc_rx51->wbuf_index % 2)
101                         lirc_rx51_off(lirc_rx51);
102                 else
103                         lirc_rx51_on(lirc_rx51);
104 
105                 ns = 1000 * lirc_rx51->wbuf[lirc_rx51->wbuf_index];
106                 hrtimer_add_expires_ns(timer, ns);
107 
108                 lirc_rx51->wbuf_index++;
109 
110                 now = timer->base->get_time();
111 
112         } while (hrtimer_get_expires_tv64(timer) < now.tv64);
113 
114         return HRTIMER_RESTART;
115 end:
116         /* Stop TX here */
117         lirc_rx51_off(lirc_rx51);
118         lirc_rx51->wbuf_index = -1;
119 
120         wake_up_interruptible(&lirc_rx51->wqueue);
121 
122         return HRTIMER_NORESTART;
123 }
124 
125 static ssize_t lirc_rx51_write(struct file *file, const char *buf,
126                           size_t n, loff_t *ppos)
127 {
128         int count, i;
129         struct lirc_rx51 *lirc_rx51 = file->private_data;
130 
131         if (n % sizeof(int))
132                 return -EINVAL;
133 
134         count = n / sizeof(int);
135         if ((count > WBUF_LEN) || (count % 2 == 0))
136                 return -EINVAL;
137 
138         /* Wait any pending transfers to finish */
139         wait_event_interruptible(lirc_rx51->wqueue, lirc_rx51->wbuf_index < 0);
140 
141         if (copy_from_user(lirc_rx51->wbuf, buf, n))
142                 return -EFAULT;
143 
144         /* Sanity check the input pulses */
145         for (i = 0; i < count; i++)
146                 if (lirc_rx51->wbuf[i] < 0)
147                         return -EINVAL;
148 
149         init_timing_params(lirc_rx51);
150         if (count < WBUF_LEN)
151                 lirc_rx51->wbuf[count] = -1; /* Insert termination mark */
152 
153         /*
154          * Adjust latency requirements so the device doesn't go in too
155          * deep sleep states
156          */
157         lirc_rx51->pdata->set_max_mpu_wakeup_lat(lirc_rx51->dev, 50);
158 
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);
164         /*
165          * Don't return back to the userspace until the transfer has
166          * finished
167          */
168         wait_event_interruptible(lirc_rx51->wqueue, lirc_rx51->wbuf_index < 0);
169 
170         /* We can sleep again */
171         lirc_rx51->pdata->set_max_mpu_wakeup_lat(lirc_rx51->dev, -1);
172 
173         return n;
174 }
175 
176 static long lirc_rx51_ioctl(struct file *filep,
177                         unsigned int cmd, unsigned long arg)
178 {
179         int result;
180         unsigned long value;
181         unsigned int ivalue;
182         struct lirc_rx51 *lirc_rx51 = filep->private_data;
183 
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;
190 
191         case LIRC_SET_SEND_MODE:
192                 result = get_user(value, (unsigned long *)arg);
193                 if (result)
194                         return result;
195 
196                 /* only LIRC_MODE_PULSE supported */
197                 if (value != LIRC_MODE_PULSE)
198                         return -ENOSYS;
199                 break;
200 
201         case LIRC_GET_REC_MODE:
202                 result = put_user(0, (unsigned long *) arg);
203                 if (result)
204                         return result;
205                 break;
206 
207         case LIRC_GET_LENGTH:
208                 return -ENOSYS;
209                 break;
210 
211         case LIRC_SET_SEND_DUTY_CYCLE:
212                 result = get_user(ivalue, (unsigned int *) arg);
213                 if (result)
214                         return result;
215 
216                 if (ivalue <= 0 || ivalue > 100) {
217                         dev_err(lirc_rx51->dev, ": invalid duty cycle %d\n",
218                                 ivalue);
219                         return -EINVAL;
220                 }
221 
222                 lirc_rx51->duty_cycle = ivalue;
223                 break;
224 
225         case LIRC_SET_SEND_CARRIER:
226                 result = get_user(ivalue, (unsigned int *) arg);
227                 if (result)
228                         return result;
229 
230                 if (ivalue > 500000 || ivalue < 20000) {
231                         dev_err(lirc_rx51->dev, ": invalid carrier freq %d\n",
232                                 ivalue);
233                         return -EINVAL;
234                 }
235 
236                 lirc_rx51->freq = ivalue;
237                 break;
238 
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;
245 
246         default:
247                 return -ENOIOCTLCMD;
248         }
249 
250         return 0;
251 }
252 
253 static int lirc_rx51_open(struct inode *inode, struct file *file)
254 {
255         struct lirc_rx51 *lirc_rx51 = lirc_get_pdata(file);
256         BUG_ON(!lirc_rx51);
257 
258         file->private_data = lirc_rx51;
259 
260         if (test_and_set_bit(1, &lirc_rx51->device_is_open))
261                 return -EBUSY;
262 
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);
266 
267                 dev_err(lirc_rx51->dev, "pwm_get failed: %d\n", res);
268                 return res;
269         }
270 
271         return 0;
272 }
273 
274 static int lirc_rx51_release(struct inode *inode, struct file *file)
275 {
276         struct lirc_rx51 *lirc_rx51 = file->private_data;
277 
278         hrtimer_cancel(&lirc_rx51->timer);
279         lirc_rx51_off(lirc_rx51);
280         pwm_put(lirc_rx51->pwm);
281 
282         clear_bit(1, &lirc_rx51->device_is_open);
283 
284         return 0;
285 }
286 
287 static struct lirc_rx51 lirc_rx51 = {
288         .duty_cycle     = 50,
289         .wbuf_index     = -1,
290 };
291 
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,
300 };
301 
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,
309 };
310 
311 #ifdef CONFIG_PM
312 
313 static int lirc_rx51_suspend(struct platform_device *dev, pm_message_t state)
314 {
315         /*
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.
322          */
323         if (test_and_set_bit(1, &lirc_rx51.device_is_open))
324                 return -EAGAIN;
325 
326         clear_bit(1, &lirc_rx51.device_is_open);
327 
328         return 0;
329 }
330 
331 static int lirc_rx51_resume(struct platform_device *dev)
332 {
333         return 0;
334 }
335 
336 #else
337 
338 #define lirc_rx51_suspend       NULL
339 #define lirc_rx51_resume        NULL
340 
341 #endif /* CONFIG_PM */
342 
343 static int lirc_rx51_probe(struct platform_device *dev)
344 {
345         struct pwm_device *pwm;
346 
347         lirc_rx51_driver.features = LIRC_RX51_DRIVER_FEATURES;
348         lirc_rx51.pdata = dev->dev.platform_data;
349 
350         if (!lirc_rx51.pdata) {
351                 dev_err(&dev->dev, "Platform Data is missing\n");
352                 return -ENXIO;
353         }
354 
355         pwm = pwm_get(&dev->dev, NULL);
356         if (IS_ERR(pwm)) {
357                 int err = PTR_ERR(pwm);
358 
359                 if (err != -EPROBE_DEFER)
360                         dev_err(&dev->dev, "pwm_get failed: %d\n", err);
361                 return err;
362         }
363 
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);
367 
368         hrtimer_init(&lirc_rx51.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
369         lirc_rx51.timer.function = lirc_rx51_timer_cb;
370 
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);
375 
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;
380         }
381 
382         return 0;
383 }
384 
385 static int lirc_rx51_remove(struct platform_device *dev)
386 {
387         return lirc_unregister_driver(lirc_rx51_driver.minor);
388 }
389 
390 static const struct of_device_id lirc_rx51_match[] = {
391         {
392                 .compatible = "nokia,n900-ir",
393         },
394         {},
395 };
396 MODULE_DEVICE_TABLE(of, lirc_rx51_match);
397 
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),
406         },
407 };
408 module_platform_driver(lirc_rx51_platform_driver);
409 
410 MODULE_DESCRIPTION("LIRC TX driver for Nokia RX51");
411 MODULE_AUTHOR("Nokia Corporation");
412 MODULE_LICENSE("GPL");
413 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us