Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Copyright (C) 2008 Nokia Corporation
0004  *
0005  *  Based on lirc_serial.c
0006  */
0007 #include <linux/clk.h>
0008 #include <linux/module.h>
0009 #include <linux/platform_device.h>
0010 #include <linux/wait.h>
0011 #include <linux/pwm.h>
0012 #include <linux/of.h>
0013 #include <linux/hrtimer.h>
0014 
0015 #include <media/rc-core.h>
0016 
0017 #define WBUF_LEN 256
0018 
0019 struct ir_rx51 {
0020     struct rc_dev *rcdev;
0021     struct pwm_device *pwm;
0022     struct pwm_state state;
0023     struct hrtimer timer;
0024     struct device        *dev;
0025     wait_queue_head_t     wqueue;
0026 
0027     unsigned int    freq;       /* carrier frequency */
0028     unsigned int    duty_cycle; /* carrier duty cycle */
0029     int     wbuf[WBUF_LEN];
0030     int     wbuf_index;
0031     unsigned long   device_is_open;
0032 };
0033 
0034 static inline void ir_rx51_on(struct ir_rx51 *ir_rx51)
0035 {
0036     ir_rx51->state.enabled = true;
0037     pwm_apply_state(ir_rx51->pwm, &ir_rx51->state);
0038 }
0039 
0040 static inline void ir_rx51_off(struct ir_rx51 *ir_rx51)
0041 {
0042     ir_rx51->state.enabled = false;
0043     pwm_apply_state(ir_rx51->pwm, &ir_rx51->state);
0044 }
0045 
0046 static int init_timing_params(struct ir_rx51 *ir_rx51)
0047 {
0048     ir_rx51->state.period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, ir_rx51->freq);
0049     pwm_set_relative_duty_cycle(&ir_rx51->state, ir_rx51->duty_cycle, 100);
0050 
0051     return 0;
0052 }
0053 
0054 static enum hrtimer_restart ir_rx51_timer_cb(struct hrtimer *timer)
0055 {
0056     struct ir_rx51 *ir_rx51 = container_of(timer, struct ir_rx51, timer);
0057     ktime_t now;
0058 
0059     if (ir_rx51->wbuf_index < 0) {
0060         dev_err_ratelimited(ir_rx51->dev,
0061                     "BUG wbuf_index has value of %i\n",
0062                     ir_rx51->wbuf_index);
0063         goto end;
0064     }
0065 
0066     /*
0067      * If we happen to hit an odd latency spike, loop through the
0068      * pulses until we catch up.
0069      */
0070     do {
0071         u64 ns;
0072 
0073         if (ir_rx51->wbuf_index >= WBUF_LEN)
0074             goto end;
0075         if (ir_rx51->wbuf[ir_rx51->wbuf_index] == -1)
0076             goto end;
0077 
0078         if (ir_rx51->wbuf_index % 2)
0079             ir_rx51_off(ir_rx51);
0080         else
0081             ir_rx51_on(ir_rx51);
0082 
0083         ns = US_TO_NS(ir_rx51->wbuf[ir_rx51->wbuf_index]);
0084         hrtimer_add_expires_ns(timer, ns);
0085 
0086         ir_rx51->wbuf_index++;
0087 
0088         now = timer->base->get_time();
0089 
0090     } while (hrtimer_get_expires_tv64(timer) < now);
0091 
0092     return HRTIMER_RESTART;
0093 end:
0094     /* Stop TX here */
0095     ir_rx51_off(ir_rx51);
0096     ir_rx51->wbuf_index = -1;
0097 
0098     wake_up_interruptible(&ir_rx51->wqueue);
0099 
0100     return HRTIMER_NORESTART;
0101 }
0102 
0103 static int ir_rx51_tx(struct rc_dev *dev, unsigned int *buffer,
0104               unsigned int count)
0105 {
0106     struct ir_rx51 *ir_rx51 = dev->priv;
0107 
0108     if (count > WBUF_LEN)
0109         return -EINVAL;
0110 
0111     memcpy(ir_rx51->wbuf, buffer, count * sizeof(unsigned int));
0112 
0113     /* Wait any pending transfers to finish */
0114     wait_event_interruptible(ir_rx51->wqueue, ir_rx51->wbuf_index < 0);
0115 
0116     init_timing_params(ir_rx51);
0117     if (count < WBUF_LEN)
0118         ir_rx51->wbuf[count] = -1; /* Insert termination mark */
0119 
0120     /*
0121      * REVISIT: Adjust latency requirements so the device doesn't go in too
0122      * deep sleep states with pm_qos_add_request().
0123      */
0124 
0125     ir_rx51_on(ir_rx51);
0126     ir_rx51->wbuf_index = 1;
0127     hrtimer_start(&ir_rx51->timer,
0128               ns_to_ktime(US_TO_NS(ir_rx51->wbuf[0])),
0129               HRTIMER_MODE_REL);
0130     /*
0131      * Don't return back to the userspace until the transfer has
0132      * finished
0133      */
0134     wait_event_interruptible(ir_rx51->wqueue, ir_rx51->wbuf_index < 0);
0135 
0136     /* REVISIT: Remove pm_qos constraint, we can sleep again */
0137 
0138     return count;
0139 }
0140 
0141 static int ir_rx51_open(struct rc_dev *dev)
0142 {
0143     struct ir_rx51 *ir_rx51 = dev->priv;
0144 
0145     if (test_and_set_bit(1, &ir_rx51->device_is_open))
0146         return -EBUSY;
0147 
0148     ir_rx51->pwm = pwm_get(ir_rx51->dev, NULL);
0149     if (IS_ERR(ir_rx51->pwm)) {
0150         int res = PTR_ERR(ir_rx51->pwm);
0151 
0152         dev_err(ir_rx51->dev, "pwm_get failed: %d\n", res);
0153         return res;
0154     }
0155 
0156     return 0;
0157 }
0158 
0159 static void ir_rx51_release(struct rc_dev *dev)
0160 {
0161     struct ir_rx51 *ir_rx51 = dev->priv;
0162 
0163     hrtimer_cancel(&ir_rx51->timer);
0164     ir_rx51_off(ir_rx51);
0165     pwm_put(ir_rx51->pwm);
0166 
0167     clear_bit(1, &ir_rx51->device_is_open);
0168 }
0169 
0170 static struct ir_rx51 ir_rx51 = {
0171     .duty_cycle = 50,
0172     .wbuf_index = -1,
0173 };
0174 
0175 static int ir_rx51_set_duty_cycle(struct rc_dev *dev, u32 duty)
0176 {
0177     struct ir_rx51 *ir_rx51 = dev->priv;
0178 
0179     ir_rx51->duty_cycle = duty;
0180 
0181     return 0;
0182 }
0183 
0184 static int ir_rx51_set_tx_carrier(struct rc_dev *dev, u32 carrier)
0185 {
0186     struct ir_rx51 *ir_rx51 = dev->priv;
0187 
0188     if (carrier > 500000 || carrier < 20000)
0189         return -EINVAL;
0190 
0191     ir_rx51->freq = carrier;
0192 
0193     return 0;
0194 }
0195 
0196 #ifdef CONFIG_PM
0197 
0198 static int ir_rx51_suspend(struct platform_device *dev, pm_message_t state)
0199 {
0200     /*
0201      * In case the device is still open, do not suspend. Normally
0202      * this should not be a problem as lircd only keeps the device
0203      * open only for short periods of time. We also don't want to
0204      * get involved with race conditions that might happen if we
0205      * were in a middle of a transmit. Thus, we defer any suspend
0206      * actions until transmit has completed.
0207      */
0208     if (test_and_set_bit(1, &ir_rx51.device_is_open))
0209         return -EAGAIN;
0210 
0211     clear_bit(1, &ir_rx51.device_is_open);
0212 
0213     return 0;
0214 }
0215 
0216 static int ir_rx51_resume(struct platform_device *dev)
0217 {
0218     return 0;
0219 }
0220 
0221 #else
0222 
0223 #define ir_rx51_suspend NULL
0224 #define ir_rx51_resume  NULL
0225 
0226 #endif /* CONFIG_PM */
0227 
0228 static int ir_rx51_probe(struct platform_device *dev)
0229 {
0230     struct pwm_device *pwm;
0231     struct rc_dev *rcdev;
0232 
0233     pwm = pwm_get(&dev->dev, NULL);
0234     if (IS_ERR(pwm)) {
0235         int err = PTR_ERR(pwm);
0236 
0237         if (err != -EPROBE_DEFER)
0238             dev_err(&dev->dev, "pwm_get failed: %d\n", err);
0239         return err;
0240     }
0241 
0242     /* Use default, in case userspace does not set the carrier */
0243     ir_rx51.freq = DIV_ROUND_CLOSEST_ULL(pwm_get_period(pwm), NSEC_PER_SEC);
0244     pwm_init_state(pwm, &ir_rx51.state);
0245     pwm_put(pwm);
0246 
0247     hrtimer_init(&ir_rx51.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
0248     ir_rx51.timer.function = ir_rx51_timer_cb;
0249 
0250     ir_rx51.dev = &dev->dev;
0251 
0252     rcdev = devm_rc_allocate_device(&dev->dev, RC_DRIVER_IR_RAW_TX);
0253     if (!rcdev)
0254         return -ENOMEM;
0255 
0256     rcdev->priv = &ir_rx51;
0257     rcdev->open = ir_rx51_open;
0258     rcdev->close = ir_rx51_release;
0259     rcdev->tx_ir = ir_rx51_tx;
0260     rcdev->s_tx_duty_cycle = ir_rx51_set_duty_cycle;
0261     rcdev->s_tx_carrier = ir_rx51_set_tx_carrier;
0262     rcdev->driver_name = KBUILD_MODNAME;
0263 
0264     ir_rx51.rcdev = rcdev;
0265 
0266     return devm_rc_register_device(&dev->dev, ir_rx51.rcdev);
0267 }
0268 
0269 static int ir_rx51_remove(struct platform_device *dev)
0270 {
0271     return 0;
0272 }
0273 
0274 static const struct of_device_id ir_rx51_match[] = {
0275     {
0276         .compatible = "nokia,n900-ir",
0277     },
0278     {},
0279 };
0280 MODULE_DEVICE_TABLE(of, ir_rx51_match);
0281 
0282 static struct platform_driver ir_rx51_platform_driver = {
0283     .probe      = ir_rx51_probe,
0284     .remove     = ir_rx51_remove,
0285     .suspend    = ir_rx51_suspend,
0286     .resume     = ir_rx51_resume,
0287     .driver     = {
0288         .name   = KBUILD_MODNAME,
0289         .of_match_table = of_match_ptr(ir_rx51_match),
0290     },
0291 };
0292 module_platform_driver(ir_rx51_platform_driver);
0293 
0294 MODULE_DESCRIPTION("IR TX driver for Nokia RX51");
0295 MODULE_AUTHOR("Nokia Corporation");
0296 MODULE_LICENSE("GPL");