0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
0011
0012 #include <linux/spi/spi.h>
0013 #include <linux/regulator/consumer.h>
0014 #include <linux/ktime.h>
0015
0016 #include <media/dvb_demux.h>
0017 #include <media/dmxdev.h>
0018 #include <media/dvb_frontend.h>
0019 #include "cxd2880.h"
0020
0021 #define CXD2880_MAX_FILTER_SIZE 32
0022 #define BURST_WRITE_MAX 128
0023 #define MAX_TRANS_PKT 300
0024
0025 struct cxd2880_ts_buf_info {
0026 u8 read_ready:1;
0027 u8 almost_full:1;
0028 u8 almost_empty:1;
0029 u8 overflow:1;
0030 u8 underflow:1;
0031 u16 pkt_num;
0032 };
0033
0034 struct cxd2880_pid_config {
0035 u8 is_enable;
0036 u16 pid;
0037 };
0038
0039 struct cxd2880_pid_filter_config {
0040 u8 is_negative;
0041 struct cxd2880_pid_config pid_config[CXD2880_MAX_FILTER_SIZE];
0042 };
0043
0044 struct cxd2880_dvb_spi {
0045 struct dvb_frontend dvb_fe;
0046 struct dvb_adapter adapter;
0047 struct dvb_demux demux;
0048 struct dmxdev dmxdev;
0049 struct dmx_frontend dmx_fe;
0050 struct task_struct *cxd2880_ts_read_thread;
0051 struct spi_device *spi;
0052 struct mutex spi_mutex;
0053 int feed_count;
0054 int all_pid_feed_count;
0055 struct regulator *vcc_supply;
0056 u8 *ts_buf;
0057 struct cxd2880_pid_filter_config filter_config;
0058 };
0059
0060 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
0061
0062 static int cxd2880_write_spi(struct spi_device *spi, u8 *data, u32 size)
0063 {
0064 struct spi_message msg;
0065 struct spi_transfer tx = {};
0066
0067 if (!spi || !data) {
0068 pr_err("invalid arg\n");
0069 return -EINVAL;
0070 }
0071
0072 tx.tx_buf = data;
0073 tx.len = size;
0074
0075 spi_message_init(&msg);
0076 spi_message_add_tail(&tx, &msg);
0077
0078 return spi_sync(spi, &msg);
0079 }
0080
0081 static int cxd2880_write_reg(struct spi_device *spi,
0082 u8 sub_address, const u8 *data, u32 size)
0083 {
0084 u8 send_data[BURST_WRITE_MAX + 4];
0085 const u8 *write_data_top = NULL;
0086 int ret = 0;
0087
0088 if (!spi || !data) {
0089 pr_err("invalid arg\n");
0090 return -EINVAL;
0091 }
0092 if (size > BURST_WRITE_MAX || size > U8_MAX) {
0093 pr_err("data size > WRITE_MAX\n");
0094 return -EINVAL;
0095 }
0096
0097 if (sub_address + size > 0x100) {
0098 pr_err("out of range\n");
0099 return -EINVAL;
0100 }
0101
0102 send_data[0] = 0x0e;
0103 write_data_top = data;
0104
0105 send_data[1] = sub_address;
0106 send_data[2] = (u8)size;
0107
0108 memcpy(&send_data[3], write_data_top, send_data[2]);
0109
0110 ret = cxd2880_write_spi(spi, send_data, send_data[2] + 3);
0111 if (ret)
0112 pr_err("write spi failed %d\n", ret);
0113
0114 return ret;
0115 }
0116
0117 static int cxd2880_spi_read_ts(struct spi_device *spi,
0118 u8 *read_data,
0119 u32 packet_num)
0120 {
0121 int ret;
0122 u8 data[3];
0123 struct spi_message message;
0124 struct spi_transfer transfer[2] = {};
0125
0126 if (!spi || !read_data || !packet_num) {
0127 pr_err("invalid arg\n");
0128 return -EINVAL;
0129 }
0130 if (packet_num > 0xffff) {
0131 pr_err("packet num > 0xffff\n");
0132 return -EINVAL;
0133 }
0134
0135 data[0] = 0x10;
0136 data[1] = packet_num >> 8;
0137 data[2] = packet_num;
0138
0139 spi_message_init(&message);
0140
0141 transfer[0].len = 3;
0142 transfer[0].tx_buf = data;
0143 spi_message_add_tail(&transfer[0], &message);
0144 transfer[1].len = packet_num * 188;
0145 transfer[1].rx_buf = read_data;
0146 spi_message_add_tail(&transfer[1], &message);
0147
0148 ret = spi_sync(spi, &message);
0149 if (ret)
0150 pr_err("spi_sync failed\n");
0151
0152 return ret;
0153 }
0154
0155 static int cxd2880_spi_read_ts_buffer_info(struct spi_device *spi,
0156 struct cxd2880_ts_buf_info *info)
0157 {
0158 u8 send_data = 0x20;
0159 u8 recv_data[2];
0160 int ret;
0161
0162 if (!spi || !info) {
0163 pr_err("invalid arg\n");
0164 return -EINVAL;
0165 }
0166
0167 ret = spi_write_then_read(spi, &send_data, 1,
0168 recv_data, sizeof(recv_data));
0169 if (ret)
0170 pr_err("spi_write_then_read failed\n");
0171
0172 info->read_ready = (recv_data[0] & 0x80) ? 1 : 0;
0173 info->almost_full = (recv_data[0] & 0x40) ? 1 : 0;
0174 info->almost_empty = (recv_data[0] & 0x20) ? 1 : 0;
0175 info->overflow = (recv_data[0] & 0x10) ? 1 : 0;
0176 info->underflow = (recv_data[0] & 0x08) ? 1 : 0;
0177 info->pkt_num = ((recv_data[0] & 0x07) << 8) | recv_data[1];
0178
0179 return ret;
0180 }
0181
0182 static int cxd2880_spi_clear_ts_buffer(struct spi_device *spi)
0183 {
0184 u8 data = 0x03;
0185 int ret;
0186
0187 ret = cxd2880_write_spi(spi, &data, 1);
0188
0189 if (ret)
0190 pr_err("write spi failed\n");
0191
0192 return ret;
0193 }
0194
0195 static int cxd2880_set_pid_filter(struct spi_device *spi,
0196 struct cxd2880_pid_filter_config *cfg)
0197 {
0198 u8 data[65];
0199 int i;
0200 u16 pid = 0;
0201 int ret;
0202
0203 if (!spi) {
0204 pr_err("invalid arg\n");
0205 return -EINVAL;
0206 }
0207
0208 data[0] = 0x00;
0209 ret = cxd2880_write_reg(spi, 0x00, &data[0], 1);
0210 if (ret)
0211 return ret;
0212 if (!cfg) {
0213 data[0] = 0x02;
0214 ret = cxd2880_write_reg(spi, 0x50, &data[0], 1);
0215 } else {
0216 data[0] = cfg->is_negative ? 0x01 : 0x00;
0217
0218 for (i = 0; i < CXD2880_MAX_FILTER_SIZE; i++) {
0219 pid = cfg->pid_config[i].pid;
0220 if (cfg->pid_config[i].is_enable) {
0221 data[1 + (i * 2)] = (pid >> 8) | 0x20;
0222 data[2 + (i * 2)] = pid & 0xff;
0223 } else {
0224 data[1 + (i * 2)] = 0x00;
0225 data[2 + (i * 2)] = 0x00;
0226 }
0227 }
0228 ret = cxd2880_write_reg(spi, 0x50, data, 65);
0229 }
0230
0231 return ret;
0232 }
0233
0234 static int cxd2880_update_pid_filter(struct cxd2880_dvb_spi *dvb_spi,
0235 struct cxd2880_pid_filter_config *cfg,
0236 bool is_all_pid_filter)
0237 {
0238 int ret;
0239
0240 if (!dvb_spi || !cfg) {
0241 pr_err("invalid arg.\n");
0242 return -EINVAL;
0243 }
0244
0245 mutex_lock(&dvb_spi->spi_mutex);
0246 if (is_all_pid_filter) {
0247 struct cxd2880_pid_filter_config tmpcfg;
0248
0249 memset(&tmpcfg, 0, sizeof(tmpcfg));
0250 tmpcfg.is_negative = 1;
0251 tmpcfg.pid_config[0].is_enable = 1;
0252 tmpcfg.pid_config[0].pid = 0x1fff;
0253
0254 ret = cxd2880_set_pid_filter(dvb_spi->spi, &tmpcfg);
0255 } else {
0256 ret = cxd2880_set_pid_filter(dvb_spi->spi, cfg);
0257 }
0258 mutex_unlock(&dvb_spi->spi_mutex);
0259
0260 if (ret)
0261 pr_err("set_pid_filter failed\n");
0262
0263 return ret;
0264 }
0265
0266 static int cxd2880_ts_read(void *arg)
0267 {
0268 struct cxd2880_dvb_spi *dvb_spi = NULL;
0269 struct cxd2880_ts_buf_info info;
0270 ktime_t start;
0271 u32 i;
0272 int ret;
0273
0274 dvb_spi = arg;
0275 if (!dvb_spi) {
0276 pr_err("invalid arg\n");
0277 return -EINVAL;
0278 }
0279
0280 ret = cxd2880_spi_clear_ts_buffer(dvb_spi->spi);
0281 if (ret) {
0282 pr_err("set_clear_ts_buffer failed\n");
0283 return ret;
0284 }
0285
0286 start = ktime_get();
0287 while (!kthread_should_stop()) {
0288 ret = cxd2880_spi_read_ts_buffer_info(dvb_spi->spi,
0289 &info);
0290 if (ret) {
0291 pr_err("spi_read_ts_buffer_info error\n");
0292 return ret;
0293 }
0294
0295 if (info.pkt_num > MAX_TRANS_PKT) {
0296 for (i = 0; i < info.pkt_num / MAX_TRANS_PKT; i++) {
0297 cxd2880_spi_read_ts(dvb_spi->spi,
0298 dvb_spi->ts_buf,
0299 MAX_TRANS_PKT);
0300 dvb_dmx_swfilter(&dvb_spi->demux,
0301 dvb_spi->ts_buf,
0302 MAX_TRANS_PKT * 188);
0303 }
0304 start = ktime_get();
0305 } else if ((info.pkt_num > 0) &&
0306 (ktime_to_ms(ktime_sub(ktime_get(), start)) >= 500)) {
0307 cxd2880_spi_read_ts(dvb_spi->spi,
0308 dvb_spi->ts_buf,
0309 info.pkt_num);
0310 dvb_dmx_swfilter(&dvb_spi->demux,
0311 dvb_spi->ts_buf,
0312 info.pkt_num * 188);
0313 start = ktime_get();
0314 } else {
0315 usleep_range(10000, 11000);
0316 }
0317 }
0318
0319 return 0;
0320 }
0321
0322 static int cxd2880_start_feed(struct dvb_demux_feed *feed)
0323 {
0324 int ret = 0;
0325 int i = 0;
0326 struct dvb_demux *demux = NULL;
0327 struct cxd2880_dvb_spi *dvb_spi = NULL;
0328
0329 if (!feed) {
0330 pr_err("invalid arg\n");
0331 return -EINVAL;
0332 }
0333
0334 demux = feed->demux;
0335 if (!demux) {
0336 pr_err("feed->demux is NULL\n");
0337 return -EINVAL;
0338 }
0339 dvb_spi = demux->priv;
0340
0341 if (dvb_spi->feed_count == CXD2880_MAX_FILTER_SIZE) {
0342 pr_err("Exceeded maximum PID count (32).");
0343 pr_err("Selected PID cannot be enabled.\n");
0344 return -EINVAL;
0345 }
0346
0347 if (feed->pid == 0x2000) {
0348 if (dvb_spi->all_pid_feed_count == 0) {
0349 ret = cxd2880_update_pid_filter(dvb_spi,
0350 &dvb_spi->filter_config,
0351 true);
0352 if (ret) {
0353 pr_err("update pid filter failed\n");
0354 return ret;
0355 }
0356 }
0357 dvb_spi->all_pid_feed_count++;
0358
0359 pr_debug("all PID feed (count = %d)\n",
0360 dvb_spi->all_pid_feed_count);
0361 } else {
0362 struct cxd2880_pid_filter_config cfgtmp;
0363
0364 cfgtmp = dvb_spi->filter_config;
0365
0366 for (i = 0; i < CXD2880_MAX_FILTER_SIZE; i++) {
0367 if (cfgtmp.pid_config[i].is_enable == 0) {
0368 cfgtmp.pid_config[i].is_enable = 1;
0369 cfgtmp.pid_config[i].pid = feed->pid;
0370 pr_debug("store PID %d to #%d\n",
0371 feed->pid, i);
0372 break;
0373 }
0374 }
0375 if (i == CXD2880_MAX_FILTER_SIZE) {
0376 pr_err("PID filter is full.\n");
0377 return -EINVAL;
0378 }
0379 if (!dvb_spi->all_pid_feed_count)
0380 ret = cxd2880_update_pid_filter(dvb_spi,
0381 &cfgtmp,
0382 false);
0383 if (ret)
0384 return ret;
0385
0386 dvb_spi->filter_config = cfgtmp;
0387 }
0388
0389 if (dvb_spi->feed_count == 0) {
0390 dvb_spi->ts_buf =
0391 kmalloc(MAX_TRANS_PKT * 188,
0392 GFP_KERNEL | GFP_DMA);
0393 if (!dvb_spi->ts_buf) {
0394 pr_err("ts buffer allocate failed\n");
0395 memset(&dvb_spi->filter_config, 0,
0396 sizeof(dvb_spi->filter_config));
0397 dvb_spi->all_pid_feed_count = 0;
0398 return -ENOMEM;
0399 }
0400 dvb_spi->cxd2880_ts_read_thread = kthread_run(cxd2880_ts_read,
0401 dvb_spi,
0402 "cxd2880_ts_read");
0403 if (IS_ERR(dvb_spi->cxd2880_ts_read_thread)) {
0404 pr_err("kthread_run failed\n");
0405 kfree(dvb_spi->ts_buf);
0406 dvb_spi->ts_buf = NULL;
0407 memset(&dvb_spi->filter_config, 0,
0408 sizeof(dvb_spi->filter_config));
0409 dvb_spi->all_pid_feed_count = 0;
0410 return PTR_ERR(dvb_spi->cxd2880_ts_read_thread);
0411 }
0412 }
0413
0414 dvb_spi->feed_count++;
0415
0416 pr_debug("start feed (count %d)\n", dvb_spi->feed_count);
0417 return 0;
0418 }
0419
0420 static int cxd2880_stop_feed(struct dvb_demux_feed *feed)
0421 {
0422 int i = 0;
0423 int ret;
0424 struct dvb_demux *demux = NULL;
0425 struct cxd2880_dvb_spi *dvb_spi = NULL;
0426
0427 if (!feed) {
0428 pr_err("invalid arg\n");
0429 return -EINVAL;
0430 }
0431
0432 demux = feed->demux;
0433 if (!demux) {
0434 pr_err("feed->demux is NULL\n");
0435 return -EINVAL;
0436 }
0437 dvb_spi = demux->priv;
0438
0439 if (!dvb_spi->feed_count) {
0440 pr_err("no feed is started\n");
0441 return -EINVAL;
0442 }
0443
0444 if (feed->pid == 0x2000) {
0445
0446
0447
0448
0449
0450 if (dvb_spi->all_pid_feed_count <= 0) {
0451 pr_err("PID %d not found\n", feed->pid);
0452 return -EINVAL;
0453 }
0454 dvb_spi->all_pid_feed_count--;
0455 } else {
0456 struct cxd2880_pid_filter_config cfgtmp;
0457
0458 cfgtmp = dvb_spi->filter_config;
0459
0460 for (i = 0; i < CXD2880_MAX_FILTER_SIZE; i++) {
0461 if (feed->pid == cfgtmp.pid_config[i].pid &&
0462 cfgtmp.pid_config[i].is_enable != 0) {
0463 cfgtmp.pid_config[i].is_enable = 0;
0464 cfgtmp.pid_config[i].pid = 0;
0465 pr_debug("removed PID %d from #%d\n",
0466 feed->pid, i);
0467 break;
0468 }
0469 }
0470 dvb_spi->filter_config = cfgtmp;
0471
0472 if (i == CXD2880_MAX_FILTER_SIZE) {
0473 pr_err("PID %d not found\n", feed->pid);
0474 return -EINVAL;
0475 }
0476 }
0477
0478 ret = cxd2880_update_pid_filter(dvb_spi,
0479 &dvb_spi->filter_config,
0480 dvb_spi->all_pid_feed_count > 0);
0481 dvb_spi->feed_count--;
0482
0483 if (dvb_spi->feed_count == 0) {
0484 int ret_stop = 0;
0485
0486 ret_stop = kthread_stop(dvb_spi->cxd2880_ts_read_thread);
0487 if (ret_stop) {
0488 pr_err("kthread_stop failed. (%d)\n", ret_stop);
0489 ret = ret_stop;
0490 }
0491 kfree(dvb_spi->ts_buf);
0492 dvb_spi->ts_buf = NULL;
0493 }
0494
0495 pr_debug("stop feed ok.(count %d)\n", dvb_spi->feed_count);
0496
0497 return ret;
0498 }
0499
0500 static const struct of_device_id cxd2880_spi_of_match[] = {
0501 { .compatible = "sony,cxd2880" },
0502 { }
0503 };
0504
0505 MODULE_DEVICE_TABLE(of, cxd2880_spi_of_match);
0506
0507 static int
0508 cxd2880_spi_probe(struct spi_device *spi)
0509 {
0510 int ret;
0511 struct cxd2880_dvb_spi *dvb_spi = NULL;
0512 struct cxd2880_config config;
0513
0514 if (!spi) {
0515 pr_err("invalid arg\n");
0516 return -EINVAL;
0517 }
0518
0519 dvb_spi = kzalloc(sizeof(struct cxd2880_dvb_spi), GFP_KERNEL);
0520 if (!dvb_spi)
0521 return -ENOMEM;
0522
0523 dvb_spi->vcc_supply = devm_regulator_get_optional(&spi->dev, "vcc");
0524 if (IS_ERR(dvb_spi->vcc_supply)) {
0525 if (PTR_ERR(dvb_spi->vcc_supply) == -EPROBE_DEFER) {
0526 ret = -EPROBE_DEFER;
0527 goto fail_regulator;
0528 }
0529 dvb_spi->vcc_supply = NULL;
0530 } else {
0531 ret = regulator_enable(dvb_spi->vcc_supply);
0532 if (ret)
0533 goto fail_regulator;
0534 }
0535
0536 dvb_spi->spi = spi;
0537 mutex_init(&dvb_spi->spi_mutex);
0538 spi_set_drvdata(spi, dvb_spi);
0539 config.spi = spi;
0540 config.spi_mutex = &dvb_spi->spi_mutex;
0541
0542 ret = dvb_register_adapter(&dvb_spi->adapter,
0543 "CXD2880",
0544 THIS_MODULE,
0545 &spi->dev,
0546 adapter_nr);
0547 if (ret < 0) {
0548 pr_err("dvb_register_adapter() failed\n");
0549 goto fail_adapter;
0550 }
0551
0552 if (!dvb_attach(cxd2880_attach, &dvb_spi->dvb_fe, &config)) {
0553 pr_err("cxd2880_attach failed\n");
0554 ret = -ENODEV;
0555 goto fail_attach;
0556 }
0557
0558 ret = dvb_register_frontend(&dvb_spi->adapter,
0559 &dvb_spi->dvb_fe);
0560 if (ret < 0) {
0561 pr_err("dvb_register_frontend() failed\n");
0562 goto fail_frontend;
0563 }
0564
0565 dvb_spi->demux.dmx.capabilities = DMX_TS_FILTERING;
0566 dvb_spi->demux.priv = dvb_spi;
0567 dvb_spi->demux.filternum = CXD2880_MAX_FILTER_SIZE;
0568 dvb_spi->demux.feednum = CXD2880_MAX_FILTER_SIZE;
0569 dvb_spi->demux.start_feed = cxd2880_start_feed;
0570 dvb_spi->demux.stop_feed = cxd2880_stop_feed;
0571
0572 ret = dvb_dmx_init(&dvb_spi->demux);
0573 if (ret < 0) {
0574 pr_err("dvb_dmx_init() failed\n");
0575 goto fail_dmx;
0576 }
0577
0578 dvb_spi->dmxdev.filternum = CXD2880_MAX_FILTER_SIZE;
0579 dvb_spi->dmxdev.demux = &dvb_spi->demux.dmx;
0580 dvb_spi->dmxdev.capabilities = 0;
0581 ret = dvb_dmxdev_init(&dvb_spi->dmxdev,
0582 &dvb_spi->adapter);
0583 if (ret < 0) {
0584 pr_err("dvb_dmxdev_init() failed\n");
0585 goto fail_dmxdev;
0586 }
0587
0588 dvb_spi->dmx_fe.source = DMX_FRONTEND_0;
0589 ret = dvb_spi->demux.dmx.add_frontend(&dvb_spi->demux.dmx,
0590 &dvb_spi->dmx_fe);
0591 if (ret < 0) {
0592 pr_err("add_frontend() failed\n");
0593 goto fail_dmx_fe;
0594 }
0595
0596 ret = dvb_spi->demux.dmx.connect_frontend(&dvb_spi->demux.dmx,
0597 &dvb_spi->dmx_fe);
0598 if (ret < 0) {
0599 pr_err("connect_frontend() failed\n");
0600 goto fail_fe_conn;
0601 }
0602
0603 pr_info("Sony CXD2880 has successfully attached.\n");
0604
0605 return 0;
0606
0607 fail_fe_conn:
0608 dvb_spi->demux.dmx.remove_frontend(&dvb_spi->demux.dmx,
0609 &dvb_spi->dmx_fe);
0610 fail_dmx_fe:
0611 dvb_dmxdev_release(&dvb_spi->dmxdev);
0612 fail_dmxdev:
0613 dvb_dmx_release(&dvb_spi->demux);
0614 fail_dmx:
0615 dvb_unregister_frontend(&dvb_spi->dvb_fe);
0616 fail_frontend:
0617 dvb_frontend_detach(&dvb_spi->dvb_fe);
0618 fail_attach:
0619 dvb_unregister_adapter(&dvb_spi->adapter);
0620 fail_adapter:
0621 if (dvb_spi->vcc_supply)
0622 regulator_disable(dvb_spi->vcc_supply);
0623 fail_regulator:
0624 kfree(dvb_spi);
0625 return ret;
0626 }
0627
0628 static void
0629 cxd2880_spi_remove(struct spi_device *spi)
0630 {
0631 struct cxd2880_dvb_spi *dvb_spi = spi_get_drvdata(spi);
0632
0633 dvb_spi->demux.dmx.remove_frontend(&dvb_spi->demux.dmx,
0634 &dvb_spi->dmx_fe);
0635 dvb_dmxdev_release(&dvb_spi->dmxdev);
0636 dvb_dmx_release(&dvb_spi->demux);
0637 dvb_unregister_frontend(&dvb_spi->dvb_fe);
0638 dvb_frontend_detach(&dvb_spi->dvb_fe);
0639 dvb_unregister_adapter(&dvb_spi->adapter);
0640
0641 if (dvb_spi->vcc_supply)
0642 regulator_disable(dvb_spi->vcc_supply);
0643
0644 kfree(dvb_spi);
0645 pr_info("cxd2880_spi remove ok.\n");
0646 }
0647
0648 static const struct spi_device_id cxd2880_spi_id[] = {
0649 { "cxd2880", 0 },
0650 { }
0651 };
0652 MODULE_DEVICE_TABLE(spi, cxd2880_spi_id);
0653
0654 static struct spi_driver cxd2880_spi_driver = {
0655 .driver = {
0656 .name = "cxd2880",
0657 .of_match_table = cxd2880_spi_of_match,
0658 },
0659 .id_table = cxd2880_spi_id,
0660 .probe = cxd2880_spi_probe,
0661 .remove = cxd2880_spi_remove,
0662 };
0663 module_spi_driver(cxd2880_spi_driver);
0664
0665 MODULE_DESCRIPTION("Sony CXD2880 DVB-T2/T tuner + demod driver SPI adapter");
0666 MODULE_AUTHOR("Sony Semiconductor Solutions Corporation");
0667 MODULE_LICENSE("GPL v2");