Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright (C) 2017 Sean Young <sean@mess.org>
  4 */
  5
  6#include <linux/kernel.h>
  7#include <linux/module.h>
  8#include <linux/pwm.h>
  9#include <linux/delay.h>
 10#include <linux/slab.h>
 11#include <linux/of.h>
 12#include <linux/platform_device.h>
 
 
 13#include <media/rc-core.h>
 14
 15#define DRIVER_NAME	"pwm-ir-tx"
 16#define DEVICE_NAME	"PWM IR Transmitter"
 17
 18struct pwm_ir {
 19	struct pwm_device *pwm;
 20	unsigned int carrier;
 21	unsigned int duty_cycle;
 
 
 
 
 
 
 22};
 23
 24static const struct of_device_id pwm_ir_of_match[] = {
 25	{ .compatible = "pwm-ir-tx", },
 
 26	{ },
 27};
 28MODULE_DEVICE_TABLE(of, pwm_ir_of_match);
 29
 30static int pwm_ir_set_duty_cycle(struct rc_dev *dev, u32 duty_cycle)
 31{
 32	struct pwm_ir *pwm_ir = dev->priv;
 33
 34	pwm_ir->duty_cycle = duty_cycle;
 35
 36	return 0;
 37}
 38
 39static int pwm_ir_set_carrier(struct rc_dev *dev, u32 carrier)
 40{
 41	struct pwm_ir *pwm_ir = dev->priv;
 42
 43	if (!carrier)
 44		return -EINVAL;
 45
 46	pwm_ir->carrier = carrier;
 47
 48	return 0;
 49}
 50
 51static int pwm_ir_tx(struct rc_dev *dev, unsigned int *txbuf,
 52		     unsigned int count)
 53{
 54	struct pwm_ir *pwm_ir = dev->priv;
 55	struct pwm_device *pwm = pwm_ir->pwm;
 56	struct pwm_state state;
 57	int i;
 58	ktime_t edge;
 59	long delta;
 60
 61	pwm_init_state(pwm, &state);
 62
 63	state.period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, pwm_ir->carrier);
 64	pwm_set_relative_duty_cycle(&state, pwm_ir->duty_cycle, 100);
 65
 66	edge = ktime_get();
 67
 68	for (i = 0; i < count; i++) {
 69		state.enabled = !(i % 2);
 70		pwm_apply_state(pwm, &state);
 71
 72		edge = ktime_add_us(edge, txbuf[i]);
 73		delta = ktime_us_delta(edge, ktime_get());
 74		if (delta > 0)
 75			usleep_range(delta, delta + 10);
 76	}
 77
 78	state.enabled = false;
 79	pwm_apply_state(pwm, &state);
 80
 81	return count;
 82}
 83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 84static int pwm_ir_probe(struct platform_device *pdev)
 85{
 86	struct pwm_ir *pwm_ir;
 87	struct rc_dev *rcdev;
 88	int rc;
 89
 90	pwm_ir = devm_kmalloc(&pdev->dev, sizeof(*pwm_ir), GFP_KERNEL);
 91	if (!pwm_ir)
 92		return -ENOMEM;
 93
 94	pwm_ir->pwm = devm_pwm_get(&pdev->dev, NULL);
 95	if (IS_ERR(pwm_ir->pwm))
 96		return PTR_ERR(pwm_ir->pwm);
 97
 98	pwm_ir->carrier = 38000;
 99	pwm_ir->duty_cycle = 50;
100
101	rcdev = devm_rc_allocate_device(&pdev->dev, RC_DRIVER_IR_RAW_TX);
102	if (!rcdev)
103		return -ENOMEM;
104
 
 
 
 
 
 
 
 
 
 
105	rcdev->priv = pwm_ir;
106	rcdev->driver_name = DRIVER_NAME;
107	rcdev->device_name = DEVICE_NAME;
108	rcdev->tx_ir = pwm_ir_tx;
109	rcdev->s_tx_duty_cycle = pwm_ir_set_duty_cycle;
110	rcdev->s_tx_carrier = pwm_ir_set_carrier;
111
112	rc = devm_rc_register_device(&pdev->dev, rcdev);
113	if (rc < 0)
114		dev_err(&pdev->dev, "failed to register rc device\n");
115
116	return rc;
117}
118
119static struct platform_driver pwm_ir_driver = {
120	.probe = pwm_ir_probe,
121	.driver = {
122		.name	= DRIVER_NAME,
123		.of_match_table = of_match_ptr(pwm_ir_of_match),
124	},
125};
126module_platform_driver(pwm_ir_driver);
127
128MODULE_DESCRIPTION("PWM IR Transmitter");
129MODULE_AUTHOR("Sean Young <sean@mess.org>");
130MODULE_LICENSE("GPL");
v6.8
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright (C) 2017 Sean Young <sean@mess.org>
  4 */
  5
  6#include <linux/kernel.h>
  7#include <linux/module.h>
  8#include <linux/pwm.h>
  9#include <linux/delay.h>
 10#include <linux/slab.h>
 11#include <linux/of.h>
 12#include <linux/platform_device.h>
 13#include <linux/hrtimer.h>
 14#include <linux/completion.h>
 15#include <media/rc-core.h>
 16
 17#define DRIVER_NAME	"pwm-ir-tx"
 18#define DEVICE_NAME	"PWM IR Transmitter"
 19
 20struct pwm_ir {
 21	struct pwm_device *pwm;
 22	struct hrtimer timer;
 23	struct completion tx_done;
 24	struct pwm_state *state;
 25	u32 carrier;
 26	u32 duty_cycle;
 27	const unsigned int *txbuf;
 28	unsigned int txbuf_len;
 29	unsigned int txbuf_index;
 30};
 31
 32static const struct of_device_id pwm_ir_of_match[] = {
 33	{ .compatible = "pwm-ir-tx", },
 34	{ .compatible = "nokia,n900-ir" },
 35	{ },
 36};
 37MODULE_DEVICE_TABLE(of, pwm_ir_of_match);
 38
 39static int pwm_ir_set_duty_cycle(struct rc_dev *dev, u32 duty_cycle)
 40{
 41	struct pwm_ir *pwm_ir = dev->priv;
 42
 43	pwm_ir->duty_cycle = duty_cycle;
 44
 45	return 0;
 46}
 47
 48static int pwm_ir_set_carrier(struct rc_dev *dev, u32 carrier)
 49{
 50	struct pwm_ir *pwm_ir = dev->priv;
 51
 52	if (!carrier)
 53		return -EINVAL;
 54
 55	pwm_ir->carrier = carrier;
 56
 57	return 0;
 58}
 59
 60static int pwm_ir_tx_sleep(struct rc_dev *dev, unsigned int *txbuf,
 61			   unsigned int count)
 62{
 63	struct pwm_ir *pwm_ir = dev->priv;
 64	struct pwm_device *pwm = pwm_ir->pwm;
 65	struct pwm_state state;
 66	int i;
 67	ktime_t edge;
 68	long delta;
 69
 70	pwm_init_state(pwm, &state);
 71
 72	state.period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, pwm_ir->carrier);
 73	pwm_set_relative_duty_cycle(&state, pwm_ir->duty_cycle, 100);
 74
 75	edge = ktime_get();
 76
 77	for (i = 0; i < count; i++) {
 78		state.enabled = !(i % 2);
 79		pwm_apply_might_sleep(pwm, &state);
 80
 81		edge = ktime_add_us(edge, txbuf[i]);
 82		delta = ktime_us_delta(edge, ktime_get());
 83		if (delta > 0)
 84			usleep_range(delta, delta + 10);
 85	}
 86
 87	state.enabled = false;
 88	pwm_apply_might_sleep(pwm, &state);
 89
 90	return count;
 91}
 92
 93static int pwm_ir_tx_atomic(struct rc_dev *dev, unsigned int *txbuf,
 94			    unsigned int count)
 95{
 96	struct pwm_ir *pwm_ir = dev->priv;
 97	struct pwm_device *pwm = pwm_ir->pwm;
 98	struct pwm_state state;
 99
100	pwm_init_state(pwm, &state);
101
102	state.period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, pwm_ir->carrier);
103	pwm_set_relative_duty_cycle(&state, pwm_ir->duty_cycle, 100);
104
105	pwm_ir->txbuf = txbuf;
106	pwm_ir->txbuf_len = count;
107	pwm_ir->txbuf_index = 0;
108	pwm_ir->state = &state;
109
110	hrtimer_start(&pwm_ir->timer, 0, HRTIMER_MODE_REL);
111
112	wait_for_completion(&pwm_ir->tx_done);
113
114	return count;
115}
116
117static enum hrtimer_restart pwm_ir_timer(struct hrtimer *timer)
118{
119	struct pwm_ir *pwm_ir = container_of(timer, struct pwm_ir, timer);
120	ktime_t now;
121
122	/*
123	 * If we happen to hit an odd latency spike, loop through the
124	 * pulses until we catch up.
125	 */
126	do {
127		u64 ns;
128
129		pwm_ir->state->enabled = !(pwm_ir->txbuf_index % 2);
130		pwm_apply_atomic(pwm_ir->pwm, pwm_ir->state);
131
132		if (pwm_ir->txbuf_index >= pwm_ir->txbuf_len) {
133			complete(&pwm_ir->tx_done);
134
135			return HRTIMER_NORESTART;
136		}
137
138		ns = US_TO_NS(pwm_ir->txbuf[pwm_ir->txbuf_index]);
139		hrtimer_add_expires_ns(timer, ns);
140
141		pwm_ir->txbuf_index++;
142
143		now = timer->base->get_time();
144	} while (hrtimer_get_expires_tv64(timer) < now);
145
146	return HRTIMER_RESTART;
147}
148
149static int pwm_ir_probe(struct platform_device *pdev)
150{
151	struct pwm_ir *pwm_ir;
152	struct rc_dev *rcdev;
153	int rc;
154
155	pwm_ir = devm_kmalloc(&pdev->dev, sizeof(*pwm_ir), GFP_KERNEL);
156	if (!pwm_ir)
157		return -ENOMEM;
158
159	pwm_ir->pwm = devm_pwm_get(&pdev->dev, NULL);
160	if (IS_ERR(pwm_ir->pwm))
161		return PTR_ERR(pwm_ir->pwm);
162
163	pwm_ir->carrier = 38000;
164	pwm_ir->duty_cycle = 50;
165
166	rcdev = devm_rc_allocate_device(&pdev->dev, RC_DRIVER_IR_RAW_TX);
167	if (!rcdev)
168		return -ENOMEM;
169
170	if (pwm_might_sleep(pwm_ir->pwm)) {
171		dev_info(&pdev->dev, "TX will not be accurate as PWM device might sleep\n");
172		rcdev->tx_ir = pwm_ir_tx_sleep;
173	} else {
174		init_completion(&pwm_ir->tx_done);
175		hrtimer_init(&pwm_ir->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
176		pwm_ir->timer.function = pwm_ir_timer;
177		rcdev->tx_ir = pwm_ir_tx_atomic;
178	}
179
180	rcdev->priv = pwm_ir;
181	rcdev->driver_name = DRIVER_NAME;
182	rcdev->device_name = DEVICE_NAME;
 
183	rcdev->s_tx_duty_cycle = pwm_ir_set_duty_cycle;
184	rcdev->s_tx_carrier = pwm_ir_set_carrier;
185
186	rc = devm_rc_register_device(&pdev->dev, rcdev);
187	if (rc < 0)
188		dev_err(&pdev->dev, "failed to register rc device\n");
189
190	return rc;
191}
192
193static struct platform_driver pwm_ir_driver = {
194	.probe = pwm_ir_probe,
195	.driver = {
196		.name	= DRIVER_NAME,
197		.of_match_table = pwm_ir_of_match,
198	},
199};
200module_platform_driver(pwm_ir_driver);
201
202MODULE_DESCRIPTION("PWM IR Transmitter");
203MODULE_AUTHOR("Sean Young <sean@mess.org>");
204MODULE_LICENSE("GPL");