1 // SPDX-License-Identifier: GPL-2.0
4 * Sony CXD2880 DVB-T2/T tuner + demodulator driver
7 * Copyright (C) 2016, 2017, 2018 Sony Semiconductor Solutions Corporation
10 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
12 #include <linux/spi/spi.h>
13 #include <linux/ktime.h>
15 #include <media/dvb_demux.h>
16 #include <media/dmxdev.h>
17 #include <media/dvb_frontend.h>
20 #define CXD2880_MAX_FILTER_SIZE 32
21 #define BURST_WRITE_MAX 128
22 #define MAX_TRANS_PKT 300
24 struct cxd2880_ts_buf_info
{
33 struct cxd2880_pid_config
{
38 struct cxd2880_pid_filter_config
{
40 struct cxd2880_pid_config pid_config
[CXD2880_MAX_FILTER_SIZE
];
43 struct cxd2880_dvb_spi
{
44 struct dvb_frontend dvb_fe
;
45 struct dvb_adapter adapter
;
46 struct dvb_demux demux
;
48 struct dmx_frontend dmx_fe
;
49 struct task_struct
*cxd2880_ts_read_thread
;
50 struct spi_device
*spi
;
51 struct mutex spi_mutex
; /* For SPI access exclusive control */
53 int all_pid_feed_count
;
55 struct cxd2880_pid_filter_config filter_config
;
58 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
60 static int cxd2880_write_spi(struct spi_device
*spi
, u8
*data
, u32 size
)
62 struct spi_message msg
;
63 struct spi_transfer tx
= {};
66 pr_err("invalid arg\n");
73 spi_message_init(&msg
);
74 spi_message_add_tail(&tx
, &msg
);
76 return spi_sync(spi
, &msg
);
79 static int cxd2880_write_reg(struct spi_device
*spi
,
80 u8 sub_address
, const u8
*data
, u32 size
)
82 u8 send_data
[BURST_WRITE_MAX
+ 4];
83 const u8
*write_data_top
= NULL
;
87 pr_err("invalid arg\n");
90 if (size
> BURST_WRITE_MAX
|| size
> U8_MAX
) {
91 pr_err("data size > WRITE_MAX\n");
95 if (sub_address
+ size
> 0x100) {
96 pr_err("out of range\n");
101 write_data_top
= data
;
103 send_data
[1] = sub_address
;
104 send_data
[2] = (u8
)size
;
106 memcpy(&send_data
[3], write_data_top
, send_data
[2]);
108 ret
= cxd2880_write_spi(spi
, send_data
, send_data
[2] + 3);
110 pr_err("write spi failed %d\n", ret
);
115 static int cxd2880_spi_read_ts(struct spi_device
*spi
,
121 struct spi_message message
;
122 struct spi_transfer transfer
[2] = {};
124 if (!spi
|| !read_data
|| !packet_num
) {
125 pr_err("invalid arg\n");
128 if (packet_num
> 0xffff) {
129 pr_err("packet num > 0xffff\n");
134 data
[1] = packet_num
>> 8;
135 data
[2] = packet_num
;
137 spi_message_init(&message
);
140 transfer
[0].tx_buf
= data
;
141 spi_message_add_tail(&transfer
[0], &message
);
142 transfer
[1].len
= packet_num
* 188;
143 transfer
[1].rx_buf
= read_data
;
144 spi_message_add_tail(&transfer
[1], &message
);
146 ret
= spi_sync(spi
, &message
);
148 pr_err("spi_write_then_read failed\n");
153 static int cxd2880_spi_read_ts_buffer_info(struct spi_device
*spi
,
154 struct cxd2880_ts_buf_info
*info
)
161 pr_err("invalid arg\n");
165 ret
= spi_write_then_read(spi
, &send_data
, 1,
166 recv_data
, sizeof(recv_data
));
168 pr_err("spi_write_then_read failed\n");
170 info
->read_ready
= (recv_data
[0] & 0x80) ? 1 : 0;
171 info
->almost_full
= (recv_data
[0] & 0x40) ? 1 : 0;
172 info
->almost_empty
= (recv_data
[0] & 0x20) ? 1 : 0;
173 info
->overflow
= (recv_data
[0] & 0x10) ? 1 : 0;
174 info
->underflow
= (recv_data
[0] & 0x08) ? 1 : 0;
175 info
->pkt_num
= ((recv_data
[0] & 0x07) << 8) | recv_data
[1];
180 static int cxd2880_spi_clear_ts_buffer(struct spi_device
*spi
)
185 ret
= cxd2880_write_spi(spi
, &data
, 1);
188 pr_err("write spi failed\n");
193 static int cxd2880_set_pid_filter(struct spi_device
*spi
,
194 struct cxd2880_pid_filter_config
*cfg
)
202 pr_err("invalid arg\n");
207 ret
= cxd2880_write_reg(spi
, 0x00, &data
[0], 1);
212 ret
= cxd2880_write_reg(spi
, 0x50, &data
[0], 1);
214 data
[0] = cfg
->is_negative
? 0x01 : 0x00;
216 for (i
= 0; i
< CXD2880_MAX_FILTER_SIZE
; i
++) {
217 pid
= cfg
->pid_config
[i
].pid
;
218 if (cfg
->pid_config
[i
].is_enable
) {
219 data
[1 + (i
* 2)] = (pid
>> 8) | 0x20;
220 data
[2 + (i
* 2)] = pid
& 0xff;
222 data
[1 + (i
* 2)] = 0x00;
223 data
[2 + (i
* 2)] = 0x00;
226 ret
= cxd2880_write_reg(spi
, 0x50, data
, 65);
232 static int cxd2880_update_pid_filter(struct cxd2880_dvb_spi
*dvb_spi
,
233 struct cxd2880_pid_filter_config
*cfg
,
234 bool is_all_pid_filter
)
238 if (!dvb_spi
|| !cfg
) {
239 pr_err("invalid arg.\n");
243 mutex_lock(&dvb_spi
->spi_mutex
);
244 if (is_all_pid_filter
) {
245 struct cxd2880_pid_filter_config tmpcfg
;
247 memset(&tmpcfg
, 0, sizeof(tmpcfg
));
248 tmpcfg
.is_negative
= 1;
249 tmpcfg
.pid_config
[0].is_enable
= 1;
250 tmpcfg
.pid_config
[0].pid
= 0x1fff;
252 ret
= cxd2880_set_pid_filter(dvb_spi
->spi
, &tmpcfg
);
254 ret
= cxd2880_set_pid_filter(dvb_spi
->spi
, cfg
);
256 mutex_unlock(&dvb_spi
->spi_mutex
);
259 pr_err("set_pid_filter failed\n");
264 static int cxd2880_ts_read(void *arg
)
266 struct cxd2880_dvb_spi
*dvb_spi
= NULL
;
267 struct cxd2880_ts_buf_info info
;
274 pr_err("invalid arg\n");
278 ret
= cxd2880_spi_clear_ts_buffer(dvb_spi
->spi
);
280 pr_err("set_clear_ts_buffer failed\n");
285 while (!kthread_should_stop()) {
286 ret
= cxd2880_spi_read_ts_buffer_info(dvb_spi
->spi
,
289 pr_err("spi_read_ts_buffer_info error\n");
293 if (info
.pkt_num
> MAX_TRANS_PKT
) {
294 for (i
= 0; i
< info
.pkt_num
/ MAX_TRANS_PKT
; i
++) {
295 cxd2880_spi_read_ts(dvb_spi
->spi
,
298 dvb_dmx_swfilter(&dvb_spi
->demux
,
300 MAX_TRANS_PKT
* 188);
303 } else if ((info
.pkt_num
> 0) &&
304 (ktime_to_ms(ktime_sub(ktime_get(), start
)) >= 500)) {
305 cxd2880_spi_read_ts(dvb_spi
->spi
,
308 dvb_dmx_swfilter(&dvb_spi
->demux
,
313 usleep_range(10000, 11000);
320 static int cxd2880_start_feed(struct dvb_demux_feed
*feed
)
324 struct dvb_demux
*demux
= NULL
;
325 struct cxd2880_dvb_spi
*dvb_spi
= NULL
;
328 pr_err("invalid arg\n");
334 pr_err("feed->demux is NULL\n");
337 dvb_spi
= demux
->priv
;
339 if (dvb_spi
->feed_count
== CXD2880_MAX_FILTER_SIZE
) {
340 pr_err("Exceeded maximum PID count (32).");
341 pr_err("Selected PID cannot be enabled.\n");
345 if (feed
->pid
== 0x2000) {
346 if (dvb_spi
->all_pid_feed_count
== 0) {
347 ret
= cxd2880_update_pid_filter(dvb_spi
,
348 &dvb_spi
->filter_config
,
351 pr_err("update pid filter failed\n");
355 dvb_spi
->all_pid_feed_count
++;
357 pr_debug("all PID feed (count = %d)\n",
358 dvb_spi
->all_pid_feed_count
);
360 struct cxd2880_pid_filter_config cfgtmp
;
362 cfgtmp
= dvb_spi
->filter_config
;
364 for (i
= 0; i
< CXD2880_MAX_FILTER_SIZE
; i
++) {
365 if (cfgtmp
.pid_config
[i
].is_enable
== 0) {
366 cfgtmp
.pid_config
[i
].is_enable
= 1;
367 cfgtmp
.pid_config
[i
].pid
= feed
->pid
;
368 pr_debug("store PID %d to #%d\n",
373 if (i
== CXD2880_MAX_FILTER_SIZE
) {
374 pr_err("PID filter is full.\n");
377 if (!dvb_spi
->all_pid_feed_count
)
378 ret
= cxd2880_update_pid_filter(dvb_spi
,
384 dvb_spi
->filter_config
= cfgtmp
;
387 if (dvb_spi
->feed_count
== 0) {
389 kmalloc(MAX_TRANS_PKT
* 188,
390 GFP_KERNEL
| GFP_DMA
);
391 if (!dvb_spi
->ts_buf
) {
392 pr_err("ts buffer allocate failed\n");
393 memset(&dvb_spi
->filter_config
, 0,
394 sizeof(dvb_spi
->filter_config
));
395 dvb_spi
->all_pid_feed_count
= 0;
398 dvb_spi
->cxd2880_ts_read_thread
= kthread_run(cxd2880_ts_read
,
401 if (IS_ERR(dvb_spi
->cxd2880_ts_read_thread
)) {
402 pr_err("kthread_run failed/\n");
403 kfree(dvb_spi
->ts_buf
);
404 dvb_spi
->ts_buf
= NULL
;
405 memset(&dvb_spi
->filter_config
, 0,
406 sizeof(dvb_spi
->filter_config
));
407 dvb_spi
->all_pid_feed_count
= 0;
408 return PTR_ERR(dvb_spi
->cxd2880_ts_read_thread
);
412 dvb_spi
->feed_count
++;
414 pr_debug("start feed (count %d)\n", dvb_spi
->feed_count
);
418 static int cxd2880_stop_feed(struct dvb_demux_feed
*feed
)
422 struct dvb_demux
*demux
= NULL
;
423 struct cxd2880_dvb_spi
*dvb_spi
= NULL
;
426 pr_err("invalid arg\n");
432 pr_err("feed->demux is NULL\n");
435 dvb_spi
= demux
->priv
;
437 if (!dvb_spi
->feed_count
) {
438 pr_err("no feed is started\n");
442 if (feed
->pid
== 0x2000) {
445 * Number of 0x2000 feed request was stored
446 * in dvb_spi->all_pid_feed_count.
448 if (dvb_spi
->all_pid_feed_count
<= 0) {
449 pr_err("PID %d not found.\n", feed
->pid
);
452 dvb_spi
->all_pid_feed_count
--;
454 struct cxd2880_pid_filter_config cfgtmp
;
456 cfgtmp
= dvb_spi
->filter_config
;
458 for (i
= 0; i
< CXD2880_MAX_FILTER_SIZE
; i
++) {
459 if (feed
->pid
== cfgtmp
.pid_config
[i
].pid
&&
460 cfgtmp
.pid_config
[i
].is_enable
!= 0) {
461 cfgtmp
.pid_config
[i
].is_enable
= 0;
462 cfgtmp
.pid_config
[i
].pid
= 0;
463 pr_debug("removed PID %d from #%d\n",
468 dvb_spi
->filter_config
= cfgtmp
;
470 if (i
== CXD2880_MAX_FILTER_SIZE
) {
471 pr_err("PID %d not found\n", feed
->pid
);
476 ret
= cxd2880_update_pid_filter(dvb_spi
,
477 &dvb_spi
->filter_config
,
478 dvb_spi
->all_pid_feed_count
> 0);
479 dvb_spi
->feed_count
--;
481 if (dvb_spi
->feed_count
== 0) {
484 ret_stop
= kthread_stop(dvb_spi
->cxd2880_ts_read_thread
);
486 pr_err("'kthread_stop failed. (%d)\n", ret_stop
);
489 kfree(dvb_spi
->ts_buf
);
490 dvb_spi
->ts_buf
= NULL
;
493 pr_debug("stop feed ok.(count %d)\n", dvb_spi
->feed_count
);
498 static const struct of_device_id cxd2880_spi_of_match
[] = {
499 { .compatible
= "sony,cxd2880" },
503 MODULE_DEVICE_TABLE(of
, cxd2880_spi_of_match
);
506 cxd2880_spi_probe(struct spi_device
*spi
)
509 struct cxd2880_dvb_spi
*dvb_spi
= NULL
;
510 struct cxd2880_config config
;
513 pr_err("invalid arg.\n");
517 dvb_spi
= kzalloc(sizeof(struct cxd2880_dvb_spi
), GFP_KERNEL
);
522 mutex_init(&dvb_spi
->spi_mutex
);
523 dev_set_drvdata(&spi
->dev
, dvb_spi
);
525 config
.spi_mutex
= &dvb_spi
->spi_mutex
;
527 ret
= dvb_register_adapter(&dvb_spi
->adapter
,
533 pr_err("dvb_register_adapter() failed\n");
537 if (!dvb_attach(cxd2880_attach
, &dvb_spi
->dvb_fe
, &config
)) {
538 pr_err("cxd2880_attach failed\n");
543 ret
= dvb_register_frontend(&dvb_spi
->adapter
,
546 pr_err("dvb_register_frontend() failed\n");
550 dvb_spi
->demux
.dmx
.capabilities
= DMX_TS_FILTERING
;
551 dvb_spi
->demux
.priv
= dvb_spi
;
552 dvb_spi
->demux
.filternum
= CXD2880_MAX_FILTER_SIZE
;
553 dvb_spi
->demux
.feednum
= CXD2880_MAX_FILTER_SIZE
;
554 dvb_spi
->demux
.start_feed
= cxd2880_start_feed
;
555 dvb_spi
->demux
.stop_feed
= cxd2880_stop_feed
;
557 ret
= dvb_dmx_init(&dvb_spi
->demux
);
559 pr_err("dvb_dmx_init() failed\n");
563 dvb_spi
->dmxdev
.filternum
= CXD2880_MAX_FILTER_SIZE
;
564 dvb_spi
->dmxdev
.demux
= &dvb_spi
->demux
.dmx
;
565 dvb_spi
->dmxdev
.capabilities
= 0;
566 ret
= dvb_dmxdev_init(&dvb_spi
->dmxdev
,
569 pr_err("dvb_dmxdev_init() failed\n");
573 dvb_spi
->dmx_fe
.source
= DMX_FRONTEND_0
;
574 ret
= dvb_spi
->demux
.dmx
.add_frontend(&dvb_spi
->demux
.dmx
,
577 pr_err("add_frontend() failed\n");
581 ret
= dvb_spi
->demux
.dmx
.connect_frontend(&dvb_spi
->demux
.dmx
,
584 pr_err("dvb_register_frontend() failed\n");
588 pr_info("Sony CXD2880 has successfully attached.\n");
593 dvb_spi
->demux
.dmx
.remove_frontend(&dvb_spi
->demux
.dmx
,
596 dvb_dmxdev_release(&dvb_spi
->dmxdev
);
598 dvb_dmx_release(&dvb_spi
->demux
);
600 dvb_unregister_frontend(&dvb_spi
->dvb_fe
);
602 dvb_frontend_detach(&dvb_spi
->dvb_fe
);
604 dvb_unregister_adapter(&dvb_spi
->adapter
);
611 cxd2880_spi_remove(struct spi_device
*spi
)
613 struct cxd2880_dvb_spi
*dvb_spi
;
616 pr_err("invalid arg\n");
620 dvb_spi
= dev_get_drvdata(&spi
->dev
);
626 dvb_spi
->demux
.dmx
.remove_frontend(&dvb_spi
->demux
.dmx
,
628 dvb_dmxdev_release(&dvb_spi
->dmxdev
);
629 dvb_dmx_release(&dvb_spi
->demux
);
630 dvb_unregister_frontend(&dvb_spi
->dvb_fe
);
631 dvb_frontend_detach(&dvb_spi
->dvb_fe
);
632 dvb_unregister_adapter(&dvb_spi
->adapter
);
635 pr_info("cxd2880_spi remove ok.\n");
640 static const struct spi_device_id cxd2880_spi_id
[] = {
644 MODULE_DEVICE_TABLE(spi
, cxd2880_spi_id
);
646 static struct spi_driver cxd2880_spi_driver
= {
649 .of_match_table
= cxd2880_spi_of_match
,
651 .id_table
= cxd2880_spi_id
,
652 .probe
= cxd2880_spi_probe
,
653 .remove
= cxd2880_spi_remove
,
655 module_spi_driver(cxd2880_spi_driver
);
657 MODULE_DESCRIPTION("Sony CXD2880 DVB-T2/T tuner + demod driver SPI adapter");
658 MODULE_AUTHOR("Sony Semiconductor Solutions Corporation");
659 MODULE_LICENSE("GPL v2");