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/platform_device.h> 19 #include <linux/wait.h> 20 #include <linux/pwm.h> 21 #include <linux/of.h> 22 #include <linux/hrtimer.h> 23 24 #include <media/rc-core.h> 25 #include <linux/platform_data/media/ir-rx51.h> 26 27 #define WBUF_LEN 256 28 29 struct ir_rx51 { 30 struct rc_dev *rcdev; 31 struct pwm_device *pwm; 32 struct hrtimer timer; 33 struct device *dev; 34 struct ir_rx51_platform_data *pdata; 35 wait_queue_head_t wqueue; 36 37 unsigned int freq; /* carrier frequency */ 38 unsigned int duty_cycle; /* carrier duty cycle */ 39 int wbuf[WBUF_LEN]; 40 int wbuf_index; 41 unsigned long device_is_open; 42 }; 43 44 static inline void ir_rx51_on(struct ir_rx51 *ir_rx51) 45 { 46 pwm_enable(ir_rx51->pwm); 47 } 48 49 static inline void ir_rx51_off(struct ir_rx51 *ir_rx51) 50 { 51 pwm_disable(ir_rx51->pwm); 52 } 53 54 static int init_timing_params(struct ir_rx51 *ir_rx51) 55 { 56 struct pwm_device *pwm = ir_rx51->pwm; 57 int duty, period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, ir_rx51->freq); 58 59 duty = DIV_ROUND_CLOSEST(ir_rx51->duty_cycle * period, 100); 60 61 pwm_config(pwm, duty, period); 62 63 return 0; 64 } 65 66 static enum hrtimer_restart ir_rx51_timer_cb(struct hrtimer *timer) 67 { 68 struct ir_rx51 *ir_rx51 = container_of(timer, struct ir_rx51, timer); 69 ktime_t now; 70 71 if (ir_rx51->wbuf_index < 0) { 72 dev_err_ratelimited(ir_rx51->dev, 73 "BUG wbuf_index has value of %i\n", 74 ir_rx51->wbuf_index); 75 goto end; 76 } 77 78 /* 79 * If we happen to hit an odd latency spike, loop through the 80 * pulses until we catch up. 81 */ 82 do { 83 u64 ns; 84 85 if (ir_rx51->wbuf_index >= WBUF_LEN) 86 goto end; 87 if (ir_rx51->wbuf[ir_rx51->wbuf_index] == -1) 88 goto end; 89 90 if (ir_rx51->wbuf_index % 2) 91 ir_rx51_off(ir_rx51); 92 else 93 ir_rx51_on(ir_rx51); 94 95 ns = US_TO_NS(ir_rx51->wbuf[ir_rx51->wbuf_index]); 96 hrtimer_add_expires_ns(timer, ns); 97 98 ir_rx51->wbuf_index++; 99 100 now = timer->base->get_time(); 101 102 } while (hrtimer_get_expires_tv64(timer) < now); 103 104 return HRTIMER_RESTART; 105 end: 106 /* Stop TX here */ 107 ir_rx51_off(ir_rx51); 108 ir_rx51->wbuf_index = -1; 109 110 wake_up_interruptible(&ir_rx51->wqueue); 111 112 return HRTIMER_NORESTART; 113 } 114 115 static int ir_rx51_tx(struct rc_dev *dev, unsigned int *buffer, 116 unsigned int count) 117 { 118 struct ir_rx51 *ir_rx51 = dev->priv; 119 120 if (count > WBUF_LEN) 121 return -EINVAL; 122 123 memcpy(ir_rx51->wbuf, buffer, count * sizeof(unsigned int)); 124 125 /* Wait any pending transfers to finish */ 126 wait_event_interruptible(ir_rx51->wqueue, ir_rx51->wbuf_index < 0); 127 128 init_timing_params(ir_rx51); 129 if (count < WBUF_LEN) 130 ir_rx51->wbuf[count] = -1; /* Insert termination mark */ 131 132 /* 133 * Adjust latency requirements so the device doesn't go in too 134 * deep sleep states 135 */ 136 ir_rx51->pdata->set_max_mpu_wakeup_lat(ir_rx51->dev, 50); 137 138 ir_rx51_on(ir_rx51); 139 ir_rx51->wbuf_index = 1; 140 hrtimer_start(&ir_rx51->timer, 141 ns_to_ktime(US_TO_NS(ir_rx51->wbuf[0])), 142 HRTIMER_MODE_REL); 143 /* 144 * Don't return back to the userspace until the transfer has 145 * finished 146 */ 147 wait_event_interruptible(ir_rx51->wqueue, ir_rx51->wbuf_index < 0); 148 149 /* We can sleep again */ 150 ir_rx51->pdata->set_max_mpu_wakeup_lat(ir_rx51->dev, -1); 151 152 return count; 153 } 154 155 static int ir_rx51_open(struct rc_dev *dev) 156 { 157 struct ir_rx51 *ir_rx51 = dev->priv; 158 159 if (test_and_set_bit(1, &ir_rx51->device_is_open)) 160 return -EBUSY; 161 162 ir_rx51->pwm = pwm_get(ir_rx51->dev, NULL); 163 if (IS_ERR(ir_rx51->pwm)) { 164 int res = PTR_ERR(ir_rx51->pwm); 165 166 dev_err(ir_rx51->dev, "pwm_get failed: %d\n", res); 167 return res; 168 } 169 170 return 0; 171 } 172 173 static void ir_rx51_release(struct rc_dev *dev) 174 { 175 struct ir_rx51 *ir_rx51 = dev->priv; 176 177 hrtimer_cancel(&ir_rx51->timer); 178 ir_rx51_off(ir_rx51); 179 pwm_put(ir_rx51->pwm); 180 181 clear_bit(1, &ir_rx51->device_is_open); 182 } 183 184 static struct ir_rx51 ir_rx51 = { 185 .duty_cycle = 50, 186 .wbuf_index = -1, 187 }; 188 189 static int ir_rx51_set_duty_cycle(struct rc_dev *dev, u32 duty) 190 { 191 struct ir_rx51 *ir_rx51 = dev->priv; 192 193 ir_rx51->duty_cycle = duty; 194 195 return 0; 196 } 197 198 static int ir_rx51_set_tx_carrier(struct rc_dev *dev, u32 carrier) 199 { 200 struct ir_rx51 *ir_rx51 = dev->priv; 201 202 if (carrier > 500000 || carrier < 20000) 203 return -EINVAL; 204 205 ir_rx51->freq = carrier; 206 207 return 0; 208 } 209 210 #ifdef CONFIG_PM 211 212 static int ir_rx51_suspend(struct platform_device *dev, pm_message_t state) 213 { 214 /* 215 * In case the device is still open, do not suspend. Normally 216 * this should not be a problem as lircd only keeps the device 217 * open only for short periods of time. We also don't want to 218 * get involved with race conditions that might happen if we 219 * were in a middle of a transmit. Thus, we defer any suspend 220 * actions until transmit has completed. 221 */ 222 if (test_and_set_bit(1, &ir_rx51.device_is_open)) 223 return -EAGAIN; 224 225 clear_bit(1, &ir_rx51.device_is_open); 226 227 return 0; 228 } 229 230 static int ir_rx51_resume(struct platform_device *dev) 231 { 232 return 0; 233 } 234 235 #else 236 237 #define ir_rx51_suspend NULL 238 #define ir_rx51_resume NULL 239 240 #endif /* CONFIG_PM */ 241 242 static int ir_rx51_probe(struct platform_device *dev) 243 { 244 struct pwm_device *pwm; 245 struct rc_dev *rcdev; 246 247 ir_rx51.pdata = dev->dev.platform_data; 248 249 if (!ir_rx51.pdata) { 250 dev_err(&dev->dev, "Platform Data is missing\n"); 251 return -ENXIO; 252 } 253 254 pwm = pwm_get(&dev->dev, NULL); 255 if (IS_ERR(pwm)) { 256 int err = PTR_ERR(pwm); 257 258 if (err != -EPROBE_DEFER) 259 dev_err(&dev->dev, "pwm_get failed: %d\n", err); 260 return err; 261 } 262 263 /* Use default, in case userspace does not set the carrier */ 264 ir_rx51.freq = DIV_ROUND_CLOSEST(pwm_get_period(pwm), NSEC_PER_SEC); 265 pwm_put(pwm); 266 267 hrtimer_init(&ir_rx51.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 268 ir_rx51.timer.function = ir_rx51_timer_cb; 269 270 ir_rx51.dev = &dev->dev; 271 272 rcdev = devm_rc_allocate_device(&dev->dev, RC_DRIVER_IR_RAW_TX); 273 if (!rcdev) 274 return -ENOMEM; 275 276 rcdev->priv = &ir_rx51; 277 rcdev->open = ir_rx51_open; 278 rcdev->close = ir_rx51_release; 279 rcdev->tx_ir = ir_rx51_tx; 280 rcdev->s_tx_duty_cycle = ir_rx51_set_duty_cycle; 281 rcdev->s_tx_carrier = ir_rx51_set_tx_carrier; 282 rcdev->driver_name = KBUILD_MODNAME; 283 284 ir_rx51.rcdev = rcdev; 285 286 return devm_rc_register_device(&dev->dev, ir_rx51.rcdev); 287 } 288 289 static int ir_rx51_remove(struct platform_device *dev) 290 { 291 return 0; 292 } 293 294 static const struct of_device_id ir_rx51_match[] = { 295 { 296 .compatible = "nokia,n900-ir", 297 }, 298 {}, 299 }; 300 MODULE_DEVICE_TABLE(of, ir_rx51_match); 301 302 static struct platform_driver ir_rx51_platform_driver = { 303 .probe = ir_rx51_probe, 304 .remove = ir_rx51_remove, 305 .suspend = ir_rx51_suspend, 306 .resume = ir_rx51_resume, 307 .driver = { 308 .name = KBUILD_MODNAME, 309 .of_match_table = of_match_ptr(ir_rx51_match), 310 }, 311 }; 312 module_platform_driver(ir_rx51_platform_driver); 313 314 MODULE_DESCRIPTION("IR TX driver for Nokia RX51"); 315 MODULE_AUTHOR("Nokia Corporation"); 316 MODULE_LICENSE("GPL"); 317