hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2021 Rockchip Electronics Co. Ltd.
 *
 * Author: Shunqing Chen <csq@rock-chips.com>
 */
 
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/sched.h>
#include <linux/slab.h>
 
#include <media/cec.h>
#include <media/cec-notifier.h>
 
#include "rk_hdmirx.h"
#include "rk_hdmirx_cec.h"
 
static void hdmirx_cec_write(struct hdmirx_cec *cec, int reg, u32 val)
{
   cec->ops->write(cec->hdmirx, reg, val);
}
 
static u32 hdmirx_cec_read(struct hdmirx_cec *cec, int reg)
{
   return cec->ops->read(cec->hdmirx, reg);
}
 
static void hdmirx_cec_update_bits(struct hdmirx_cec *cec, int reg, u32 mask,
                  u32 data)
{
   u32 val = hdmirx_cec_read(cec, reg) & ~mask;
 
   val |= (data & mask);
   hdmirx_cec_write(cec, reg, val);
}
 
static int hdmirx_cec_log_addr(struct cec_adapter *adap, u8 logical_addr)
{
   struct hdmirx_cec *cec = cec_get_drvdata(adap);
 
   if (logical_addr == CEC_LOG_ADDR_INVALID)
       cec->addresses = 0;
   else
       cec->addresses |= BIT(logical_addr) | BIT(15);
 
   hdmirx_cec_write(cec, CEC_ADDR, cec->addresses);
 
   return 0;
}
 
static int hdmirx_cec_transmit(struct cec_adapter *adap, u8 attempts,
                  u32 signal_free_time, struct cec_msg *msg)
{
   struct hdmirx_cec *cec = cec_get_drvdata(adap);
   u32 data[4] = {0};
   int i, data_len, msg_len;
 
   msg_len = msg->len;
   if (msg->len > 16)
       msg_len = 16;
   if (msg_len <= 0)
       return 0;
 
   hdmirx_cec_write(cec, CEC_TX_COUNT, msg_len - 1);
   for (i = 0; i < msg_len; i++)
       data[i / 4] |= msg->msg[i] << (i % 4) * 8;
 
   data_len = (msg_len + 3) / 4;
   for (i = 0; i < data_len; i++)
       hdmirx_cec_write(cec, CEC_TX_DATA3_0 + i * 4, data[i]);
 
   hdmirx_cec_write(cec, CEC_TX_CONTROL, 0x1);
 
   return 0;
}
 
static irqreturn_t hdmirx_cec_hardirq(int irq, void *data)
{
   struct cec_adapter *adap = data;
   struct hdmirx_cec *cec = cec_get_drvdata(adap);
   u32 stat = hdmirx_cec_read(cec, CEC_INT_STATUS);
   irqreturn_t ret = IRQ_HANDLED;
   u32 val;
 
   if (stat == 0)
       return IRQ_NONE;
 
   hdmirx_cec_write(cec, CEC_INT_CLEAR, stat);
 
   if (stat & CECTX_LINE_ERR) {
       cec->tx_status = CEC_TX_STATUS_ERROR;
       cec->tx_done = true;
       ret = IRQ_WAKE_THREAD;
   } else if (stat & CECTX_DONE) {
       cec->tx_status = CEC_TX_STATUS_OK;
       cec->tx_done = true;
       ret = IRQ_WAKE_THREAD;
   } else if (stat & CECTX_NACK) {
       cec->tx_status = CEC_TX_STATUS_NACK;
       cec->tx_done = true;
       ret = IRQ_WAKE_THREAD;
   }
 
   if (stat & CECRX_EOM) {
       unsigned int len, i;
 
       val = hdmirx_cec_read(cec, CEC_RX_COUNT_STATUS);
       /* rxbuffer locked status */
       if ((val & 0x80))
           return ret;
 
       len = (val & 0xf) + 1;
       if (len > sizeof(cec->rx_msg.msg))
           len = sizeof(cec->rx_msg.msg);
 
       for (i = 0; i < len; i++) {
           if (i % 4 == 0)
               val = hdmirx_cec_read(cec, CEC_RX_DATA3_0 + i / 4 * 4);
           cec->rx_msg.msg[i] = (val >> ((i % 4) * 8)) & 0xff;
       }
 
       cec->rx_msg.len = len;
       smp_wmb(); /* receive RX msg */
       cec->rx_done = true;
       hdmirx_cec_write(cec, CEC_LOCK_CONTROL, 0x1);
 
       ret = IRQ_WAKE_THREAD;
   }
 
   return ret;
}
 
static irqreturn_t hdmirx_cec_thread(int irq, void *data)
{
   struct cec_adapter *adap = data;
   struct hdmirx_cec *cec = cec_get_drvdata(adap);
 
   if (cec->tx_done) {
       cec->tx_done = false;
       cec_transmit_attempt_done(adap, cec->tx_status);
   }
   if (cec->rx_done) {
       cec->rx_done = false;
       smp_rmb(); /* RX msg has been received */
       cec_received_msg(adap, &cec->rx_msg);
   }
 
   return IRQ_HANDLED;
}
 
