blob: 68fc6a24d07710f6c8786abf4dca9d6395c70edc [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright 2017 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
3 *
4 * This program is free software; you may redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
9 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
10 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
11 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
12 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
13 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
14 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
15 * SOFTWARE.
16 */
17
18#include <linux/delay.h>
19#include <linux/slab.h>
20#include <linux/sched/types.h>
21
22#include <media/cec-pin.h>
23
24/* All timings are in microseconds */
25
26/* start bit timings */
27#define CEC_TIM_START_BIT_LOW 3700
28#define CEC_TIM_START_BIT_LOW_MIN 3500
29#define CEC_TIM_START_BIT_LOW_MAX 3900
30#define CEC_TIM_START_BIT_TOTAL 4500
31#define CEC_TIM_START_BIT_TOTAL_MIN 4300
32#define CEC_TIM_START_BIT_TOTAL_MAX 4700
33
34/* data bit timings */
35#define CEC_TIM_DATA_BIT_0_LOW 1500
36#define CEC_TIM_DATA_BIT_0_LOW_MIN 1300
37#define CEC_TIM_DATA_BIT_0_LOW_MAX 1700
38#define CEC_TIM_DATA_BIT_1_LOW 600
39#define CEC_TIM_DATA_BIT_1_LOW_MIN 400
40#define CEC_TIM_DATA_BIT_1_LOW_MAX 800
41#define CEC_TIM_DATA_BIT_TOTAL 2400
42#define CEC_TIM_DATA_BIT_TOTAL_MIN 2050
43#define CEC_TIM_DATA_BIT_TOTAL_MAX 2750
44/* earliest safe time to sample the bit state */
45#define CEC_TIM_DATA_BIT_SAMPLE 850
46/* earliest time the bit is back to 1 (T7 + 50) */
47#define CEC_TIM_DATA_BIT_HIGH 1750
48
49/* when idle, sample once per millisecond */
50#define CEC_TIM_IDLE_SAMPLE 1000
51/* when processing the start bit, sample twice per millisecond */
52#define CEC_TIM_START_BIT_SAMPLE 500
53/* when polling for a state change, sample once every 50 micoseconds */
54#define CEC_TIM_SAMPLE 50
55
56#define CEC_TIM_LOW_DRIVE_ERROR (1.5 * CEC_TIM_DATA_BIT_TOTAL)
57
58struct cec_state {
59 const char * const name;
60 unsigned int usecs;
61};
62
63static const struct cec_state states[CEC_PIN_STATES] = {
64 { "Off", 0 },
65 { "Idle", CEC_TIM_IDLE_SAMPLE },
66 { "Tx Wait", CEC_TIM_SAMPLE },
67 { "Tx Wait for High", CEC_TIM_IDLE_SAMPLE },
68 { "Tx Start Bit Low", CEC_TIM_START_BIT_LOW },
69 { "Tx Start Bit High", CEC_TIM_START_BIT_TOTAL - CEC_TIM_START_BIT_LOW },
70 { "Tx Data 0 Low", CEC_TIM_DATA_BIT_0_LOW },
71 { "Tx Data 0 High", CEC_TIM_DATA_BIT_TOTAL - CEC_TIM_DATA_BIT_0_LOW },
72 { "Tx Data 1 Low", CEC_TIM_DATA_BIT_1_LOW },
73 { "Tx Data 1 High", CEC_TIM_DATA_BIT_TOTAL - CEC_TIM_DATA_BIT_1_LOW },
74 { "Tx Data 1 Pre Sample", CEC_TIM_DATA_BIT_SAMPLE - CEC_TIM_DATA_BIT_1_LOW },
75 { "Tx Data 1 Post Sample", CEC_TIM_DATA_BIT_TOTAL - CEC_TIM_DATA_BIT_SAMPLE },
76 { "Rx Start Bit Low", CEC_TIM_SAMPLE },
77 { "Rx Start Bit High", CEC_TIM_SAMPLE },
78 { "Rx Data Sample", CEC_TIM_DATA_BIT_SAMPLE },
79 { "Rx Data Post Sample", CEC_TIM_DATA_BIT_HIGH - CEC_TIM_DATA_BIT_SAMPLE },
80 { "Rx Data High", CEC_TIM_SAMPLE },
81 { "Rx Ack Low", CEC_TIM_DATA_BIT_0_LOW },
82 { "Rx Ack Low Post", CEC_TIM_DATA_BIT_HIGH - CEC_TIM_DATA_BIT_0_LOW },
83 { "Rx Ack High Post", CEC_TIM_DATA_BIT_HIGH },
84 { "Rx Ack Finish", CEC_TIM_DATA_BIT_TOTAL_MIN - CEC_TIM_DATA_BIT_HIGH },
85 { "Rx Low Drive", CEC_TIM_LOW_DRIVE_ERROR },
86 { "Rx Irq", 0 },
87};
88
89static void cec_pin_update(struct cec_pin *pin, bool v, bool force)
90{
91 if (!force && v == pin->adap->cec_pin_is_high)
92 return;
93
94 pin->adap->cec_pin_is_high = v;
95 if (atomic_read(&pin->work_pin_events) < CEC_NUM_PIN_EVENTS) {
96 pin->work_pin_is_high[pin->work_pin_events_wr] = v;
97 pin->work_pin_ts[pin->work_pin_events_wr] = ktime_get();
98 pin->work_pin_events_wr =
99 (pin->work_pin_events_wr + 1) % CEC_NUM_PIN_EVENTS;
100 atomic_inc(&pin->work_pin_events);
101 }
102 wake_up_interruptible(&pin->kthread_waitq);
103}
104
105static bool cec_pin_read(struct cec_pin *pin)
106{
107 bool v = pin->ops->read(pin->adap);
108
109 cec_pin_update(pin, v, false);
110 return v;
111}
112
113static void cec_pin_low(struct cec_pin *pin)
114{
115 pin->ops->low(pin->adap);
116 cec_pin_update(pin, false, false);
117}
118
119static bool cec_pin_high(struct cec_pin *pin)
120{
121 pin->ops->high(pin->adap);
122 return cec_pin_read(pin);
123}
124
125static void cec_pin_to_idle(struct cec_pin *pin)
126{
127 /*
128 * Reset all status fields, release the bus and
129 * go to idle state.
130 */
131 pin->rx_bit = pin->tx_bit = 0;
132 pin->rx_msg.len = 0;
133 memset(pin->rx_msg.msg, 0, sizeof(pin->rx_msg.msg));
134 pin->state = CEC_ST_IDLE;
135 pin->ts = 0;
136}
137
138/*
139 * Handle Transmit-related states
140 *
141 * Basic state changes when transmitting:
142 *
143 * Idle -> Tx Wait (waiting for the end of signal free time) ->
144 * Tx Start Bit Low -> Tx Start Bit High ->
145 *
146 * Regular data bits + EOM:
147 * Tx Data 0 Low -> Tx Data 0 High ->
148 * or:
149 * Tx Data 1 Low -> Tx Data 1 High ->
150 *
151 * First 4 data bits or Ack bit:
152 * Tx Data 0 Low -> Tx Data 0 High ->
153 * or:
154 * Tx Data 1 Low -> Tx Data 1 High -> Tx Data 1 Pre Sample ->
155 * Tx Data 1 Post Sample ->
156 *
157 * After the last Ack go to Idle.
158 *
159 * If it detects a Low Drive condition then:
160 * Tx Wait For High -> Idle
161 *
162 * If it loses arbitration, then it switches to state Rx Data Post Sample.
163 */
164static void cec_pin_tx_states(struct cec_pin *pin, ktime_t ts)
165{
166 bool v;
167 bool is_ack_bit, ack;
168
169 switch (pin->state) {
170 case CEC_ST_TX_WAIT_FOR_HIGH:
171 if (cec_pin_read(pin))
172 cec_pin_to_idle(pin);
173 break;
174
175 case CEC_ST_TX_START_BIT_LOW:
176 pin->state = CEC_ST_TX_START_BIT_HIGH;
177 /* Generate start bit */
178 cec_pin_high(pin);
179 break;
180
181 case CEC_ST_TX_DATA_BIT_1_HIGH_POST_SAMPLE:
182 /* If the read value is 1, then all is OK */
183 if (!cec_pin_read(pin)) {
184 /*
185 * It's 0, so someone detected an error and pulled the
186 * line low for 1.5 times the nominal bit period.
187 */
188 pin->tx_msg.len = 0;
189 pin->work_tx_ts = ts;
190 pin->work_tx_status = CEC_TX_STATUS_LOW_DRIVE;
191 pin->state = CEC_ST_TX_WAIT_FOR_HIGH;
192 wake_up_interruptible(&pin->kthread_waitq);
193 break;
194 }
195 if (pin->tx_nacked) {
196 cec_pin_to_idle(pin);
197 pin->tx_msg.len = 0;
198 pin->work_tx_ts = ts;
199 pin->work_tx_status = CEC_TX_STATUS_NACK;
200 wake_up_interruptible(&pin->kthread_waitq);
201 break;
202 }
203 /* fall through */
204 case CEC_ST_TX_DATA_BIT_0_HIGH:
205 case CEC_ST_TX_DATA_BIT_1_HIGH:
206 pin->tx_bit++;
207 /* fall through */
208 case CEC_ST_TX_START_BIT_HIGH:
209 if (pin->tx_bit / 10 >= pin->tx_msg.len) {
210 cec_pin_to_idle(pin);
211 pin->tx_msg.len = 0;
212 pin->work_tx_ts = ts;
213 pin->work_tx_status = CEC_TX_STATUS_OK;
214 wake_up_interruptible(&pin->kthread_waitq);
215 break;
216 }
217
218 switch (pin->tx_bit % 10) {
219 default:
220 v = pin->tx_msg.msg[pin->tx_bit / 10] &
221 (1 << (7 - (pin->tx_bit % 10)));
222 pin->state = v ? CEC_ST_TX_DATA_BIT_1_LOW :
223 CEC_ST_TX_DATA_BIT_0_LOW;
224 break;
225 case 8:
226 v = pin->tx_bit / 10 == pin->tx_msg.len - 1;
227 pin->state = v ? CEC_ST_TX_DATA_BIT_1_LOW :
228 CEC_ST_TX_DATA_BIT_0_LOW;
229 break;
230 case 9:
231 pin->state = CEC_ST_TX_DATA_BIT_1_LOW;
232 break;
233 }
234 cec_pin_low(pin);
235 break;
236
237 case CEC_ST_TX_DATA_BIT_0_LOW:
238 case CEC_ST_TX_DATA_BIT_1_LOW:
239 v = pin->state == CEC_ST_TX_DATA_BIT_1_LOW;
240 pin->state = v ? CEC_ST_TX_DATA_BIT_1_HIGH :
241 CEC_ST_TX_DATA_BIT_0_HIGH;
242 is_ack_bit = pin->tx_bit % 10 == 9;
243 if (v && (pin->tx_bit < 4 || is_ack_bit))
244 pin->state = CEC_ST_TX_DATA_BIT_1_HIGH_PRE_SAMPLE;
245 cec_pin_high(pin);
246 break;
247
248 case CEC_ST_TX_DATA_BIT_1_HIGH_PRE_SAMPLE:
249 /* Read the CEC value at the sample time */
250 v = cec_pin_read(pin);
251 is_ack_bit = pin->tx_bit % 10 == 9;
252 /*
253 * If v == 0 and we're within the first 4 bits
254 * of the initiator, then someone else started
255 * transmitting and we lost the arbitration
256 * (i.e. the logical address of the other
257 * transmitter has more leading 0 bits in the
258 * initiator).
259 */
260 if (!v && !is_ack_bit) {
261 pin->tx_msg.len = 0;
262 pin->work_tx_ts = ts;
263 pin->work_tx_status = CEC_TX_STATUS_ARB_LOST;
264 wake_up_interruptible(&pin->kthread_waitq);
265 pin->rx_bit = pin->tx_bit;
266 pin->tx_bit = 0;
267 memset(pin->rx_msg.msg, 0, sizeof(pin->rx_msg.msg));
268 pin->rx_msg.msg[0] = pin->tx_msg.msg[0];
269 pin->rx_msg.msg[0] &= ~(1 << (7 - pin->rx_bit));
270 pin->rx_msg.len = 0;
271 pin->state = CEC_ST_RX_DATA_POST_SAMPLE;
272 pin->rx_bit++;
273 break;
274 }
275 pin->state = CEC_ST_TX_DATA_BIT_1_HIGH_POST_SAMPLE;
276 if (!is_ack_bit)
277 break;
278 /* Was the message ACKed? */
279 ack = cec_msg_is_broadcast(&pin->tx_msg) ? v : !v;
280 if (!ack) {
281 /*
282 * Note: the CEC spec is ambiguous regarding
283 * what action to take when a NACK appears
284 * before the last byte of the payload was
285 * transmitted: either stop transmitting
286 * immediately, or wait until the last byte
287 * was transmitted.
288 *
289 * Most CEC implementations appear to stop
290 * immediately, and that's what we do here
291 * as well.
292 */
293 pin->tx_nacked = true;
294 }
295 break;
296
297 default:
298 break;
299 }
300}
301
302/*
303 * Handle Receive-related states
304 *
305 * Basic state changes when receiving:
306 *
307 * Rx Start Bit Low -> Rx Start Bit High ->
308 * Regular data bits + EOM:
309 * Rx Data Sample -> Rx Data Post Sample -> Rx Data High ->
310 * Ack bit 0:
311 * Rx Ack Low -> Rx Ack Low Post -> Rx Data High ->
312 * Ack bit 1:
313 * Rx Ack High Post -> Rx Data High ->
314 * Ack bit 0 && EOM:
315 * Rx Ack Low -> Rx Ack Low Post -> Rx Ack Finish -> Idle
316 */
317static void cec_pin_rx_states(struct cec_pin *pin, ktime_t ts)
318{
319 s32 delta;
320 bool v;
321 bool ack;
322 bool bcast, for_us;
323 u8 dest;
324
325 switch (pin->state) {
326 /* Receive states */
327 case CEC_ST_RX_START_BIT_LOW:
328 v = cec_pin_read(pin);
329 if (!v)
330 break;
331 pin->state = CEC_ST_RX_START_BIT_HIGH;
332 delta = ktime_us_delta(ts, pin->ts);
333 pin->ts = ts;
334 /* Start bit low is too short, go back to idle */
335 if (delta < CEC_TIM_START_BIT_LOW_MIN -
336 CEC_TIM_IDLE_SAMPLE) {
337 cec_pin_to_idle(pin);
338 }
339 break;
340
341 case CEC_ST_RX_START_BIT_HIGH:
342 v = cec_pin_read(pin);
343 delta = ktime_us_delta(ts, pin->ts);
344 if (v && delta > CEC_TIM_START_BIT_TOTAL_MAX -
345 CEC_TIM_START_BIT_LOW_MIN) {
346 cec_pin_to_idle(pin);
347 break;
348 }
349 if (v)
350 break;
351 pin->state = CEC_ST_RX_DATA_SAMPLE;
352 pin->ts = ts;
353 pin->rx_eom = false;
354 break;
355
356 case CEC_ST_RX_DATA_SAMPLE:
357 v = cec_pin_read(pin);
358 pin->state = CEC_ST_RX_DATA_POST_SAMPLE;
359 switch (pin->rx_bit % 10) {
360 default:
361 if (pin->rx_bit / 10 < CEC_MAX_MSG_SIZE)
362 pin->rx_msg.msg[pin->rx_bit / 10] |=
363 v << (7 - (pin->rx_bit % 10));
364 break;
365 case 8:
366 pin->rx_eom = v;
367 pin->rx_msg.len = pin->rx_bit / 10 + 1;
368 break;
369 case 9:
370 break;
371 }
372 pin->rx_bit++;
373 break;
374
375 case CEC_ST_RX_DATA_POST_SAMPLE:
376 pin->state = CEC_ST_RX_DATA_HIGH;
377 break;
378
379 case CEC_ST_RX_DATA_HIGH:
380 v = cec_pin_read(pin);
381 delta = ktime_us_delta(ts, pin->ts);
382 if (v && delta > CEC_TIM_DATA_BIT_TOTAL_MAX) {
383 cec_pin_to_idle(pin);
384 break;
385 }
386 if (v)
387 break;
388 /*
389 * Go to low drive state when the total bit time is
390 * too short.
391 */
392 if (delta < CEC_TIM_DATA_BIT_TOTAL_MIN) {
393 cec_pin_low(pin);
394 pin->state = CEC_ST_LOW_DRIVE;
395 break;
396 }
397 pin->ts = ts;
398 if (pin->rx_bit % 10 != 9) {
399 pin->state = CEC_ST_RX_DATA_SAMPLE;
400 break;
401 }
402
403 dest = cec_msg_destination(&pin->rx_msg);
404 bcast = dest == CEC_LOG_ADDR_BROADCAST;
405 /* for_us == broadcast or directed to us */
406 for_us = bcast || (pin->la_mask & (1 << dest));
407 /* ACK bit value */
408 ack = bcast ? 1 : !for_us;
409
410 if (ack) {
411 /* No need to write to the bus, just wait */
412 pin->state = CEC_ST_RX_ACK_HIGH_POST;
413 break;
414 }
415 cec_pin_low(pin);
416 pin->state = CEC_ST_RX_ACK_LOW;
417 break;
418
419 case CEC_ST_RX_ACK_LOW:
420 cec_pin_high(pin);
421 pin->state = CEC_ST_RX_ACK_LOW_POST;
422 break;
423
424 case CEC_ST_RX_ACK_LOW_POST:
425 case CEC_ST_RX_ACK_HIGH_POST:
426 v = cec_pin_read(pin);
427 if (v && pin->rx_eom) {
428 pin->work_rx_msg = pin->rx_msg;
429 pin->work_rx_msg.rx_ts = ts;
430 wake_up_interruptible(&pin->kthread_waitq);
431 pin->ts = ts;
432 pin->state = CEC_ST_RX_ACK_FINISH;
433 break;
434 }
435 pin->rx_bit++;
436 pin->state = CEC_ST_RX_DATA_HIGH;
437 break;
438
439 case CEC_ST_RX_ACK_FINISH:
440 cec_pin_to_idle(pin);
441 break;
442
443 default:
444 break;
445 }
446}
447
448/*
449 * Main timer function
450 *
451 */
452static enum hrtimer_restart cec_pin_timer(struct hrtimer *timer)
453{
454 struct cec_pin *pin = container_of(timer, struct cec_pin, timer);
455 struct cec_adapter *adap = pin->adap;
456 ktime_t ts;
457 s32 delta;
458
459 ts = ktime_get();
460 if (pin->timer_ts) {
461 delta = ktime_us_delta(ts, pin->timer_ts);
462 pin->timer_cnt++;
463 if (delta > 100 && pin->state != CEC_ST_IDLE) {
464 /* Keep track of timer overruns */
465 pin->timer_sum_overrun += delta;
466 pin->timer_100ms_overruns++;
467 if (delta > 300)
468 pin->timer_300ms_overruns++;
469 if (delta > pin->timer_max_overrun)
470 pin->timer_max_overrun = delta;
471 }
472 }
473 if (adap->monitor_pin_cnt)
474 cec_pin_read(pin);
475
476 if (pin->wait_usecs) {
477 /*
478 * If we are monitoring the pin, then we have to
479 * sample at regular intervals.
480 */
481 if (pin->wait_usecs > 150) {
482 pin->wait_usecs -= 100;
483 pin->timer_ts = ktime_add_us(ts, 100);
484 hrtimer_forward_now(timer, 100000);
485 return HRTIMER_RESTART;
486 }
487 if (pin->wait_usecs > 100) {
488 pin->wait_usecs /= 2;
489 pin->timer_ts = ktime_add_us(ts, pin->wait_usecs);
490 hrtimer_forward_now(timer, pin->wait_usecs * 1000);
491 return HRTIMER_RESTART;
492 }
493 pin->timer_ts = ktime_add_us(ts, pin->wait_usecs);
494 hrtimer_forward_now(timer, pin->wait_usecs * 1000);
495 pin->wait_usecs = 0;
496 return HRTIMER_RESTART;
497 }
498
499 switch (pin->state) {
500 /* Transmit states */
501 case CEC_ST_TX_WAIT_FOR_HIGH:
502 case CEC_ST_TX_START_BIT_LOW:
503 case CEC_ST_TX_DATA_BIT_1_HIGH_POST_SAMPLE:
504 case CEC_ST_TX_DATA_BIT_0_HIGH:
505 case CEC_ST_TX_DATA_BIT_1_HIGH:
506 case CEC_ST_TX_START_BIT_HIGH:
507 case CEC_ST_TX_DATA_BIT_0_LOW:
508 case CEC_ST_TX_DATA_BIT_1_LOW:
509 case CEC_ST_TX_DATA_BIT_1_HIGH_PRE_SAMPLE:
510 cec_pin_tx_states(pin, ts);
511 break;
512
513 /* Receive states */
514 case CEC_ST_RX_START_BIT_LOW:
515 case CEC_ST_RX_START_BIT_HIGH:
516 case CEC_ST_RX_DATA_SAMPLE:
517 case CEC_ST_RX_DATA_POST_SAMPLE:
518 case CEC_ST_RX_DATA_HIGH:
519 case CEC_ST_RX_ACK_LOW:
520 case CEC_ST_RX_ACK_LOW_POST:
521 case CEC_ST_RX_ACK_HIGH_POST:
522 case CEC_ST_RX_ACK_FINISH:
523 cec_pin_rx_states(pin, ts);
524 break;
525
526 case CEC_ST_IDLE:
527 case CEC_ST_TX_WAIT:
528 if (!cec_pin_high(pin)) {
529 /* Start bit, switch to receive state */
530 pin->ts = ts;
531 pin->state = CEC_ST_RX_START_BIT_LOW;
532 /*
533 * If a transmit is pending, then that transmit should
534 * use a signal free time of no more than
535 * CEC_SIGNAL_FREE_TIME_NEW_INITIATOR since it will
536 * have a new initiator due to the receive that is now
537 * starting.
538 */
539 if (pin->tx_msg.len && pin->tx_signal_free_time >
540 CEC_SIGNAL_FREE_TIME_NEW_INITIATOR)
541 pin->tx_signal_free_time =
542 CEC_SIGNAL_FREE_TIME_NEW_INITIATOR;
543 break;
544 }
545 if (pin->ts == 0)
546 pin->ts = ts;
547 if (pin->tx_msg.len) {
548 /*
549 * Check if the bus has been free for long enough
550 * so we can kick off the pending transmit.
551 */
552 delta = ktime_us_delta(ts, pin->ts);
553 if (delta / CEC_TIM_DATA_BIT_TOTAL >
554 pin->tx_signal_free_time) {
555 pin->tx_nacked = false;
556 pin->state = CEC_ST_TX_START_BIT_LOW;
557 /* Generate start bit */
558 cec_pin_low(pin);
559 break;
560 }
561 if (delta / CEC_TIM_DATA_BIT_TOTAL >
562 pin->tx_signal_free_time - 1)
563 pin->state = CEC_ST_TX_WAIT;
564 break;
565 }
566 if (pin->state != CEC_ST_IDLE || pin->ops->enable_irq == NULL ||
567 pin->enable_irq_failed || adap->is_configuring ||
568 adap->is_configured || adap->monitor_all_cnt)
569 break;
570 /* Switch to interrupt mode */
571 atomic_set(&pin->work_irq_change, CEC_PIN_IRQ_ENABLE);
572 pin->state = CEC_ST_RX_IRQ;
573 wake_up_interruptible(&pin->kthread_waitq);
574 return HRTIMER_NORESTART;
575
576 case CEC_ST_LOW_DRIVE:
577 cec_pin_to_idle(pin);
578 break;
579
580 default:
581 break;
582 }
583 if (!adap->monitor_pin_cnt || states[pin->state].usecs <= 150) {
584 pin->wait_usecs = 0;
585 pin->timer_ts = ktime_add_us(ts, states[pin->state].usecs);
586 hrtimer_forward_now(timer, states[pin->state].usecs * 1000);
587 return HRTIMER_RESTART;
588 }
589 pin->wait_usecs = states[pin->state].usecs - 100;
590 pin->timer_ts = ktime_add_us(ts, 100);
591 hrtimer_forward_now(timer, 100000);
592 return HRTIMER_RESTART;
593}
594
595static int cec_pin_thread_func(void *_adap)
596{
597 struct cec_adapter *adap = _adap;
598 struct cec_pin *pin = adap->pin;
599
600 for (;;) {
601 wait_event_interruptible(pin->kthread_waitq,
602 kthread_should_stop() ||
603 pin->work_rx_msg.len ||
604 pin->work_tx_status ||
605 atomic_read(&pin->work_irq_change) ||
606 atomic_read(&pin->work_pin_events));
607
608 if (pin->work_rx_msg.len) {
609 cec_received_msg_ts(adap, &pin->work_rx_msg,
610 pin->work_rx_msg.rx_ts);
611 pin->work_rx_msg.len = 0;
612 }
613 if (pin->work_tx_status) {
614 unsigned int tx_status = pin->work_tx_status;
615
616 pin->work_tx_status = 0;
617 cec_transmit_attempt_done_ts(adap, tx_status,
618 pin->work_tx_ts);
619 }
620
621 while (atomic_read(&pin->work_pin_events)) {
622 unsigned int idx = pin->work_pin_events_rd;
623
624 cec_queue_pin_cec_event(adap,
625 pin->work_pin_is_high[idx],
626 pin->work_pin_ts[idx]);
627 pin->work_pin_events_rd = (idx + 1) % CEC_NUM_PIN_EVENTS;
628 atomic_dec(&pin->work_pin_events);
629 }
630
631 switch (atomic_xchg(&pin->work_irq_change,
632 CEC_PIN_IRQ_UNCHANGED)) {
633 case CEC_PIN_IRQ_DISABLE:
634 pin->ops->disable_irq(adap);
635 cec_pin_high(pin);
636 cec_pin_to_idle(pin);
637 hrtimer_start(&pin->timer, 0, HRTIMER_MODE_REL);
638 break;
639 case CEC_PIN_IRQ_ENABLE:
640 pin->enable_irq_failed = !pin->ops->enable_irq(adap);
641 if (pin->enable_irq_failed) {
642 cec_pin_to_idle(pin);
643 hrtimer_start(&pin->timer, 0, HRTIMER_MODE_REL);
644 }
645 break;
646 default:
647 break;
648 }
649
650 if (kthread_should_stop())
651 break;
652 }
653 return 0;
654}
655
656static int cec_pin_adap_enable(struct cec_adapter *adap, bool enable)
657{
658 struct cec_pin *pin = adap->pin;
659
660 pin->enabled = enable;
661 if (enable) {
662 atomic_set(&pin->work_pin_events, 0);
663 pin->work_pin_events_rd = pin->work_pin_events_wr = 0;
664 cec_pin_read(pin);
665 cec_pin_to_idle(pin);
666 pin->tx_msg.len = 0;
667 pin->timer_ts = 0;
668 atomic_set(&pin->work_irq_change, CEC_PIN_IRQ_UNCHANGED);
669 pin->kthread = kthread_run(cec_pin_thread_func, adap,
670 "cec-pin");
671 if (IS_ERR(pin->kthread)) {
672 pr_err("cec-pin: kernel_thread() failed\n");
673 return PTR_ERR(pin->kthread);
674 }
675 hrtimer_start(&pin->timer, 0, HRTIMER_MODE_REL);
676 } else {
677 if (pin->ops->disable_irq)
678 pin->ops->disable_irq(adap);
679 hrtimer_cancel(&pin->timer);
680 kthread_stop(pin->kthread);
681 cec_pin_read(pin);
682 cec_pin_to_idle(pin);
683 pin->state = CEC_ST_OFF;
684 }
685 return 0;
686}
687
688static int cec_pin_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
689{
690 struct cec_pin *pin = adap->pin;
691
692 if (log_addr == CEC_LOG_ADDR_INVALID)
693 pin->la_mask = 0;
694 else
695 pin->la_mask |= (1 << log_addr);
696 return 0;
697}
698
699static int cec_pin_adap_transmit(struct cec_adapter *adap, u8 attempts,
700 u32 signal_free_time, struct cec_msg *msg)
701{
702 struct cec_pin *pin = adap->pin;
703
704 /*
705 * If a receive is in progress, then this transmit should use
706 * a signal free time of max CEC_SIGNAL_FREE_TIME_NEW_INITIATOR
707 * since when it starts transmitting it will have a new initiator.
708 */
709 if (pin->state != CEC_ST_IDLE &&
710 signal_free_time > CEC_SIGNAL_FREE_TIME_NEW_INITIATOR)
711 signal_free_time = CEC_SIGNAL_FREE_TIME_NEW_INITIATOR;
712
713 pin->tx_signal_free_time = signal_free_time;
714 pin->tx_msg = *msg;
715 pin->work_tx_status = 0;
716 pin->tx_bit = 0;
717 if (pin->state == CEC_ST_RX_IRQ) {
718 atomic_set(&pin->work_irq_change, CEC_PIN_IRQ_UNCHANGED);
719 pin->ops->disable_irq(adap);
720 cec_pin_high(pin);
721 cec_pin_to_idle(pin);
722 hrtimer_start(&pin->timer, 0, HRTIMER_MODE_REL);
723 }
724 return 0;
725}
726
727static void cec_pin_adap_status(struct cec_adapter *adap,
728 struct seq_file *file)
729{
730 struct cec_pin *pin = adap->pin;
731
732 seq_printf(file, "state: %s\n", states[pin->state].name);
733 seq_printf(file, "tx_bit: %d\n", pin->tx_bit);
734 seq_printf(file, "rx_bit: %d\n", pin->rx_bit);
735 seq_printf(file, "cec pin: %d\n", pin->ops->read(adap));
736 seq_printf(file, "irq failed: %d\n", pin->enable_irq_failed);
737 if (pin->timer_100ms_overruns) {
738 seq_printf(file, "timer overruns > 100ms: %u of %u\n",
739 pin->timer_100ms_overruns, pin->timer_cnt);
740 seq_printf(file, "timer overruns > 300ms: %u of %u\n",
741 pin->timer_300ms_overruns, pin->timer_cnt);
742 seq_printf(file, "max timer overrun: %u usecs\n",
743 pin->timer_max_overrun);
744 seq_printf(file, "avg timer overrun: %u usecs\n",
745 pin->timer_sum_overrun / pin->timer_100ms_overruns);
746 }
747 pin->timer_cnt = 0;
748 pin->timer_100ms_overruns = 0;
749 pin->timer_300ms_overruns = 0;
750 pin->timer_max_overrun = 0;
751 pin->timer_sum_overrun = 0;
752 if (pin->ops->status)
753 pin->ops->status(adap, file);
754}
755
756static int cec_pin_adap_monitor_all_enable(struct cec_adapter *adap,
757 bool enable)
758{
759 struct cec_pin *pin = adap->pin;
760
761 pin->monitor_all = enable;
762 return 0;
763}
764
765static void cec_pin_adap_free(struct cec_adapter *adap)
766{
767 struct cec_pin *pin = adap->pin;
768
769 if (pin->ops->free)
770 pin->ops->free(adap);
771 adap->pin = NULL;
772 kfree(pin);
773}
774
775void cec_pin_changed(struct cec_adapter *adap, bool value)
776{
777 struct cec_pin *pin = adap->pin;
778
779 cec_pin_update(pin, value, false);
780 if (!value && (adap->is_configuring || adap->is_configured ||
781 adap->monitor_all_cnt))
782 atomic_set(&pin->work_irq_change, CEC_PIN_IRQ_DISABLE);
783}
784EXPORT_SYMBOL_GPL(cec_pin_changed);
785
786static const struct cec_adap_ops cec_pin_adap_ops = {
787 .adap_enable = cec_pin_adap_enable,
788 .adap_monitor_all_enable = cec_pin_adap_monitor_all_enable,
789 .adap_log_addr = cec_pin_adap_log_addr,
790 .adap_transmit = cec_pin_adap_transmit,
791 .adap_status = cec_pin_adap_status,
792 .adap_free = cec_pin_adap_free,
793};
794
795struct cec_adapter *cec_pin_allocate_adapter(const struct cec_pin_ops *pin_ops,
796 void *priv, const char *name, u32 caps)
797{
798 struct cec_adapter *adap;
799 struct cec_pin *pin = kzalloc(sizeof(*pin), GFP_KERNEL);
800
801 if (pin == NULL)
802 return ERR_PTR(-ENOMEM);
803 pin->ops = pin_ops;
804 hrtimer_init(&pin->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
805 pin->timer.function = cec_pin_timer;
806 init_waitqueue_head(&pin->kthread_waitq);
807
808 adap = cec_allocate_adapter(&cec_pin_adap_ops, priv, name,
809 caps | CEC_CAP_MONITOR_ALL | CEC_CAP_MONITOR_PIN,
810 CEC_MAX_LOG_ADDRS);
811
812 if (PTR_ERR_OR_ZERO(adap)) {
813 kfree(pin);
814 return adap;
815 }
816
817 adap->pin = pin;
818 pin->adap = adap;
819 cec_pin_update(pin, cec_pin_high(pin), true);
820 return adap;
821}
822EXPORT_SYMBOL_GPL(cec_pin_allocate_adapter);