xref: /openbmc/linux/drivers/media/usb/go7007/go7007-i2c.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1*1802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27955f03dSHans Verkuil /*
37955f03dSHans Verkuil  * Copyright (C) 2005-2006 Micronas USA Inc.
47955f03dSHans Verkuil  */
57955f03dSHans Verkuil 
67955f03dSHans Verkuil #include <linux/module.h>
77955f03dSHans Verkuil #include <linux/delay.h>
87955f03dSHans Verkuil #include <linux/sched.h>
97955f03dSHans Verkuil #include <linux/list.h>
107955f03dSHans Verkuil #include <linux/unistd.h>
117955f03dSHans Verkuil #include <linux/time.h>
127955f03dSHans Verkuil #include <linux/device.h>
137955f03dSHans Verkuil #include <linux/i2c.h>
147955f03dSHans Verkuil #include <linux/mutex.h>
157955f03dSHans Verkuil #include <linux/uaccess.h>
167955f03dSHans Verkuil 
177955f03dSHans Verkuil #include "go7007-priv.h"
187955f03dSHans Verkuil 
197955f03dSHans Verkuil /********************* Driver for on-board I2C adapter *********************/
207955f03dSHans Verkuil 
217955f03dSHans Verkuil /* #define GO7007_I2C_DEBUG */
227955f03dSHans Verkuil 
237955f03dSHans Verkuil #define SPI_I2C_ADDR_BASE		0x1400
247955f03dSHans Verkuil #define STATUS_REG_ADDR			(SPI_I2C_ADDR_BASE + 0x2)
257955f03dSHans Verkuil #define I2C_CTRL_REG_ADDR		(SPI_I2C_ADDR_BASE + 0x6)
267955f03dSHans Verkuil #define I2C_DEV_UP_ADDR_REG_ADDR	(SPI_I2C_ADDR_BASE + 0x7)
277955f03dSHans Verkuil #define I2C_LO_ADDR_REG_ADDR		(SPI_I2C_ADDR_BASE + 0x8)
287955f03dSHans Verkuil #define I2C_DATA_REG_ADDR		(SPI_I2C_ADDR_BASE + 0x9)
297955f03dSHans Verkuil #define I2C_CLKFREQ_REG_ADDR		(SPI_I2C_ADDR_BASE + 0xa)
307955f03dSHans Verkuil 
317955f03dSHans Verkuil #define I2C_STATE_MASK			0x0007
327955f03dSHans Verkuil #define I2C_READ_READY_MASK		0x0008
337955f03dSHans Verkuil 
347955f03dSHans Verkuil /* There is only one I2C port on the TW2804 that feeds all four GO7007 VIPs
357955f03dSHans Verkuil  * on the Adlink PCI-MPG24, so access is shared between all of them. */
367955f03dSHans Verkuil static DEFINE_MUTEX(adlink_mpg24_i2c_lock);
377955f03dSHans Verkuil 
go7007_i2c_xfer(struct go7007 * go,u16 addr,int read,u16 command,int flags,u8 * data)387955f03dSHans Verkuil static int go7007_i2c_xfer(struct go7007 *go, u16 addr, int read,
397955f03dSHans Verkuil 		u16 command, int flags, u8 *data)
407955f03dSHans Verkuil {
417955f03dSHans Verkuil 	int i, ret = -EIO;
427955f03dSHans Verkuil 	u16 val;
437955f03dSHans Verkuil 
447955f03dSHans Verkuil 	if (go->status == STATUS_SHUTDOWN)
457955f03dSHans Verkuil 		return -ENODEV;
467955f03dSHans Verkuil 
477955f03dSHans Verkuil #ifdef GO7007_I2C_DEBUG
487955f03dSHans Verkuil 	if (read)
497955f03dSHans Verkuil 		dev_dbg(go->dev, "go7007-i2c: reading 0x%02x on 0x%02x\n",
507955f03dSHans Verkuil 			command, addr);
517955f03dSHans Verkuil 	else
527955f03dSHans Verkuil 		dev_dbg(go->dev,
537955f03dSHans Verkuil 			"go7007-i2c: writing 0x%02x to 0x%02x on 0x%02x\n",
547955f03dSHans Verkuil 			*data, command, addr);
557955f03dSHans Verkuil #endif
567955f03dSHans Verkuil 
577955f03dSHans Verkuil 	mutex_lock(&go->hw_lock);
587955f03dSHans Verkuil 
597955f03dSHans Verkuil 	if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
607955f03dSHans Verkuil 		/* Bridge the I2C port on this GO7007 to the shared bus */
617955f03dSHans Verkuil 		mutex_lock(&adlink_mpg24_i2c_lock);
627955f03dSHans Verkuil 		go7007_write_addr(go, 0x3c82, 0x0020);
637955f03dSHans Verkuil 	}
647955f03dSHans Verkuil 
657955f03dSHans Verkuil 	/* Wait for I2C adapter to be ready */
667955f03dSHans Verkuil 	for (i = 0; i < 10; ++i) {
677955f03dSHans Verkuil 		if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
687955f03dSHans Verkuil 			goto i2c_done;
697955f03dSHans Verkuil 		if (!(val & I2C_STATE_MASK))
707955f03dSHans Verkuil 			break;
717955f03dSHans Verkuil 		msleep(100);
727955f03dSHans Verkuil 	}
737955f03dSHans Verkuil 	if (i == 10) {
747955f03dSHans Verkuil 		dev_err(go->dev, "go7007-i2c: I2C adapter is hung\n");
757955f03dSHans Verkuil 		goto i2c_done;
767955f03dSHans Verkuil 	}
777955f03dSHans Verkuil 
787955f03dSHans Verkuil 	/* Set target register (command) */
797955f03dSHans Verkuil 	go7007_write_addr(go, I2C_CTRL_REG_ADDR, flags);
807955f03dSHans Verkuil 	go7007_write_addr(go, I2C_LO_ADDR_REG_ADDR, command);
817955f03dSHans Verkuil 
827955f03dSHans Verkuil 	/* If we're writing, send the data and target address and we're done */
837955f03dSHans Verkuil 	if (!read) {
847955f03dSHans Verkuil 		go7007_write_addr(go, I2C_DATA_REG_ADDR, *data);
857955f03dSHans Verkuil 		go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
867955f03dSHans Verkuil 					(addr << 9) | (command >> 8));
877955f03dSHans Verkuil 		ret = 0;
887955f03dSHans Verkuil 		goto i2c_done;
897955f03dSHans Verkuil 	}
907955f03dSHans Verkuil 
917955f03dSHans Verkuil 	/* Otherwise, we're reading.  First clear i2c_rx_data_rdy. */
927955f03dSHans Verkuil 	if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
937955f03dSHans Verkuil 		goto i2c_done;
947955f03dSHans Verkuil 
957955f03dSHans Verkuil 	/* Send the target address plus read flag */
967955f03dSHans Verkuil 	go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
977955f03dSHans Verkuil 			(addr << 9) | 0x0100 | (command >> 8));
987955f03dSHans Verkuil 
997955f03dSHans Verkuil 	/* Wait for i2c_rx_data_rdy */
1007955f03dSHans Verkuil 	for (i = 0; i < 10; ++i) {
1017955f03dSHans Verkuil 		if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
1027955f03dSHans Verkuil 			goto i2c_done;
1037955f03dSHans Verkuil 		if (val & I2C_READ_READY_MASK)
1047955f03dSHans Verkuil 			break;
1057955f03dSHans Verkuil 		msleep(100);
1067955f03dSHans Verkuil 	}
1077955f03dSHans Verkuil 	if (i == 10) {
1087955f03dSHans Verkuil 		dev_err(go->dev, "go7007-i2c: I2C adapter is hung\n");
1097955f03dSHans Verkuil 		goto i2c_done;
1107955f03dSHans Verkuil 	}
1117955f03dSHans Verkuil 
1127955f03dSHans Verkuil 	/* Retrieve the read byte */
1137955f03dSHans Verkuil 	if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
1147955f03dSHans Verkuil 		goto i2c_done;
1157955f03dSHans Verkuil 	*data = val;
1167955f03dSHans Verkuil 	ret = 0;
1177955f03dSHans Verkuil 
1187955f03dSHans Verkuil i2c_done:
1197955f03dSHans Verkuil 	if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
1207955f03dSHans Verkuil 		/* Isolate the I2C port on this GO7007 from the shared bus */
1217955f03dSHans Verkuil 		go7007_write_addr(go, 0x3c82, 0x0000);
1227955f03dSHans Verkuil 		mutex_unlock(&adlink_mpg24_i2c_lock);
1237955f03dSHans Verkuil 	}
1247955f03dSHans Verkuil 	mutex_unlock(&go->hw_lock);
1257955f03dSHans Verkuil 	return ret;
1267955f03dSHans Verkuil }
1277955f03dSHans Verkuil 
go7007_smbus_xfer(struct i2c_adapter * adapter,u16 addr,unsigned short flags,char read_write,u8 command,int size,union i2c_smbus_data * data)1287955f03dSHans Verkuil static int go7007_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
1297955f03dSHans Verkuil 		unsigned short flags, char read_write,
1307955f03dSHans Verkuil 		u8 command, int size, union i2c_smbus_data *data)
1317955f03dSHans Verkuil {
1327955f03dSHans Verkuil 	struct go7007 *go = i2c_get_adapdata(adapter);
1337955f03dSHans Verkuil 
1347955f03dSHans Verkuil 	if (size != I2C_SMBUS_BYTE_DATA)
1357955f03dSHans Verkuil 		return -EIO;
1367955f03dSHans Verkuil 	return go7007_i2c_xfer(go, addr, read_write == I2C_SMBUS_READ, command,
1377955f03dSHans Verkuil 			flags & I2C_CLIENT_SCCB ? 0x10 : 0x00, &data->byte);
1387955f03dSHans Verkuil }
1397955f03dSHans Verkuil 
1407955f03dSHans Verkuil /* VERY LIMITED I2C master xfer function -- only needed because the
1417955f03dSHans Verkuil  * SMBus functions only support 8-bit commands and the SAA7135 uses
1427955f03dSHans Verkuil  * 16-bit commands.  The I2C interface on the GO7007, as limited as
1437955f03dSHans Verkuil  * it is, does support this mode. */
1447955f03dSHans Verkuil 
go7007_i2c_master_xfer(struct i2c_adapter * adapter,struct i2c_msg msgs[],int num)1457955f03dSHans Verkuil static int go7007_i2c_master_xfer(struct i2c_adapter *adapter,
1467955f03dSHans Verkuil 					struct i2c_msg msgs[], int num)
1477955f03dSHans Verkuil {
1487955f03dSHans Verkuil 	struct go7007 *go = i2c_get_adapdata(adapter);
1497955f03dSHans Verkuil 	int i;
1507955f03dSHans Verkuil 
1517955f03dSHans Verkuil 	for (i = 0; i < num; ++i) {
1527955f03dSHans Verkuil 		/* We can only do two things here -- write three bytes, or
1537955f03dSHans Verkuil 		 * write two bytes and read one byte. */
1547955f03dSHans Verkuil 		if (msgs[i].len == 2) {
1557955f03dSHans Verkuil 			if (i + 1 == num || msgs[i].addr != msgs[i + 1].addr ||
1567955f03dSHans Verkuil 					(msgs[i].flags & I2C_M_RD) ||
1577955f03dSHans Verkuil 					!(msgs[i + 1].flags & I2C_M_RD) ||
1587955f03dSHans Verkuil 					msgs[i + 1].len != 1)
1597955f03dSHans Verkuil 				return -EIO;
1607955f03dSHans Verkuil 			if (go7007_i2c_xfer(go, msgs[i].addr, 1,
1617955f03dSHans Verkuil 					(msgs[i].buf[0] << 8) | msgs[i].buf[1],
1627955f03dSHans Verkuil 					0x01, &msgs[i + 1].buf[0]) < 0)
1637955f03dSHans Verkuil 				return -EIO;
1647955f03dSHans Verkuil 			++i;
1657955f03dSHans Verkuil 		} else if (msgs[i].len == 3) {
1667955f03dSHans Verkuil 			if (msgs[i].flags & I2C_M_RD)
1677955f03dSHans Verkuil 				return -EIO;
1687955f03dSHans Verkuil 			if (go7007_i2c_xfer(go, msgs[i].addr, 0,
1697955f03dSHans Verkuil 					(msgs[i].buf[0] << 8) | msgs[i].buf[1],
1707955f03dSHans Verkuil 					0x01, &msgs[i].buf[2]) < 0)
1717955f03dSHans Verkuil 				return -EIO;
1727955f03dSHans Verkuil 		} else
1737955f03dSHans Verkuil 			return -EIO;
1747955f03dSHans Verkuil 	}
1757955f03dSHans Verkuil 
1767955f03dSHans Verkuil 	return num;
1777955f03dSHans Verkuil }
1787955f03dSHans Verkuil 
go7007_functionality(struct i2c_adapter * adapter)1797955f03dSHans Verkuil static u32 go7007_functionality(struct i2c_adapter *adapter)
1807955f03dSHans Verkuil {
1817955f03dSHans Verkuil 	return I2C_FUNC_SMBUS_BYTE_DATA;
1827955f03dSHans Verkuil }
1837955f03dSHans Verkuil 
18478f2c50bSJulia Lawall static const struct i2c_algorithm go7007_algo = {
1857955f03dSHans Verkuil 	.smbus_xfer	= go7007_smbus_xfer,
1867955f03dSHans Verkuil 	.master_xfer	= go7007_i2c_master_xfer,
1877955f03dSHans Verkuil 	.functionality	= go7007_functionality,
1887955f03dSHans Verkuil };
1897955f03dSHans Verkuil 
1907955f03dSHans Verkuil static struct i2c_adapter go7007_adap_templ = {
1917955f03dSHans Verkuil 	.owner			= THIS_MODULE,
1927955f03dSHans Verkuil 	.name			= "WIS GO7007SB",
1937955f03dSHans Verkuil 	.algo			= &go7007_algo,
1947955f03dSHans Verkuil };
1957955f03dSHans Verkuil 
go7007_i2c_init(struct go7007 * go)1967955f03dSHans Verkuil int go7007_i2c_init(struct go7007 *go)
1977955f03dSHans Verkuil {
1987955f03dSHans Verkuil 	memcpy(&go->i2c_adapter, &go7007_adap_templ,
1997955f03dSHans Verkuil 			sizeof(go7007_adap_templ));
2007955f03dSHans Verkuil 	go->i2c_adapter.dev.parent = go->dev;
2017955f03dSHans Verkuil 	i2c_set_adapdata(&go->i2c_adapter, go);
2027955f03dSHans Verkuil 	if (i2c_add_adapter(&go->i2c_adapter) < 0) {
2037955f03dSHans Verkuil 		dev_err(go->dev,
2047955f03dSHans Verkuil 			"go7007-i2c: error: i2c_add_adapter failed\n");
2057955f03dSHans Verkuil 		return -1;
2067955f03dSHans Verkuil 	}
2077955f03dSHans Verkuil 	return 0;
2087955f03dSHans Verkuil }
209