xref: /openbmc/linux/drivers/media/spi/cxd2880-spi.c (revision 2359ccdd)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * cxd2880-spi.c
4  * Sony CXD2880 DVB-T2/T tuner + demodulator driver
5  * SPI adapter
6  *
7  * Copyright (C) 2016, 2017, 2018 Sony Semiconductor Solutions Corporation
8  */
9 
10 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
11 
12 #include <linux/spi/spi.h>
13 #include <linux/ktime.h>
14 
15 #include <media/dvb_demux.h>
16 #include <media/dmxdev.h>
17 #include <media/dvb_frontend.h>
18 #include "cxd2880.h"
19 
20 #define CXD2880_MAX_FILTER_SIZE 32
21 #define BURST_WRITE_MAX 128
22 #define MAX_TRANS_PKT 300
23 
24 struct cxd2880_ts_buf_info {
25 	u8 read_ready:1;
26 	u8 almost_full:1;
27 	u8 almost_empty:1;
28 	u8 overflow:1;
29 	u8 underflow:1;
30 	u16 pkt_num;
31 };
32 
33 struct cxd2880_pid_config {
34 	u8 is_enable;
35 	u16 pid;
36 };
37 
38 struct cxd2880_pid_filter_config {
39 	u8 is_negative;
40 	struct cxd2880_pid_config pid_config[CXD2880_MAX_FILTER_SIZE];
41 };
42 
43 struct cxd2880_dvb_spi {
44 	struct dvb_frontend dvb_fe;
45 	struct dvb_adapter adapter;
46 	struct dvb_demux demux;
47 	struct dmxdev dmxdev;
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 */
52 	int feed_count;
53 	int all_pid_feed_count;
54 	u8 *ts_buf;
55 	struct cxd2880_pid_filter_config filter_config;
56 };
57 
58 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
59 
60 static int cxd2880_write_spi(struct spi_device *spi, u8 *data, u32 size)
61 {
62 	struct spi_message msg;
63 	struct spi_transfer tx;
64 
65 	if (!spi || !data) {
66 		pr_err("invalid arg\n");
67 		return -EINVAL;
68 	}
69 
70 	memset(&tx, 0, sizeof(tx));
71 	tx.tx_buf = data;
72 	tx.len = size;
73 
74 	spi_message_init(&msg);
75 	spi_message_add_tail(&tx, &msg);
76 
77 	return spi_sync(spi, &msg);
78 }
79 
80 static int cxd2880_write_reg(struct spi_device *spi,
81 			     u8 sub_address, const u8 *data, u32 size)
82 {
83 	u8 send_data[BURST_WRITE_MAX + 4];
84 	const u8 *write_data_top = NULL;
85 	int ret = 0;
86 
87 	if (!spi || !data) {
88 		pr_err("invalid arg\n");
89 		return -EINVAL;
90 	}
91 	if (size > BURST_WRITE_MAX) {
92 		pr_err("data size > WRITE_MAX\n");
93 		return -EINVAL;
94 	}
95 
96 	if (sub_address + size > 0x100) {
97 		pr_err("out of range\n");
98 		return -EINVAL;
99 	}
100 
101 	send_data[0] = 0x0e;
102 	write_data_top = data;
103 
104 	while (size > 0) {
105 		send_data[1] = sub_address;
106 		if (size > 255)
107 			send_data[2] = 255;
108 		else
109 			send_data[2] = (u8)size;
110 
111 		memcpy(&send_data[3], write_data_top, send_data[2]);
112 
113 		ret = cxd2880_write_spi(spi, send_data, send_data[2] + 3);
114 		if (ret) {
115 			pr_err("write spi failed %d\n", ret);
116 			break;
117 		}
118 		sub_address += send_data[2];
119 		write_data_top += send_data[2];
120 		size -= send_data[2];
121 	}
122 
123 	return ret;
124 }
125 
126 static int cxd2880_spi_read_ts(struct spi_device *spi,
127 			       u8 *read_data,
128 			       u32 packet_num)
129 {
130 	int ret;
131 	u8 data[3];
132 	struct spi_message message;
133 	struct spi_transfer transfer[2];
134 
135 	if (!spi || !read_data || !packet_num) {
136 		pr_err("invalid arg\n");
137 		return -EINVAL;
138 	}
139 	if (packet_num > 0xffff) {
140 		pr_err("packet num > 0xffff\n");
141 		return -EINVAL;
142 	}
143 
144 	data[0] = 0x10;
145 	data[1] = packet_num >> 8;
146 	data[2] = packet_num;
147 
148 	spi_message_init(&message);
149 	memset(transfer, 0, sizeof(transfer));
150 
151 	transfer[0].len = 3;
152 	transfer[0].tx_buf = data;
153 	spi_message_add_tail(&transfer[0], &message);
154 	transfer[1].len = packet_num * 188;
155 	transfer[1].rx_buf = read_data;
156 	spi_message_add_tail(&transfer[1], &message);
157 
158 	ret = spi_sync(spi, &message);
159 	if (ret)
160 		pr_err("spi_write_then_read failed\n");
161 
162 	return ret;
163 }
164 
165 static int cxd2880_spi_read_ts_buffer_info(struct spi_device *spi,
166 					   struct cxd2880_ts_buf_info *info)
167 {
168 	u8 send_data = 0x20;
169 	u8 recv_data[2];
170 	int ret;
171 
172 	if (!spi || !info) {
173 		pr_err("invalid arg\n");
174 		return -EINVAL;
175 	}
176 
177 	ret = spi_write_then_read(spi, &send_data, 1,
178 				  recv_data, sizeof(recv_data));
179 	if (ret)
180 		pr_err("spi_write_then_read failed\n");
181 
182 	info->read_ready = (recv_data[0] & 0x80) ? 1 : 0;
183 	info->almost_full = (recv_data[0] & 0x40) ? 1 : 0;
184 	info->almost_empty = (recv_data[0] & 0x20) ? 1 : 0;
185 	info->overflow = (recv_data[0] & 0x10) ? 1 : 0;
186 	info->underflow = (recv_data[0] & 0x08) ? 1 : 0;
187 	info->pkt_num = ((recv_data[0] & 0x07) << 8) | recv_data[1];
188 
189 	return ret;
190 }
191 
192 static int cxd2880_spi_clear_ts_buffer(struct spi_device *spi)
193 {
194 	u8 data = 0x03;
195 	int ret;
196 
197 	ret = cxd2880_write_spi(spi, &data, 1);
198 
199 	if (ret)
200 		pr_err("write spi failed\n");
201 
202 	return ret;
203 }
204 
205 static int cxd2880_set_pid_filter(struct spi_device *spi,
206 				  struct cxd2880_pid_filter_config *cfg)
207 {
208 	u8 data[65];
209 	int i;
210 	u16 pid = 0;
211 	int ret;
212 
213 	if (!spi) {
214 		pr_err("invalid arg\n");
215 		return -EINVAL;
216 	}
217 
218 	data[0] = 0x00;
219 	ret = cxd2880_write_reg(spi, 0x00, &data[0], 1);
220 	if (ret)
221 		return ret;
222 	if (!cfg) {
223 		data[0] = 0x02;
224 		ret = cxd2880_write_reg(spi, 0x50, &data[0], 1);
225 	} else {
226 		data[0] = cfg->is_negative ? 0x01 : 0x00;
227 
228 		for (i = 0; i < CXD2880_MAX_FILTER_SIZE; i++) {
229 			pid = cfg->pid_config[i].pid;
230 			if (cfg->pid_config[i].is_enable) {
231 				data[1 + (i * 2)] = (pid >> 8) | 0x20;
232 				data[2 + (i * 2)] = pid & 0xff;
233 			} else {
234 				data[1 + (i * 2)] = 0x00;
235 				data[2 + (i * 2)] = 0x00;
236 			}
237 		}
238 		ret = cxd2880_write_reg(spi, 0x50, data, 65);
239 	}
240 
241 	return ret;
242 }
243 
244 static int cxd2880_update_pid_filter(struct cxd2880_dvb_spi *dvb_spi,
245 				     struct cxd2880_pid_filter_config *cfg,
246 				     bool is_all_pid_filter)
247 {
248 	int ret;
249 
250 	if (!dvb_spi || !cfg) {
251 		pr_err("invalid arg.\n");
252 		return -EINVAL;
253 	}
254 
255 	mutex_lock(&dvb_spi->spi_mutex);
256 	if (is_all_pid_filter) {
257 		struct cxd2880_pid_filter_config tmpcfg;
258 
259 		memset(&tmpcfg, 0, sizeof(tmpcfg));
260 		tmpcfg.is_negative = 1;
261 		tmpcfg.pid_config[0].is_enable = 1;
262 		tmpcfg.pid_config[0].pid = 0x1fff;
263 
264 		ret = cxd2880_set_pid_filter(dvb_spi->spi, &tmpcfg);
265 	} else {
266 		ret = cxd2880_set_pid_filter(dvb_spi->spi, cfg);
267 	}
268 	mutex_unlock(&dvb_spi->spi_mutex);
269 
270 	if (ret)
271 		pr_err("set_pid_filter failed\n");
272 
273 	return ret;
274 }
275 
276 static int cxd2880_ts_read(void *arg)
277 {
278 	struct cxd2880_dvb_spi *dvb_spi = NULL;
279 	struct cxd2880_ts_buf_info info;
280 	ktime_t start;
281 	u32 i;
282 	int ret;
283 
284 	dvb_spi = arg;
285 	if (!dvb_spi) {
286 		pr_err("invalid arg\n");
287 		return -EINVAL;
288 	}
289 
290 	ret = cxd2880_spi_clear_ts_buffer(dvb_spi->spi);
291 	if (ret) {
292 		pr_err("set_clear_ts_buffer failed\n");
293 		return ret;
294 	}
295 
296 	start = ktime_get();
297 	while (!kthread_should_stop()) {
298 		ret = cxd2880_spi_read_ts_buffer_info(dvb_spi->spi,
299 						      &info);
300 		if (ret) {
301 			pr_err("spi_read_ts_buffer_info error\n");
302 			return ret;
303 		}
304 
305 		if (info.pkt_num > MAX_TRANS_PKT) {
306 			for (i = 0; i < info.pkt_num / MAX_TRANS_PKT; i++) {
307 				cxd2880_spi_read_ts(dvb_spi->spi,
308 						    dvb_spi->ts_buf,
309 						    MAX_TRANS_PKT);
310 				dvb_dmx_swfilter(&dvb_spi->demux,
311 						 dvb_spi->ts_buf,
312 						 MAX_TRANS_PKT * 188);
313 			}
314 			start = ktime_get();
315 		} else if ((info.pkt_num > 0) &&
316 			   (ktime_to_ms(ktime_sub(ktime_get(), start)) >= 500)) {
317 			cxd2880_spi_read_ts(dvb_spi->spi,
318 					    dvb_spi->ts_buf,
319 					    info.pkt_num);
320 			dvb_dmx_swfilter(&dvb_spi->demux,
321 					 dvb_spi->ts_buf,
322 					 info.pkt_num * 188);
323 			start = ktime_get();
324 		} else {
325 			usleep_range(10000, 11000);
326 		}
327 	}
328 
329 	return 0;
330 }
331 
332 static int cxd2880_start_feed(struct dvb_demux_feed *feed)
333 {
334 	int ret = 0;
335 	int i = 0;
336 	struct dvb_demux *demux = NULL;
337 	struct cxd2880_dvb_spi *dvb_spi = NULL;
338 
339 	if (!feed) {
340 		pr_err("invalid arg\n");
341 		return -EINVAL;
342 	}
343 
344 	demux = feed->demux;
345 	if (!demux) {
346 		pr_err("feed->demux is NULL\n");
347 		return -EINVAL;
348 	}
349 	dvb_spi = demux->priv;
350 
351 	if (dvb_spi->feed_count == CXD2880_MAX_FILTER_SIZE) {
352 		pr_err("Exceeded maximum PID count (32).");
353 		pr_err("Selected PID cannot be enabled.\n");
354 		return -EINVAL;
355 	}
356 
357 	if (feed->pid == 0x2000) {
358 		if (dvb_spi->all_pid_feed_count == 0) {
359 			ret = cxd2880_update_pid_filter(dvb_spi,
360 							&dvb_spi->filter_config,
361 							true);
362 			if (ret) {
363 				pr_err("update pid filter failed\n");
364 				return ret;
365 			}
366 		}
367 		dvb_spi->all_pid_feed_count++;
368 
369 		pr_debug("all PID feed (count = %d)\n",
370 			 dvb_spi->all_pid_feed_count);
371 	} else {
372 		struct cxd2880_pid_filter_config cfgtmp;
373 
374 		cfgtmp = dvb_spi->filter_config;
375 
376 		for (i = 0; i < CXD2880_MAX_FILTER_SIZE; i++) {
377 			if (cfgtmp.pid_config[i].is_enable == 0) {
378 				cfgtmp.pid_config[i].is_enable = 1;
379 				cfgtmp.pid_config[i].pid = feed->pid;
380 				pr_debug("store PID %d to #%d\n",
381 					 feed->pid, i);
382 				break;
383 			}
384 		}
385 		if (i == CXD2880_MAX_FILTER_SIZE) {
386 			pr_err("PID filter is full. Assumed bug.\n");
387 			return -EINVAL;
388 		}
389 		if (!dvb_spi->all_pid_feed_count)
390 			ret = cxd2880_update_pid_filter(dvb_spi,
391 							&cfgtmp,
392 							false);
393 		if (ret)
394 			return ret;
395 
396 		dvb_spi->filter_config = cfgtmp;
397 	}
398 
399 	if (dvb_spi->feed_count == 0) {
400 		dvb_spi->ts_buf =
401 			kmalloc(MAX_TRANS_PKT * 188,
402 				GFP_KERNEL | GFP_DMA);
403 		if (!dvb_spi->ts_buf) {
404 			pr_err("ts buffer allocate failed\n");
405 			memset(&dvb_spi->filter_config, 0,
406 			       sizeof(dvb_spi->filter_config));
407 			dvb_spi->all_pid_feed_count = 0;
408 			return -ENOMEM;
409 		}
410 		dvb_spi->cxd2880_ts_read_thread = kthread_run(cxd2880_ts_read,
411 							      dvb_spi,
412 							      "cxd2880_ts_read");
413 		if (IS_ERR(dvb_spi->cxd2880_ts_read_thread)) {
414 			pr_err("kthread_run failed/\n");
415 			kfree(dvb_spi->ts_buf);
416 			dvb_spi->ts_buf = NULL;
417 			memset(&dvb_spi->filter_config, 0,
418 			       sizeof(dvb_spi->filter_config));
419 			dvb_spi->all_pid_feed_count = 0;
420 			return PTR_ERR(dvb_spi->cxd2880_ts_read_thread);
421 		}
422 	}
423 
424 	dvb_spi->feed_count++;
425 
426 	pr_debug("start feed (count %d)\n", dvb_spi->feed_count);
427 	return 0;
428 }
429 
430 static int cxd2880_stop_feed(struct dvb_demux_feed *feed)
431 {
432 	int i = 0;
433 	int ret;
434 	struct dvb_demux *demux = NULL;
435 	struct cxd2880_dvb_spi *dvb_spi = NULL;
436 
437 	if (!feed) {
438 		pr_err("invalid arg\n");
439 		return -EINVAL;
440 	}
441 
442 	demux = feed->demux;
443 	if (!demux) {
444 		pr_err("feed->demux is NULL\n");
445 		return -EINVAL;
446 	}
447 	dvb_spi = demux->priv;
448 
449 	if (!dvb_spi->feed_count) {
450 		pr_err("no feed is started\n");
451 		return -EINVAL;
452 	}
453 
454 	if (feed->pid == 0x2000) {
455 		/*
456 		 * Special PID case.
457 		 * Number of 0x2000 feed request was stored
458 		 * in dvb_spi->all_pid_feed_count.
459 		 */
460 		if (dvb_spi->all_pid_feed_count <= 0) {
461 			pr_err("PID %d not found.\n", feed->pid);
462 			return -EINVAL;
463 		}
464 		dvb_spi->all_pid_feed_count--;
465 	} else {
466 		struct cxd2880_pid_filter_config cfgtmp;
467 
468 		cfgtmp = dvb_spi->filter_config;
469 
470 		for (i = 0; i < CXD2880_MAX_FILTER_SIZE; i++) {
471 			if (feed->pid == cfgtmp.pid_config[i].pid &&
472 			    cfgtmp.pid_config[i].is_enable != 0) {
473 				cfgtmp.pid_config[i].is_enable = 0;
474 				cfgtmp.pid_config[i].pid = 0;
475 				pr_debug("removed PID %d from #%d\n",
476 					 feed->pid, i);
477 				break;
478 			}
479 		}
480 		dvb_spi->filter_config = cfgtmp;
481 
482 		if (i == CXD2880_MAX_FILTER_SIZE) {
483 			pr_err("PID %d not found\n", feed->pid);
484 			return -EINVAL;
485 		}
486 	}
487 
488 	ret = cxd2880_update_pid_filter(dvb_spi,
489 					&dvb_spi->filter_config,
490 					dvb_spi->all_pid_feed_count > 0);
491 	dvb_spi->feed_count--;
492 
493 	if (dvb_spi->feed_count == 0) {
494 		int ret_stop = 0;
495 
496 		ret_stop = kthread_stop(dvb_spi->cxd2880_ts_read_thread);
497 		if (ret_stop) {
498 			pr_err("'kthread_stop failed. (%d)\n", ret_stop);
499 			ret = ret_stop;
500 		}
501 		kfree(dvb_spi->ts_buf);
502 		dvb_spi->ts_buf = NULL;
503 	}
504 
505 	pr_debug("stop feed ok.(count %d)\n", dvb_spi->feed_count);
506 
507 	return ret;
508 }
509 
510 static const struct of_device_id cxd2880_spi_of_match[] = {
511 	{ .compatible = "sony,cxd2880" },
512 	{ /* sentinel */ }
513 };
514 
515 MODULE_DEVICE_TABLE(of, cxd2880_spi_of_match);
516 
517 static int
518 cxd2880_spi_probe(struct spi_device *spi)
519 {
520 	int ret;
521 	struct cxd2880_dvb_spi *dvb_spi = NULL;
522 	struct cxd2880_config config;
523 
524 	if (!spi) {
525 		pr_err("invalid arg.\n");
526 		return -EINVAL;
527 	}
528 
529 	dvb_spi = kzalloc(sizeof(struct cxd2880_dvb_spi), GFP_KERNEL);
530 	if (!dvb_spi)
531 		return -ENOMEM;
532 
533 	dvb_spi->spi = spi;
534 	mutex_init(&dvb_spi->spi_mutex);
535 	dev_set_drvdata(&spi->dev, dvb_spi);
536 	config.spi = spi;
537 	config.spi_mutex = &dvb_spi->spi_mutex;
538 
539 	ret = dvb_register_adapter(&dvb_spi->adapter,
540 				   "CXD2880",
541 				   THIS_MODULE,
542 				   &spi->dev,
543 				   adapter_nr);
544 	if (ret < 0) {
545 		pr_err("dvb_register_adapter() failed\n");
546 		goto fail_adapter;
547 	}
548 
549 	if (!dvb_attach(cxd2880_attach, &dvb_spi->dvb_fe, &config)) {
550 		pr_err("cxd2880_attach failed\n");
551 		goto fail_attach;
552 	}
553 
554 	ret = dvb_register_frontend(&dvb_spi->adapter,
555 				    &dvb_spi->dvb_fe);
556 	if (ret < 0) {
557 		pr_err("dvb_register_frontend() failed\n");
558 		goto fail_frontend;
559 	}
560 
561 	dvb_spi->demux.dmx.capabilities = DMX_TS_FILTERING;
562 	dvb_spi->demux.priv = dvb_spi;
563 	dvb_spi->demux.filternum = CXD2880_MAX_FILTER_SIZE;
564 	dvb_spi->demux.feednum = CXD2880_MAX_FILTER_SIZE;
565 	dvb_spi->demux.start_feed = cxd2880_start_feed;
566 	dvb_spi->demux.stop_feed = cxd2880_stop_feed;
567 
568 	ret = dvb_dmx_init(&dvb_spi->demux);
569 	if (ret < 0) {
570 		pr_err("dvb_dmx_init() failed\n");
571 		goto fail_dmx;
572 	}
573 
574 	dvb_spi->dmxdev.filternum = CXD2880_MAX_FILTER_SIZE;
575 	dvb_spi->dmxdev.demux = &dvb_spi->demux.dmx;
576 	dvb_spi->dmxdev.capabilities = 0;
577 	ret = dvb_dmxdev_init(&dvb_spi->dmxdev,
578 			      &dvb_spi->adapter);
579 	if (ret < 0) {
580 		pr_err("dvb_dmxdev_init() failed\n");
581 		goto fail_dmxdev;
582 	}
583 
584 	dvb_spi->dmx_fe.source = DMX_FRONTEND_0;
585 	ret = dvb_spi->demux.dmx.add_frontend(&dvb_spi->demux.dmx,
586 					      &dvb_spi->dmx_fe);
587 	if (ret < 0) {
588 		pr_err("add_frontend() failed\n");
589 		goto fail_dmx_fe;
590 	}
591 
592 	ret = dvb_spi->demux.dmx.connect_frontend(&dvb_spi->demux.dmx,
593 						  &dvb_spi->dmx_fe);
594 	if (ret < 0) {
595 		pr_err("dvb_register_frontend() failed\n");
596 		goto fail_fe_conn;
597 	}
598 
599 	pr_info("Sony CXD2880 has successfully attached.\n");
600 
601 	return 0;
602 
603 fail_fe_conn:
604 	dvb_spi->demux.dmx.remove_frontend(&dvb_spi->demux.dmx,
605 					   &dvb_spi->dmx_fe);
606 fail_dmx_fe:
607 	dvb_dmxdev_release(&dvb_spi->dmxdev);
608 fail_dmxdev:
609 	dvb_dmx_release(&dvb_spi->demux);
610 fail_dmx:
611 	dvb_unregister_frontend(&dvb_spi->dvb_fe);
612 fail_frontend:
613 	dvb_frontend_detach(&dvb_spi->dvb_fe);
614 fail_attach:
615 	dvb_unregister_adapter(&dvb_spi->adapter);
616 fail_adapter:
617 	kfree(dvb_spi);
618 	return ret;
619 }
620 
621 static int
622 cxd2880_spi_remove(struct spi_device *spi)
623 {
624 	struct cxd2880_dvb_spi *dvb_spi;
625 
626 	if (!spi) {
627 		pr_err("invalid arg\n");
628 		return -EINVAL;
629 	}
630 
631 	dvb_spi = dev_get_drvdata(&spi->dev);
632 
633 	if (!dvb_spi) {
634 		pr_err("failed\n");
635 		return -EINVAL;
636 	}
637 	dvb_spi->demux.dmx.remove_frontend(&dvb_spi->demux.dmx,
638 					   &dvb_spi->dmx_fe);
639 	dvb_dmxdev_release(&dvb_spi->dmxdev);
640 	dvb_dmx_release(&dvb_spi->demux);
641 	dvb_unregister_frontend(&dvb_spi->dvb_fe);
642 	dvb_frontend_detach(&dvb_spi->dvb_fe);
643 	dvb_unregister_adapter(&dvb_spi->adapter);
644 
645 	kfree(dvb_spi);
646 	pr_info("cxd2880_spi remove ok.\n");
647 
648 	return 0;
649 }
650 
651 static const struct spi_device_id cxd2880_spi_id[] = {
652 	{ "cxd2880", 0 },
653 	{ /* sentinel */ }
654 };
655 MODULE_DEVICE_TABLE(spi, cxd2880_spi_id);
656 
657 static struct spi_driver cxd2880_spi_driver = {
658 	.driver	= {
659 		.name	= "cxd2880",
660 		.of_match_table = cxd2880_spi_of_match,
661 	},
662 	.id_table = cxd2880_spi_id,
663 	.probe    = cxd2880_spi_probe,
664 	.remove   = cxd2880_spi_remove,
665 };
666 module_spi_driver(cxd2880_spi_driver);
667 
668 MODULE_DESCRIPTION("Sony CXD2880 DVB-T2/T tuner + demod driver SPI adapter");
669 MODULE_AUTHOR("Sony Semiconductor Solutions Corporation");
670 MODULE_LICENSE("GPL v2");
671