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 */
25*c182615fSSam Ravnborg
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
radeon_process_i2c_ch(struct radeon_i2c_chan * chan,u8 slave_addr,u8 flags,u8 * buf,int num)3630388c6eSAlex Deucher static int radeon_process_i2c_ch(struct radeon_i2c_chan *chan,
3730388c6eSAlex Deucher u8 slave_addr, u8 flags,
381f81fbc4SMathieu Malaterre u8 *buf, int 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);
46831719d6SAlex Deucher int r = 0;
4730388c6eSAlex Deucher
4830388c6eSAlex Deucher memset(&args, 0, sizeof(args));
4930388c6eSAlex Deucher
50831719d6SAlex Deucher mutex_lock(&chan->mutex);
511c949842SDave Airlie mutex_lock(&rdev->mode_info.atom_context->scratch_mutex);
52831719d6SAlex Deucher
5330388c6eSAlex Deucher base = (unsigned char *)rdev->mode_info.atom_context->scratch;
5430388c6eSAlex Deucher
5530388c6eSAlex Deucher if (flags & HW_I2C_WRITE) {
5630388c6eSAlex Deucher if (num > ATOM_MAX_HW_I2C_WRITE) {
57d1e3b556SAlex Deucher DRM_ERROR("hw i2c: tried to write too many bytes (%d vs 3)\n", num);
58831719d6SAlex Deucher r = -EINVAL;
59831719d6SAlex Deucher goto done;
6030388c6eSAlex Deucher }
61ffd3d336SAlex Deucher if (buf == NULL)
62ffd3d336SAlex Deucher args.ucRegIndex = 0;
63ffd3d336SAlex Deucher else
64d1e3b556SAlex Deucher args.ucRegIndex = buf[0];
65ffd3d336SAlex Deucher if (num)
66fae009d1SJerome Glisse num--;
67ffd3d336SAlex Deucher if (num)
68fae009d1SJerome Glisse memcpy(&out, &buf[1], num);
6930388c6eSAlex Deucher args.lpI2CDataOut = cpu_to_le16(out);
7030388c6eSAlex Deucher } else {
71d1e3b556SAlex Deucher args.ucRegIndex = 0;
72d1e3b556SAlex Deucher args.lpI2CDataOut = 0;
7330388c6eSAlex Deucher }
7430388c6eSAlex Deucher
75d1e3b556SAlex Deucher args.ucFlag = flags;
7630388c6eSAlex Deucher args.ucI2CSpeed = TARGET_HW_I2C_CLOCK;
7730388c6eSAlex Deucher args.ucTransBytes = num;
7830388c6eSAlex Deucher args.ucSlaveAddr = slave_addr << 1;
7930388c6eSAlex Deucher args.ucLineNumber = chan->rec.i2c_id;
8030388c6eSAlex Deucher
811c949842SDave Airlie atom_execute_table_scratch_unlocked(rdev->mode_info.atom_context, index, (uint32_t *)&args);
8230388c6eSAlex Deucher
8330388c6eSAlex Deucher /* error */
8430388c6eSAlex Deucher if (args.ucStatus != HW_ASSISTED_I2C_STATUS_SUCCESS) {
8530388c6eSAlex Deucher DRM_DEBUG_KMS("hw_i2c error\n");
86831719d6SAlex Deucher r = -EIO;
87831719d6SAlex Deucher goto done;
8830388c6eSAlex Deucher }
8930388c6eSAlex Deucher
9030388c6eSAlex Deucher if (!(flags & HW_I2C_WRITE))
914543eda5SAlex Deucher radeon_atom_copy_swap(buf, base, num, false);
9230388c6eSAlex Deucher
93831719d6SAlex Deucher done:
941c949842SDave Airlie mutex_unlock(&rdev->mode_info.atom_context->scratch_mutex);
95831719d6SAlex Deucher mutex_unlock(&chan->mutex);
96831719d6SAlex Deucher
97831719d6SAlex Deucher return r;
9830388c6eSAlex Deucher }
9930388c6eSAlex Deucher
radeon_atom_hw_i2c_xfer(struct i2c_adapter * i2c_adap,struct i2c_msg * msgs,int num)10030388c6eSAlex Deucher int radeon_atom_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
10130388c6eSAlex Deucher struct i2c_msg *msgs, int num)
10230388c6eSAlex Deucher {
10330388c6eSAlex Deucher struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
10430388c6eSAlex Deucher struct i2c_msg *p;
10530388c6eSAlex Deucher int i, remaining, current_count, buffer_offset, max_bytes, ret;
106ffd3d336SAlex Deucher u8 flags;
10730388c6eSAlex Deucher
10830388c6eSAlex Deucher /* check for bus probe */
10930388c6eSAlex Deucher p = &msgs[0];
11030388c6eSAlex Deucher if ((num == 1) && (p->len == 0)) {
11130388c6eSAlex Deucher ret = radeon_process_i2c_ch(i2c,
11230388c6eSAlex Deucher p->addr, HW_I2C_WRITE,
113ffd3d336SAlex Deucher NULL, 0);
11430388c6eSAlex Deucher if (ret)
11530388c6eSAlex Deucher return ret;
11630388c6eSAlex Deucher else
11730388c6eSAlex Deucher return num;
11830388c6eSAlex Deucher }
11930388c6eSAlex Deucher
12030388c6eSAlex Deucher for (i = 0; i < num; i++) {
12130388c6eSAlex Deucher p = &msgs[i];
12230388c6eSAlex Deucher remaining = p->len;
12330388c6eSAlex Deucher buffer_offset = 0;
12430388c6eSAlex Deucher /* max_bytes are a limitation of ProcessI2cChannelTransaction not the hw */
12530388c6eSAlex Deucher if (p->flags & I2C_M_RD) {
12630388c6eSAlex Deucher max_bytes = ATOM_MAX_HW_I2C_READ;
12730388c6eSAlex Deucher flags = HW_I2C_READ;
12830388c6eSAlex Deucher } else {
12930388c6eSAlex Deucher max_bytes = ATOM_MAX_HW_I2C_WRITE;
13030388c6eSAlex Deucher flags = HW_I2C_WRITE;
13130388c6eSAlex Deucher }
13230388c6eSAlex Deucher while (remaining) {
13330388c6eSAlex Deucher if (remaining > max_bytes)
13430388c6eSAlex Deucher current_count = max_bytes;
13530388c6eSAlex Deucher else
13630388c6eSAlex Deucher current_count = remaining;
13730388c6eSAlex Deucher ret = radeon_process_i2c_ch(i2c,
13830388c6eSAlex Deucher p->addr, flags,
13930388c6eSAlex Deucher &p->buf[buffer_offset], current_count);
14030388c6eSAlex Deucher if (ret)
14130388c6eSAlex Deucher return ret;
14230388c6eSAlex Deucher remaining -= current_count;
14330388c6eSAlex Deucher buffer_offset += current_count;
14430388c6eSAlex Deucher }
14530388c6eSAlex Deucher }
14630388c6eSAlex Deucher
14730388c6eSAlex Deucher return num;
14830388c6eSAlex Deucher }
14930388c6eSAlex Deucher
radeon_atom_hw_i2c_func(struct i2c_adapter * adap)15030388c6eSAlex Deucher u32 radeon_atom_hw_i2c_func(struct i2c_adapter *adap)
15130388c6eSAlex Deucher {
15230388c6eSAlex Deucher return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
15330388c6eSAlex Deucher }
15430388c6eSAlex Deucher
155