Commit | Line | Data |
---|---|---|
c332e847 TK |
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. | |
c332e847 | 15 | */ |
4406d52a | 16 | #include <linux/clk.h> |
c332e847 TK |
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> | |
3fdd1526 | 23 | #include <linux/pwm.h> |
b5406176 | 24 | #include <linux/of.h> |
79cdad36 | 25 | #include <linux/hrtimer.h> |
c332e847 | 26 | |
c332e847 TK |
27 | #include <media/lirc.h> |
28 | #include <media/lirc_dev.h> | |
eb4b0ec7 | 29 | #include <linux/platform_data/media/ir-rx51.h> |
c332e847 TK |
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 | ||
c332e847 | 39 | struct lirc_rx51 { |
3fdd1526 | 40 | struct pwm_device *pwm; |
79cdad36 | 41 | struct hrtimer timer; |
c332e847 TK |
42 | struct device *dev; |
43 | struct lirc_rx51_platform_data *pdata; | |
44 | wait_queue_head_t wqueue; | |
45 | ||
c332e847 TK |
46 | unsigned int freq; /* carrier frequency */ |
47 | unsigned int duty_cycle; /* carrier duty cycle */ | |
c332e847 TK |
48 | int wbuf[WBUF_LEN]; |
49 | int wbuf_index; | |
50 | unsigned long device_is_open; | |
c332e847 TK |
51 | }; |
52 | ||
79cdad36 | 53 | static inline void lirc_rx51_on(struct lirc_rx51 *lirc_rx51) |
c332e847 | 54 | { |
3fdd1526 | 55 | pwm_enable(lirc_rx51->pwm); |
c332e847 TK |
56 | } |
57 | ||
79cdad36 | 58 | static inline void lirc_rx51_off(struct lirc_rx51 *lirc_rx51) |
c332e847 | 59 | { |
3fdd1526 | 60 | pwm_disable(lirc_rx51->pwm); |
c332e847 TK |
61 | } |
62 | ||
63 | static int init_timing_params(struct lirc_rx51 *lirc_rx51) | |
64 | { | |
3fdd1526 ID |
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); | |
3fdd1526 ID |
69 | |
70 | pwm_config(pwm, duty, period); | |
71 | ||
c332e847 TK |
72 | return 0; |
73 | } | |
74 | ||
79cdad36 | 75 | static enum hrtimer_restart lirc_rx51_timer_cb(struct hrtimer *timer) |
c332e847 | 76 | { |
79cdad36 ID |
77 | struct lirc_rx51 *lirc_rx51 = |
78 | container_of(timer, struct lirc_rx51, timer); | |
79 | ktime_t now; | |
c332e847 | 80 | |
c332e847 TK |
81 | if (lirc_rx51->wbuf_index < 0) { |
82 | dev_err_ratelimited(lirc_rx51->dev, | |
79cdad36 | 83 | "BUG wbuf_index has value of %i\n", |
c332e847 TK |
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 { | |
79cdad36 ID |
93 | u64 ns; |
94 | ||
c332e847 TK |
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 | ||
79cdad36 ID |
105 | ns = 1000 * lirc_rx51->wbuf[lirc_rx51->wbuf_index]; |
106 | hrtimer_add_expires_ns(timer, ns); | |
107 | ||
c332e847 TK |
108 | lirc_rx51->wbuf_index++; |
109 | ||
79cdad36 ID |
110 | now = timer->base->get_time(); |
111 | ||
112 | } while (hrtimer_get_expires_tv64(timer) < now.tv64); | |
c332e847 | 113 | |
79cdad36 | 114 | return HRTIMER_RESTART; |
c332e847 TK |
115 | end: |
116 | /* Stop TX here */ | |
117 | lirc_rx51_off(lirc_rx51); | |
118 | lirc_rx51->wbuf_index = -1; | |
3fdd1526 | 119 | |
c332e847 TK |
120 | wake_up_interruptible(&lirc_rx51->wqueue); |
121 | ||
79cdad36 | 122 | return HRTIMER_NORESTART; |
c332e847 TK |
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; | |
79cdad36 ID |
161 | hrtimer_start(&lirc_rx51->timer, |
162 | ns_to_ktime(1000 * lirc_rx51->wbuf[0]), | |
163 | HRTIMER_MODE_REL); | |
c332e847 TK |
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 | ||
79cdad36 ID |
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; | |
c332e847 TK |
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 | ||
79cdad36 ID |
278 | hrtimer_cancel(&lirc_rx51->timer); |
279 | lirc_rx51_off(lirc_rx51); | |
280 | pwm_put(lirc_rx51->pwm); | |
c332e847 TK |
281 | |
282 | clear_bit(1, &lirc_rx51->device_is_open); | |
283 | ||
284 | return 0; | |
285 | } | |
286 | ||
287 | static struct lirc_rx51 lirc_rx51 = { | |
c332e847 TK |
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 | ||
4c62e976 | 343 | static int lirc_rx51_probe(struct platform_device *dev) |
c332e847 | 344 | { |
3fdd1526 ID |
345 | struct pwm_device *pwm; |
346 | ||
c332e847 TK |
347 | lirc_rx51_driver.features = LIRC_RX51_DRIVER_FEATURES; |
348 | lirc_rx51.pdata = dev->dev.platform_data; | |
3fdd1526 ID |
349 | |
350 | if (!lirc_rx51.pdata) { | |
351 | dev_err(&dev->dev, "Platform Data is missing\n"); | |
352 | return -ENXIO; | |
353 | } | |
354 | ||
3fdd1526 ID |
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 | ||
79cdad36 ID |
368 | hrtimer_init(&lirc_rx51.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
369 | lirc_rx51.timer.function = lirc_rx51_timer_cb; | |
370 | ||
c332e847 TK |
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 | } | |
c332e847 TK |
381 | |
382 | return 0; | |
383 | } | |
384 | ||
bf306900 | 385 | static int lirc_rx51_remove(struct platform_device *dev) |
c332e847 TK |
386 | { |
387 | return lirc_unregister_driver(lirc_rx51_driver.minor); | |
388 | } | |
389 | ||
b5406176 ID |
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 | ||
c332e847 TK |
398 | struct platform_driver lirc_rx51_platform_driver = { |
399 | .probe = lirc_rx51_probe, | |
bf306900 | 400 | .remove = lirc_rx51_remove, |
c332e847 TK |
401 | .suspend = lirc_rx51_suspend, |
402 | .resume = lirc_rx51_resume, | |
c332e847 TK |
403 | .driver = { |
404 | .name = DRIVER_NAME, | |
b5406176 | 405 | .of_match_table = of_match_ptr(lirc_rx51_match), |
c332e847 TK |
406 | }, |
407 | }; | |
304ce75d | 408 | module_platform_driver(lirc_rx51_platform_driver); |
c332e847 TK |
409 | |
410 | MODULE_DESCRIPTION("LIRC TX driver for Nokia RX51"); | |
411 | MODULE_AUTHOR("Nokia Corporation"); | |
412 | MODULE_LICENSE("GPL"); |