xref: /openbmc/linux/drivers/gpu/drm/radeon/atombios_i2c.c (revision 831719d62f692e28699a7acd7b441c6f0c01b6f7)
130388c6eSAlex Deucher /*
230388c6eSAlex Deucher  * Copyright 2011 Advanced Micro Devices, Inc.
330388c6eSAlex Deucher  *
430388c6eSAlex Deucher  * Permission is hereby granted, free of charge, to any person obtaining a
530388c6eSAlex Deucher  * copy of this software and associated documentation files (the "Software"),
630388c6eSAlex Deucher  * to deal in the Software without restriction, including without limitation
730388c6eSAlex Deucher  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
830388c6eSAlex Deucher  * and/or sell copies of the Software, and to permit persons to whom the
930388c6eSAlex Deucher  * Software is furnished to do so, subject to the following conditions:
1030388c6eSAlex Deucher  *
1130388c6eSAlex Deucher  * The above copyright notice and this permission notice shall be included in
1230388c6eSAlex Deucher  * all copies or substantial portions of the Software.
1330388c6eSAlex Deucher  *
1430388c6eSAlex Deucher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1530388c6eSAlex Deucher  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1630388c6eSAlex Deucher  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1730388c6eSAlex Deucher  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1830388c6eSAlex Deucher  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1930388c6eSAlex Deucher  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2030388c6eSAlex Deucher  * OTHER DEALINGS IN THE SOFTWARE.
2130388c6eSAlex Deucher  *
2230388c6eSAlex Deucher  * Authors: Alex Deucher
2330388c6eSAlex Deucher  *
2430388c6eSAlex Deucher  */
25760285e7SDavid Howells #include <drm/drmP.h>
26760285e7SDavid Howells #include <drm/radeon_drm.h>
2730388c6eSAlex Deucher #include "radeon.h"
2830388c6eSAlex Deucher #include "atom.h"
2930388c6eSAlex Deucher 
3030388c6eSAlex Deucher #define TARGET_HW_I2C_CLOCK 50
3130388c6eSAlex Deucher 
3230388c6eSAlex Deucher /* these are a limitation of ProcessI2cChannelTransaction not the hw */
33d1e3b556SAlex Deucher #define ATOM_MAX_HW_I2C_WRITE 3
3430388c6eSAlex Deucher #define ATOM_MAX_HW_I2C_READ  255
3530388c6eSAlex Deucher 
3630388c6eSAlex Deucher static int radeon_process_i2c_ch(struct radeon_i2c_chan *chan,
3730388c6eSAlex Deucher 				 u8 slave_addr, u8 flags,
3830388c6eSAlex Deucher 				 u8 *buf, u8 num)
3930388c6eSAlex Deucher {
4030388c6eSAlex Deucher 	struct drm_device *dev = chan->dev;
4130388c6eSAlex Deucher 	struct radeon_device *rdev = dev->dev_private;
4230388c6eSAlex Deucher 	PROCESS_I2C_CHANNEL_TRANSACTION_PS_ALLOCATION args;
4330388c6eSAlex Deucher 	int index = GetIndexIntoMasterTable(COMMAND, ProcessI2cChannelTransaction);
4430388c6eSAlex Deucher 	unsigned char *base;
45ffd3d336SAlex Deucher 	u16 out = cpu_to_le16(0);
46*831719d6SAlex Deucher 	int r = 0;
4730388c6eSAlex Deucher 
4830388c6eSAlex Deucher 	memset(&args, 0, sizeof(args));
4930388c6eSAlex Deucher 
50*831719d6SAlex Deucher 	mutex_lock(&chan->mutex);
51*831719d6SAlex Deucher 
5230388c6eSAlex Deucher 	base = (unsigned char *)rdev->mode_info.atom_context->scratch;
5330388c6eSAlex Deucher 
5430388c6eSAlex Deucher 	if (flags & HW_I2C_WRITE) {
5530388c6eSAlex Deucher 		if (num > ATOM_MAX_HW_I2C_WRITE) {
56d1e3b556SAlex Deucher 			DRM_ERROR("hw i2c: tried to write too many bytes (%d vs 3)\n", num);
57*831719d6SAlex Deucher 			r = -EINVAL;
58*831719d6SAlex Deucher 			goto done;
5930388c6eSAlex Deucher 		}
60ffd3d336SAlex Deucher 		if (buf == NULL)
61ffd3d336SAlex Deucher 			args.ucRegIndex = 0;
62ffd3d336SAlex Deucher 		else
63d1e3b556SAlex Deucher 			args.ucRegIndex = buf[0];
64ffd3d336SAlex Deucher 		if (num)
65fae009d1SJerome Glisse 			num--;
66ffd3d336SAlex Deucher 		if (num)
67fae009d1SJerome Glisse 			memcpy(&out, &buf[1], num);
6830388c6eSAlex Deucher 		args.lpI2CDataOut = cpu_to_le16(out);
6930388c6eSAlex Deucher 	} else {
7030388c6eSAlex Deucher 		if (num > ATOM_MAX_HW_I2C_READ) {
7130388c6eSAlex Deucher 			DRM_ERROR("hw i2c: tried to read too many bytes (%d vs 255)\n", num);
72*831719d6SAlex Deucher 			r = -EINVAL;
73*831719d6SAlex Deucher 			goto done;
7430388c6eSAlex Deucher 		}
75d1e3b556SAlex Deucher 		args.ucRegIndex = 0;
76d1e3b556SAlex Deucher 		args.lpI2CDataOut = 0;
7730388c6eSAlex Deucher 	}
7830388c6eSAlex Deucher 
79d1e3b556SAlex Deucher 	args.ucFlag = flags;
8030388c6eSAlex Deucher 	args.ucI2CSpeed = TARGET_HW_I2C_CLOCK;
8130388c6eSAlex Deucher 	args.ucTransBytes = num;
8230388c6eSAlex Deucher 	args.ucSlaveAddr = slave_addr << 1;
8330388c6eSAlex Deucher 	args.ucLineNumber = chan->rec.i2c_id;
8430388c6eSAlex Deucher 
8530388c6eSAlex Deucher 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
8630388c6eSAlex Deucher 
8730388c6eSAlex Deucher 	/* error */
8830388c6eSAlex Deucher 	if (args.ucStatus != HW_ASSISTED_I2C_STATUS_SUCCESS) {
8930388c6eSAlex Deucher 		DRM_DEBUG_KMS("hw_i2c error\n");
90*831719d6SAlex Deucher 		r = -EIO;
91*831719d6SAlex Deucher 		goto done;
9230388c6eSAlex Deucher 	}
9330388c6eSAlex Deucher 
9430388c6eSAlex Deucher 	if (!(flags & HW_I2C_WRITE))
954543eda5SAlex Deucher 		radeon_atom_copy_swap(buf, base, num, false);
9630388c6eSAlex Deucher 
97*831719d6SAlex Deucher done:
98*831719d6SAlex Deucher 	mutex_unlock(&chan->mutex);
99*831719d6SAlex Deucher 
100*831719d6SAlex Deucher 	return r;
10130388c6eSAlex Deucher }
10230388c6eSAlex Deucher 
10330388c6eSAlex Deucher int radeon_atom_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
10430388c6eSAlex Deucher 			    struct i2c_msg *msgs, int num)
10530388c6eSAlex Deucher {
10630388c6eSAlex Deucher 	struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
10730388c6eSAlex Deucher 	struct i2c_msg *p;
10830388c6eSAlex Deucher 	int i, remaining, current_count, buffer_offset, max_bytes, ret;
109ffd3d336SAlex Deucher 	u8 flags;
11030388c6eSAlex Deucher 
11130388c6eSAlex Deucher 	/* check for bus probe */
11230388c6eSAlex Deucher 	p = &msgs[0];
11330388c6eSAlex Deucher 	if ((num == 1) && (p->len == 0)) {
11430388c6eSAlex Deucher 		ret = radeon_process_i2c_ch(i2c,
11530388c6eSAlex Deucher 					    p->addr, HW_I2C_WRITE,
116ffd3d336SAlex Deucher 					    NULL, 0);
11730388c6eSAlex Deucher 		if (ret)
11830388c6eSAlex Deucher 			return ret;
11930388c6eSAlex Deucher 		else
12030388c6eSAlex Deucher 			return num;
12130388c6eSAlex Deucher 	}
12230388c6eSAlex Deucher 
12330388c6eSAlex Deucher 	for (i = 0; i < num; i++) {
12430388c6eSAlex Deucher 		p = &msgs[i];
12530388c6eSAlex Deucher 		remaining = p->len;
12630388c6eSAlex Deucher 		buffer_offset = 0;
12730388c6eSAlex Deucher 		/* max_bytes are a limitation of ProcessI2cChannelTransaction not the hw */
12830388c6eSAlex Deucher 		if (p->flags & I2C_M_RD) {
12930388c6eSAlex Deucher 			max_bytes = ATOM_MAX_HW_I2C_READ;
13030388c6eSAlex Deucher 			flags = HW_I2C_READ;
13130388c6eSAlex Deucher 		} else {
13230388c6eSAlex Deucher 			max_bytes = ATOM_MAX_HW_I2C_WRITE;
13330388c6eSAlex Deucher 			flags = HW_I2C_WRITE;
13430388c6eSAlex Deucher 		}
13530388c6eSAlex Deucher 		while (remaining) {
13630388c6eSAlex Deucher 			if (remaining > max_bytes)
13730388c6eSAlex Deucher 				current_count = max_bytes;
13830388c6eSAlex Deucher 			else
13930388c6eSAlex Deucher 				current_count = remaining;
14030388c6eSAlex Deucher 			ret = radeon_process_i2c_ch(i2c,
14130388c6eSAlex Deucher 						    p->addr, flags,
14230388c6eSAlex Deucher 						    &p->buf[buffer_offset], current_count);
14330388c6eSAlex Deucher 			if (ret)
14430388c6eSAlex Deucher 				return ret;
14530388c6eSAlex Deucher 			remaining -= current_count;
14630388c6eSAlex Deucher 			buffer_offset += current_count;
14730388c6eSAlex Deucher 		}
14830388c6eSAlex Deucher 	}
14930388c6eSAlex Deucher 
15030388c6eSAlex Deucher 	return num;
15130388c6eSAlex Deucher }
15230388c6eSAlex Deucher 
15330388c6eSAlex Deucher u32 radeon_atom_hw_i2c_func(struct i2c_adapter *adap)
15430388c6eSAlex Deucher {
15530388c6eSAlex Deucher 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
15630388c6eSAlex Deucher }
15730388c6eSAlex Deucher 
158