18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * cxd2880_devio_spi.c
48c2ecf20Sopenharmony_ci * Sony CXD2880 DVB-T2/T tuner + demodulator driver
58c2ecf20Sopenharmony_ci * I/O interface via SPI
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (C) 2016, 2017, 2018 Sony Semiconductor Solutions Corporation
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include "cxd2880_devio_spi.h"
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#define BURST_WRITE_MAX 128
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistatic int cxd2880_io_spi_read_reg(struct cxd2880_io *io,
158c2ecf20Sopenharmony_ci				   enum cxd2880_io_tgt tgt,
168c2ecf20Sopenharmony_ci				   u8 sub_address, u8 *data,
178c2ecf20Sopenharmony_ci				   u32 size)
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	int ret = 0;
208c2ecf20Sopenharmony_ci	struct cxd2880_spi *spi = NULL;
218c2ecf20Sopenharmony_ci	u8 send_data[6];
228c2ecf20Sopenharmony_ci	u8 *read_data_top = data;
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	if (!io || !io->if_object || !data)
258c2ecf20Sopenharmony_ci		return -EINVAL;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	if (sub_address + size > 0x100)
288c2ecf20Sopenharmony_ci		return -EINVAL;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	spi = io->if_object;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	if (tgt == CXD2880_IO_TGT_SYS)
338c2ecf20Sopenharmony_ci		send_data[0] = 0x0b;
348c2ecf20Sopenharmony_ci	else
358c2ecf20Sopenharmony_ci		send_data[0] = 0x0a;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	send_data[3] = 0;
388c2ecf20Sopenharmony_ci	send_data[4] = 0;
398c2ecf20Sopenharmony_ci	send_data[5] = 0;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	while (size > 0) {
428c2ecf20Sopenharmony_ci		send_data[1] = sub_address;
438c2ecf20Sopenharmony_ci		if (size > 255)
448c2ecf20Sopenharmony_ci			send_data[2] = 255;
458c2ecf20Sopenharmony_ci		else
468c2ecf20Sopenharmony_ci			send_data[2] = size;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci		ret =
498c2ecf20Sopenharmony_ci		    spi->write_read(spi, send_data, sizeof(send_data),
508c2ecf20Sopenharmony_ci				    read_data_top, send_data[2]);
518c2ecf20Sopenharmony_ci		if (ret)
528c2ecf20Sopenharmony_ci			return ret;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci		sub_address += send_data[2];
558c2ecf20Sopenharmony_ci		read_data_top += send_data[2];
568c2ecf20Sopenharmony_ci		size -= send_data[2];
578c2ecf20Sopenharmony_ci	}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	return ret;
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic int cxd2880_io_spi_write_reg(struct cxd2880_io *io,
638c2ecf20Sopenharmony_ci				    enum cxd2880_io_tgt tgt,
648c2ecf20Sopenharmony_ci				    u8 sub_address,
658c2ecf20Sopenharmony_ci				    const u8 *data, u32 size)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	int ret = 0;
688c2ecf20Sopenharmony_ci	struct cxd2880_spi *spi = NULL;
698c2ecf20Sopenharmony_ci	u8 send_data[BURST_WRITE_MAX + 4];
708c2ecf20Sopenharmony_ci	const u8 *write_data_top = data;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	if (!io || !io->if_object || !data)
738c2ecf20Sopenharmony_ci		return -EINVAL;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	if (size > BURST_WRITE_MAX)
768c2ecf20Sopenharmony_ci		return -EINVAL;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	if (sub_address + size > 0x100)
798c2ecf20Sopenharmony_ci		return -EINVAL;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	spi = io->if_object;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	if (tgt == CXD2880_IO_TGT_SYS)
848c2ecf20Sopenharmony_ci		send_data[0] = 0x0f;
858c2ecf20Sopenharmony_ci	else
868c2ecf20Sopenharmony_ci		send_data[0] = 0x0e;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	while (size > 0) {
898c2ecf20Sopenharmony_ci		send_data[1] = sub_address;
908c2ecf20Sopenharmony_ci		if (size > 255)
918c2ecf20Sopenharmony_ci			send_data[2] = 255;
928c2ecf20Sopenharmony_ci		else
938c2ecf20Sopenharmony_ci			send_data[2] = size;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci		memcpy(&send_data[3], write_data_top, send_data[2]);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci		if (tgt == CXD2880_IO_TGT_SYS) {
988c2ecf20Sopenharmony_ci			send_data[3 + send_data[2]] = 0x00;
998c2ecf20Sopenharmony_ci			ret = spi->write(spi, send_data, send_data[2] + 4);
1008c2ecf20Sopenharmony_ci		} else {
1018c2ecf20Sopenharmony_ci			ret = spi->write(spi, send_data, send_data[2] + 3);
1028c2ecf20Sopenharmony_ci		}
1038c2ecf20Sopenharmony_ci		if (ret)
1048c2ecf20Sopenharmony_ci			return ret;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci		sub_address += send_data[2];
1078c2ecf20Sopenharmony_ci		write_data_top += send_data[2];
1088c2ecf20Sopenharmony_ci		size -= send_data[2];
1098c2ecf20Sopenharmony_ci	}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	return ret;
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ciint cxd2880_io_spi_create(struct cxd2880_io *io,
1158c2ecf20Sopenharmony_ci			  struct cxd2880_spi *spi, u8 slave_select)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	if (!io || !spi)
1188c2ecf20Sopenharmony_ci		return -EINVAL;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	io->read_regs = cxd2880_io_spi_read_reg;
1218c2ecf20Sopenharmony_ci	io->write_regs = cxd2880_io_spi_write_reg;
1228c2ecf20Sopenharmony_ci	io->write_reg = cxd2880_io_common_write_one_reg;
1238c2ecf20Sopenharmony_ci	io->if_object = spi;
1248c2ecf20Sopenharmony_ci	io->i2c_address_sys = 0;
1258c2ecf20Sopenharmony_ci	io->i2c_address_demod = 0;
1268c2ecf20Sopenharmony_ci	io->slave_select = slave_select;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	return 0;
1298c2ecf20Sopenharmony_ci}
130