summaryrefslogtreecommitdiffstats
path: root/drivers/media/dvb-frontends/cxd2880/cxd2880_devio_spi.c
blob: aba59400859e1b09a6f0729839b36d387e2763a0 (plain)
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
// SPDX-License-Identifier: GPL-2.0
/*
 * cxd2880_devio_spi.c
 * Sony CXD2880 DVB-T2/T tuner + demodulator driver
 * I/O interface via SPI
 *
 * Copyright (C) 2016, 2017, 2018 Sony Semiconductor Solutions Corporation
 */

#include "cxd2880_devio_spi.h"

#define BURST_WRITE_MAX 128

static int cxd2880_io_spi_read_reg(struct cxd2880_io *io,
				   enum cxd2880_io_tgt tgt,
				   u8 sub_address, u8 *data,
				   u32 size)
{
	int ret = 0;
	struct cxd2880_spi *spi = NULL;
	u8 send_data[6];
	u8 *read_data_top = data;

	if (!io || !io->if_object || !data)
		return -EINVAL;

	if (sub_address + size > 0x100)
		return -EINVAL;

	spi = io->if_object;

	if (tgt == CXD2880_IO_TGT_SYS)
		send_data[0] = 0x0b;
	else
		send_data[0] = 0x0a;

	send_data[3] = 0;
	send_data[4] = 0;
	send_data[5] = 0;

	while (size > 0) {
		send_data[1] = sub_address;
		if (size > 255)
			send_data[2] = 255;
		else
			send_data[2] = size;

		ret =
		    spi->write_read(spi, send_data, sizeof(send_data),
				    read_data_top, send_data[2]);
		if (ret)
			return ret;

		sub_address += send_data[2];
		read_data_top += send_data[2];
		size -= send_data[2];
	}

	return ret;
}

static int cxd2880_io_spi_write_reg(struct cxd2880_io *io,
				    enum cxd2880_io_tgt tgt,
				    u8 sub_address,
				    const u8 *data, u32 size)
{
	int ret = 0;
	struct cxd2880_spi *spi = NULL;
	u8 send_data[BURST_WRITE_MAX + 4];
	const u8 *write_data_top = data;

	if (!io || !io->if_object || !data)
		return -EINVAL;

	if (size > BURST_WRITE_MAX)
		return -EINVAL;

	if (sub_address + size > 0x100)
		return -EINVAL;

	spi = io->if_object;

	if (tgt == CXD2880_IO_TGT_SYS)
		send_data[0] = 0x0f;
	else
		send_data[0] = 0x0e;

	while (size > 0) {
		send_data[1] = sub_address;
		if (size > 255)
			send_data[2] = 255;
		else
			send_data[2] = size;

		memcpy(&send_data[3], write_data_top, send_data[2]);

		if (tgt == CXD2880_IO_TGT_SYS) {
			send_data[3 + send_data[2]] = 0x00;
			ret = spi->write(spi, send_data, send_data[2] + 4);
		} else {
			ret = spi->write(spi, send_data, send_data[2] + 3);
		}
		if (ret)
			return ret;

		sub_address += send_data[2];
		write_data_top += send_data[2];
		size -= send_data[2];
	}

	return ret;
}

int cxd2880_io_spi_create(struct cxd2880_io *io,
			  struct cxd2880_spi *spi, u8 slave_select)
{
	if (!io || !spi)
		return -EINVAL;

	io->read_regs = cxd2880_io_spi_read_reg;
	io->write_regs = cxd2880_io_spi_write_reg;
	io->write_reg = cxd2880_io_common_write_one_reg;
	io->if_object = spi;
	io->i2c_address_sys = 0;
	io->i2c_address_demod = 0;
	io->slave_select = slave_select;

	return 0;
}