static int hdmirx_cec_enable(struct cec_adapter *adap, bool enable)
{
   struct hdmirx_cec *cec = cec_get_drvdata(adap);
 
   if (!enable) {
       hdmirx_cec_write(cec, CEC_INT_MASK_N, 0);
       hdmirx_cec_write(cec, CEC_INT_CLEAR, 0);
       if (cec->ops->disable)
           cec->ops->disable(cec->hdmirx);
   } else {
       unsigned int irqs;
 
       hdmirx_cec_log_addr(cec->adap, CEC_LOG_ADDR_INVALID);
       if (cec->ops->enable)
           cec->ops->enable(cec->hdmirx);
       hdmirx_cec_update_bits(cec, GLOBAL_SWENABLE, CEC_ENABLE, CEC_ENABLE);
 
       irqs = CECTX_LINE_ERR | CECTX_NACK | CECRX_EOM | CECTX_DONE;
       hdmirx_cec_write(cec, CEC_INT_MASK_N, irqs);
   }
 
   return 0;
}
 
static const struct cec_adap_ops hdmirx_cec_ops = {
   .adap_enable = hdmirx_cec_enable,
   .adap_log_addr = hdmirx_cec_log_addr,
   .adap_transmit = hdmirx_cec_transmit,
};
 
static void hdmirx_cec_del(void *data)
{
   struct hdmirx_cec *cec = data;
 
   cec_delete_adapter(cec->adap);
}
 
struct hdmirx_cec *rk_hdmirx_cec_register(struct hdmirx_cec_data *data)
{
   struct hdmirx_cec *cec;
   int ret;
 
   unsigned int irqs;
 
   if (!data)
       return NULL;
 
   /*
    * Our device is just a convenience - we want to link to the real
    * hardware device here, so that userspace can see the association
    * between the HDMI hardware and its associated CEC chardev.
    */
   cec = devm_kzalloc(data->dev, sizeof(*cec), GFP_KERNEL);
   if (!cec)
       return NULL;
 
   cec->dev = data->dev;
   cec->irq = data->irq;
   cec->ops = data->ops;
   cec->hdmirx = data->hdmirx;
   cec->edid = (struct edid *)data->edid;
 
   hdmirx_cec_update_bits(cec, GLOBAL_SWENABLE, CEC_ENABLE, CEC_ENABLE);
   hdmirx_cec_update_bits(cec, CEC_CONFIG, RX_AUTO_DRIVE_ACKNOWLEDGE,
                  RX_AUTO_DRIVE_ACKNOWLEDGE);
 
   hdmirx_cec_write(cec, CEC_TX_COUNT, 0);
   hdmirx_cec_write(cec, CEC_INT_MASK_N, 0);
   hdmirx_cec_write(cec, CEC_INT_CLEAR, ~0);
 
   cec->adap = cec_allocate_adapter(&hdmirx_cec_ops, cec, "rk-hdmirx",
                    CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT |
                    CEC_CAP_RC | CEC_CAP_PASSTHROUGH,
                    CEC_MAX_LOG_ADDRS);
   if (IS_ERR(cec->adap)) {
       dev_err(cec->dev, "cec adap allocate failed!\n");
       return NULL;
   }
 
   /* override the module pointer */
   cec->adap->owner = THIS_MODULE;
 
   ret = devm_add_action(cec->dev, hdmirx_cec_del, cec);
   if (ret) {
       cec_delete_adapter(cec->adap);
       return NULL;
   }
 
   cec->notify = cec_notifier_cec_adap_register(cec->dev,
                            NULL, cec->adap);
   if (!cec->notify) {
       dev_err(cec->dev, "cec notify register failed!\n");
       return NULL;
   }
 
   ret = cec_register_adapter(cec->adap, cec->dev);
   if (ret < 0) {
       dev_err(cec->dev, "cec register adapter failed!\n");
       cec_notifier_cec_adap_unregister(cec->notify, cec->adap);
       return NULL;
   }
 
   /* The TV functionality can only map to physical address 0 */
   cec_s_phys_addr(cec->adap, 0, false);
 
   ret = devm_request_threaded_irq(cec->dev, cec->irq,
                   hdmirx_cec_hardirq,
                   hdmirx_cec_thread, IRQF_ONESHOT,
                   "rk_hdmirx_cec", cec->adap);
   if (ret) {
       dev_err(cec->dev, "cec irq request failed!\n");
       cec_notifier_cec_adap_unregister(cec->notify, cec->adap);
       dev_err(cec->dev, "request cec irq thread failed!\n");
       return NULL;
   }
 
   irqs = CECTX_LINE_ERR | CECTX_NACK | CECRX_EOM | CECTX_DONE;
   hdmirx_cec_write(cec, CEC_INT_MASK_N, irqs);
 
   /*
    * CEC documentation says we must not call cec_delete_adapter
    * after a successful call to cec_register_adapter().
    */
   devm_remove_action(cec->dev, hdmirx_cec_del, cec);
 
   return cec;
}
 
void rk_hdmirx_cec_unregister(struct hdmirx_cec *cec)
{
   if (!cec)
       return;
 
   cec_notifier_cec_adap_unregister(cec->notify, cec->adap);
   cec_unregister_adapter(cec->adap);
}