1*b843c749SSergey Zigachev /*
2*b843c749SSergey Zigachev * Copyright 2011 Advanced Micro Devices, Inc.
3*b843c749SSergey Zigachev *
4*b843c749SSergey Zigachev * Permission is hereby granted, free of charge, to any person obtaining a
5*b843c749SSergey Zigachev * copy of this software and associated documentation files (the "Software"),
6*b843c749SSergey Zigachev * to deal in the Software without restriction, including without limitation
7*b843c749SSergey Zigachev * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*b843c749SSergey Zigachev * and/or sell copies of the Software, and to permit persons to whom the
9*b843c749SSergey Zigachev * Software is furnished to do so, subject to the following conditions:
10*b843c749SSergey Zigachev *
11*b843c749SSergey Zigachev * The above copyright notice and this permission notice shall be included in
12*b843c749SSergey Zigachev * all copies or substantial portions of the Software.
13*b843c749SSergey Zigachev *
14*b843c749SSergey Zigachev * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*b843c749SSergey Zigachev * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*b843c749SSergey Zigachev * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17*b843c749SSergey Zigachev * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*b843c749SSergey Zigachev * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*b843c749SSergey Zigachev * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*b843c749SSergey Zigachev * OTHER DEALINGS IN THE SOFTWARE.
21*b843c749SSergey Zigachev *
22*b843c749SSergey Zigachev * Authors: Alex Deucher
23*b843c749SSergey Zigachev *
24*b843c749SSergey Zigachev */
25*b843c749SSergey Zigachev #include <drm/drmP.h>
26*b843c749SSergey Zigachev #include <drm/amdgpu_drm.h>
27*b843c749SSergey Zigachev #include "amdgpu.h"
28*b843c749SSergey Zigachev #include "atom.h"
29*b843c749SSergey Zigachev #include "amdgpu_atombios.h"
30*b843c749SSergey Zigachev #include "atombios_i2c.h"
31*b843c749SSergey Zigachev
32*b843c749SSergey Zigachev #define TARGET_HW_I2C_CLOCK 50
33*b843c749SSergey Zigachev
34*b843c749SSergey Zigachev /* these are a limitation of ProcessI2cChannelTransaction not the hw */
35*b843c749SSergey Zigachev #define ATOM_MAX_HW_I2C_WRITE 3
36*b843c749SSergey Zigachev #define ATOM_MAX_HW_I2C_READ 255
37*b843c749SSergey Zigachev
amdgpu_atombios_i2c_process_i2c_ch(struct amdgpu_i2c_chan * chan,u8 slave_addr,u8 flags,u8 * buf,u8 num)38*b843c749SSergey Zigachev static int amdgpu_atombios_i2c_process_i2c_ch(struct amdgpu_i2c_chan *chan,
39*b843c749SSergey Zigachev u8 slave_addr, u8 flags,
40*b843c749SSergey Zigachev u8 *buf, u8 num)
41*b843c749SSergey Zigachev {
42*b843c749SSergey Zigachev struct drm_device *dev = chan->dev;
43*b843c749SSergey Zigachev struct amdgpu_device *adev = dev->dev_private;
44*b843c749SSergey Zigachev PROCESS_I2C_CHANNEL_TRANSACTION_PS_ALLOCATION args;
45*b843c749SSergey Zigachev int index = GetIndexIntoMasterTable(COMMAND, ProcessI2cChannelTransaction);
46*b843c749SSergey Zigachev unsigned char *base;
47*b843c749SSergey Zigachev u16 out = cpu_to_le16(0);
48*b843c749SSergey Zigachev int r = 0;
49*b843c749SSergey Zigachev
50*b843c749SSergey Zigachev memset(&args, 0, sizeof(args));
51*b843c749SSergey Zigachev
52*b843c749SSergey Zigachev mutex_lock(&chan->mutex);
53*b843c749SSergey Zigachev
54*b843c749SSergey Zigachev base = (unsigned char *)adev->mode_info.atom_context->scratch;
55*b843c749SSergey Zigachev
56*b843c749SSergey Zigachev if (flags & HW_I2C_WRITE) {
57*b843c749SSergey Zigachev if (num > ATOM_MAX_HW_I2C_WRITE) {
58*b843c749SSergey Zigachev DRM_ERROR("hw i2c: tried to write too many bytes (%d vs 3)\n", num);
59*b843c749SSergey Zigachev r = -EINVAL;
60*b843c749SSergey Zigachev goto done;
61*b843c749SSergey Zigachev }
62*b843c749SSergey Zigachev if (buf == NULL)
63*b843c749SSergey Zigachev args.ucRegIndex = 0;
64*b843c749SSergey Zigachev else
65*b843c749SSergey Zigachev args.ucRegIndex = buf[0];
66*b843c749SSergey Zigachev if (num)
67*b843c749SSergey Zigachev num--;
68*b843c749SSergey Zigachev if (num) {
69*b843c749SSergey Zigachev if (buf) {
70*b843c749SSergey Zigachev memcpy(&out, &buf[1], num);
71*b843c749SSergey Zigachev } else {
72*b843c749SSergey Zigachev DRM_ERROR("hw i2c: missing buf with num > 1\n");
73*b843c749SSergey Zigachev r = -EINVAL;
74*b843c749SSergey Zigachev goto done;
75*b843c749SSergey Zigachev }
76*b843c749SSergey Zigachev }
77*b843c749SSergey Zigachev args.lpI2CDataOut = cpu_to_le16(out);
78*b843c749SSergey Zigachev } else {
79*b843c749SSergey Zigachev if (num > ATOM_MAX_HW_I2C_READ) {
80*b843c749SSergey Zigachev DRM_ERROR("hw i2c: tried to read too many bytes (%d vs 255)\n", num);
81*b843c749SSergey Zigachev r = -EINVAL;
82*b843c749SSergey Zigachev goto done;
83*b843c749SSergey Zigachev }
84*b843c749SSergey Zigachev args.ucRegIndex = 0;
85*b843c749SSergey Zigachev args.lpI2CDataOut = 0;
86*b843c749SSergey Zigachev }
87*b843c749SSergey Zigachev
88*b843c749SSergey Zigachev args.ucFlag = flags;
89*b843c749SSergey Zigachev args.ucI2CSpeed = TARGET_HW_I2C_CLOCK;
90*b843c749SSergey Zigachev args.ucTransBytes = num;
91*b843c749SSergey Zigachev args.ucSlaveAddr = slave_addr << 1;
92*b843c749SSergey Zigachev args.ucLineNumber = chan->rec.i2c_id;
93*b843c749SSergey Zigachev
94*b843c749SSergey Zigachev amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
95*b843c749SSergey Zigachev
96*b843c749SSergey Zigachev /* error */
97*b843c749SSergey Zigachev if (args.ucStatus != HW_ASSISTED_I2C_STATUS_SUCCESS) {
98*b843c749SSergey Zigachev DRM_DEBUG_KMS("hw_i2c error\n");
99*b843c749SSergey Zigachev r = -EIO;
100*b843c749SSergey Zigachev goto done;
101*b843c749SSergey Zigachev }
102*b843c749SSergey Zigachev
103*b843c749SSergey Zigachev if (!(flags & HW_I2C_WRITE))
104*b843c749SSergey Zigachev amdgpu_atombios_copy_swap(buf, base, num, false);
105*b843c749SSergey Zigachev
106*b843c749SSergey Zigachev done:
107*b843c749SSergey Zigachev mutex_unlock(&chan->mutex);
108*b843c749SSergey Zigachev
109*b843c749SSergey Zigachev return r;
110*b843c749SSergey Zigachev }
111*b843c749SSergey Zigachev
amdgpu_atombios_i2c_xfer(struct i2c_adapter * i2c_adap,struct i2c_msg * msgs,int num)112*b843c749SSergey Zigachev int amdgpu_atombios_i2c_xfer(struct i2c_adapter *i2c_adap,
113*b843c749SSergey Zigachev struct i2c_msg *msgs, int num)
114*b843c749SSergey Zigachev {
115*b843c749SSergey Zigachev struct amdgpu_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
116*b843c749SSergey Zigachev struct i2c_msg *p;
117*b843c749SSergey Zigachev int i, remaining, current_count, buffer_offset, max_bytes, ret;
118*b843c749SSergey Zigachev u8 flags;
119*b843c749SSergey Zigachev
120*b843c749SSergey Zigachev /* check for bus probe */
121*b843c749SSergey Zigachev p = &msgs[0];
122*b843c749SSergey Zigachev if ((num == 1) && (p->len == 0)) {
123*b843c749SSergey Zigachev ret = amdgpu_atombios_i2c_process_i2c_ch(i2c,
124*b843c749SSergey Zigachev p->addr, HW_I2C_WRITE,
125*b843c749SSergey Zigachev NULL, 0);
126*b843c749SSergey Zigachev if (ret)
127*b843c749SSergey Zigachev return ret;
128*b843c749SSergey Zigachev else
129*b843c749SSergey Zigachev return num;
130*b843c749SSergey Zigachev }
131*b843c749SSergey Zigachev
132*b843c749SSergey Zigachev for (i = 0; i < num; i++) {
133*b843c749SSergey Zigachev p = &msgs[i];
134*b843c749SSergey Zigachev remaining = p->len;
135*b843c749SSergey Zigachev buffer_offset = 0;
136*b843c749SSergey Zigachev /* max_bytes are a limitation of ProcessI2cChannelTransaction not the hw */
137*b843c749SSergey Zigachev if (p->flags & I2C_M_RD) {
138*b843c749SSergey Zigachev max_bytes = ATOM_MAX_HW_I2C_READ;
139*b843c749SSergey Zigachev flags = HW_I2C_READ;
140*b843c749SSergey Zigachev } else {
141*b843c749SSergey Zigachev max_bytes = ATOM_MAX_HW_I2C_WRITE;
142*b843c749SSergey Zigachev flags = HW_I2C_WRITE;
143*b843c749SSergey Zigachev }
144*b843c749SSergey Zigachev while (remaining) {
145*b843c749SSergey Zigachev if (remaining > max_bytes)
146*b843c749SSergey Zigachev current_count = max_bytes;
147*b843c749SSergey Zigachev else
148*b843c749SSergey Zigachev current_count = remaining;
149*b843c749SSergey Zigachev ret = amdgpu_atombios_i2c_process_i2c_ch(i2c,
150*b843c749SSergey Zigachev p->addr, flags,
151*b843c749SSergey Zigachev &p->buf[buffer_offset], current_count);
152*b843c749SSergey Zigachev if (ret)
153*b843c749SSergey Zigachev return ret;
154*b843c749SSergey Zigachev remaining -= current_count;
155*b843c749SSergey Zigachev buffer_offset += current_count;
156*b843c749SSergey Zigachev }
157*b843c749SSergey Zigachev }
158*b843c749SSergey Zigachev
159*b843c749SSergey Zigachev return num;
160*b843c749SSergey Zigachev }
161*b843c749SSergey Zigachev
amdgpu_atombios_i2c_func(struct i2c_adapter * adap)162*b843c749SSergey Zigachev u32 amdgpu_atombios_i2c_func(struct i2c_adapter *adap)
163*b843c749SSergey Zigachev {
164*b843c749SSergey Zigachev return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
165*b843c749SSergey Zigachev }
166*b843c749SSergey Zigachev
amdgpu_atombios_i2c_channel_trans(struct amdgpu_device * adev,u8 slave_addr,u8 line_number,u8 offset,u8 data)167*b843c749SSergey Zigachev void amdgpu_atombios_i2c_channel_trans(struct amdgpu_device* adev, u8 slave_addr, u8 line_number, u8 offset, u8 data)
168*b843c749SSergey Zigachev {
169*b843c749SSergey Zigachev PROCESS_I2C_CHANNEL_TRANSACTION_PS_ALLOCATION args;
170*b843c749SSergey Zigachev int index = GetIndexIntoMasterTable(COMMAND, ProcessI2cChannelTransaction);
171*b843c749SSergey Zigachev
172*b843c749SSergey Zigachev args.ucRegIndex = offset;
173*b843c749SSergey Zigachev args.lpI2CDataOut = data;
174*b843c749SSergey Zigachev args.ucFlag = 1;
175*b843c749SSergey Zigachev args.ucI2CSpeed = TARGET_HW_I2C_CLOCK;
176*b843c749SSergey Zigachev args.ucTransBytes = 1;
177*b843c749SSergey Zigachev args.ucSlaveAddr = slave_addr;
178*b843c749SSergey Zigachev args.ucLineNumber = line_number;
179*b843c749SSergey Zigachev
180*b843c749SSergey Zigachev amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
181*b843c749SSergey Zigachev }
182