1254885Sdumbbell/*
2254885Sdumbbell * Copyright 2011 Advanced Micro Devices, Inc.
3254885Sdumbbell *
4254885Sdumbbell * Permission is hereby granted, free of charge, to any person obtaining a
5254885Sdumbbell * copy of this software and associated documentation files (the "Software"),
6254885Sdumbbell * to deal in the Software without restriction, including without limitation
7254885Sdumbbell * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8254885Sdumbbell * and/or sell copies of the Software, and to permit persons to whom the
9254885Sdumbbell * Software is furnished to do so, subject to the following conditions:
10254885Sdumbbell *
11254885Sdumbbell * The above copyright notice and this permission notice shall be included in
12254885Sdumbbell * all copies or substantial portions of the Software.
13254885Sdumbbell *
14254885Sdumbbell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15254885Sdumbbell * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16254885Sdumbbell * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17254885Sdumbbell * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18254885Sdumbbell * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19254885Sdumbbell * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20254885Sdumbbell * OTHER DEALINGS IN THE SOFTWARE.
21254885Sdumbbell *
22254885Sdumbbell * Authors: Alex Deucher
23254885Sdumbbell *
24254885Sdumbbell */
25254885Sdumbbell
26254885Sdumbbell#include <sys/cdefs.h>
27254885Sdumbbell__FBSDID("$FreeBSD$");
28254885Sdumbbell
29254885Sdumbbell#include <dev/drm2/drmP.h>
30254885Sdumbbell#include <dev/drm2/radeon/radeon_drm.h>
31254885Sdumbbell#include <dev/iicbus/iic.h>
32254885Sdumbbell#include <dev/iicbus/iiconf.h>
33254885Sdumbbell#include <dev/iicbus/iicbus.h>
34254885Sdumbbell#include "radeon.h"
35254885Sdumbbell#include "atom.h"
36254885Sdumbbell#include "iicbus_if.h"
37254885Sdumbbell#include "iicbb_if.h"
38254885Sdumbbell
39254885Sdumbbell#define TARGET_HW_I2C_CLOCK 50
40254885Sdumbbell
41254885Sdumbbell/* these are a limitation of ProcessI2cChannelTransaction not the hw */
42254885Sdumbbell#define ATOM_MAX_HW_I2C_WRITE 2
43254885Sdumbbell#define ATOM_MAX_HW_I2C_READ  255
44254885Sdumbbell
45254885Sdumbbellstatic int radeon_process_i2c_ch(struct radeon_i2c_chan *chan,
46254885Sdumbbell				 u8 slave_addr, u8 flags,
47254885Sdumbbell				 u8 *buf, u8 num)
48254885Sdumbbell{
49254885Sdumbbell	struct drm_device *dev = chan->dev;
50254885Sdumbbell	struct radeon_device *rdev = dev->dev_private;
51254885Sdumbbell	PROCESS_I2C_CHANNEL_TRANSACTION_PS_ALLOCATION args;
52254885Sdumbbell	int index = GetIndexIntoMasterTable(COMMAND, ProcessI2cChannelTransaction);
53254885Sdumbbell	unsigned char *base;
54254885Sdumbbell	u16 out;
55254885Sdumbbell
56254885Sdumbbell	memset(&args, 0, sizeof(args));
57254885Sdumbbell
58254885Sdumbbell	base = (unsigned char *)rdev->mode_info.atom_context->scratch;
59254885Sdumbbell
60254885Sdumbbell	if (flags & HW_I2C_WRITE) {
61254885Sdumbbell		if (num > ATOM_MAX_HW_I2C_WRITE) {
62254885Sdumbbell			DRM_ERROR("hw i2c: tried to write too many bytes (%d vs 2)\n", num);
63254885Sdumbbell			return EINVAL;
64254885Sdumbbell		}
65254885Sdumbbell		memcpy(&out, buf, num);
66254885Sdumbbell		args.lpI2CDataOut = cpu_to_le16(out);
67254885Sdumbbell	}
68254885Sdumbbell
69254885Sdumbbell	args.ucI2CSpeed = TARGET_HW_I2C_CLOCK;
70254885Sdumbbell	args.ucRegIndex = 0;
71254885Sdumbbell	args.ucTransBytes = num;
72254885Sdumbbell	args.ucSlaveAddr = slave_addr << 1;
73254885Sdumbbell	args.ucLineNumber = chan->rec.i2c_id;
74254885Sdumbbell
75254885Sdumbbell	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
76254885Sdumbbell
77254885Sdumbbell	/* error */
78254885Sdumbbell	if (args.ucStatus != HW_ASSISTED_I2C_STATUS_SUCCESS) {
79254885Sdumbbell		DRM_DEBUG_KMS("hw_i2c error\n");
80254885Sdumbbell		return EIO;
81254885Sdumbbell	}
82254885Sdumbbell
83254885Sdumbbell	if (!(flags & HW_I2C_WRITE))
84254885Sdumbbell		memcpy(buf, base, num);
85254885Sdumbbell
86254885Sdumbbell	return 0;
87254885Sdumbbell}
88254885Sdumbbell
89254885Sdumbbellstatic int
90254885Sdumbbellradeon_atom_hw_i2c_xfer(device_t dev, struct iic_msg *msgs, u_int num)
91254885Sdumbbell{
92254885Sdumbbell	struct radeon_i2c_chan *i2c = device_get_softc(dev);
93254885Sdumbbell	struct iic_msg *p;
94254885Sdumbbell	int i, remaining, current_count, buffer_offset, max_bytes, ret;
95254885Sdumbbell	u8 buf = 0, flags;
96254885Sdumbbell
97254885Sdumbbell	/* check for bus probe */
98254885Sdumbbell	p = &msgs[0];
99254885Sdumbbell	if ((num == 1) && (p->len == 0)) {
100254885Sdumbbell		ret = radeon_process_i2c_ch(i2c,
101254885Sdumbbell					    p->slave, HW_I2C_WRITE,
102254885Sdumbbell					    &buf, 1);
103254885Sdumbbell		if (ret)
104254885Sdumbbell			return ret;
105254885Sdumbbell		else
106254885Sdumbbell			return (0);
107254885Sdumbbell	}
108254885Sdumbbell
109254885Sdumbbell	for (i = 0; i < num; i++) {
110254885Sdumbbell		p = &msgs[i];
111254885Sdumbbell		remaining = p->len;
112254885Sdumbbell		buffer_offset = 0;
113254885Sdumbbell		/* max_bytes are a limitation of ProcessI2cChannelTransaction not the hw */
114254885Sdumbbell		if (p->flags & IIC_M_RD) {
115254885Sdumbbell			max_bytes = ATOM_MAX_HW_I2C_READ;
116254885Sdumbbell			flags = HW_I2C_READ;
117254885Sdumbbell		} else {
118254885Sdumbbell			max_bytes = ATOM_MAX_HW_I2C_WRITE;
119254885Sdumbbell			flags = HW_I2C_WRITE;
120254885Sdumbbell		}
121254885Sdumbbell		while (remaining) {
122254885Sdumbbell			if (remaining > max_bytes)
123254885Sdumbbell				current_count = max_bytes;
124254885Sdumbbell			else
125254885Sdumbbell				current_count = remaining;
126254885Sdumbbell			ret = radeon_process_i2c_ch(i2c,
127254885Sdumbbell						    p->slave, flags,
128254885Sdumbbell						    &p->buf[buffer_offset], current_count);
129254885Sdumbbell			if (ret)
130254885Sdumbbell				return ret;
131254885Sdumbbell			remaining -= current_count;
132254885Sdumbbell			buffer_offset += current_count;
133254885Sdumbbell		}
134254885Sdumbbell	}
135254885Sdumbbell
136254885Sdumbbell	return (0);
137254885Sdumbbell}
138254885Sdumbbell
139254885Sdumbbellstatic int
140254885Sdumbbellradeon_atom_hw_i2c_probe(device_t dev)
141254885Sdumbbell{
142254885Sdumbbell
143254885Sdumbbell	return (BUS_PROBE_SPECIFIC);
144254885Sdumbbell}
145254885Sdumbbell
146254885Sdumbbellstatic int
147254885Sdumbbellradeon_atom_hw_i2c_attach(device_t dev)
148254885Sdumbbell{
149254885Sdumbbell	struct radeon_i2c_chan *i2c;
150254885Sdumbbell	device_t iic_dev;
151254885Sdumbbell
152254885Sdumbbell	i2c = device_get_softc(dev);
153254885Sdumbbell	device_set_desc(dev, i2c->name);
154254885Sdumbbell
155254885Sdumbbell	/* add generic bit-banging code */
156254885Sdumbbell	iic_dev = device_add_child(dev, "iicbus", -1);
157254885Sdumbbell	if (iic_dev == NULL)
158254885Sdumbbell		return (ENXIO);
159254885Sdumbbell	device_quiet(iic_dev);
160254885Sdumbbell
161254885Sdumbbell	/* attach and probe added child */
162254885Sdumbbell	bus_generic_attach(dev);
163254885Sdumbbell
164254885Sdumbbell	return (0);
165254885Sdumbbell}
166254885Sdumbbell
167254885Sdumbbellstatic int
168254885Sdumbbellradeon_atom_hw_i2c_detach(device_t dev)
169254885Sdumbbell{
170254885Sdumbbell	/* detach bit-banding code. */
171254885Sdumbbell	bus_generic_detach(dev);
172254885Sdumbbell
173254885Sdumbbell	/* delete bit-banding code. */
174254885Sdumbbell	device_delete_children(dev);
175254885Sdumbbell	return (0);
176254885Sdumbbell}
177254885Sdumbbell
178254885Sdumbbellstatic int
179254885Sdumbbellradeon_atom_hw_i2c_reset(device_t dev, u_char speed,
180254885Sdumbbell    u_char addr, u_char *oldaddr)
181254885Sdumbbell{
182254885Sdumbbell
183254885Sdumbbell	return (0);
184254885Sdumbbell}
185254885Sdumbbell
186254885Sdumbbellstatic device_method_t radeon_atom_hw_i2c_methods[] = {
187254885Sdumbbell	DEVMETHOD(device_probe,		radeon_atom_hw_i2c_probe),
188254885Sdumbbell	DEVMETHOD(device_attach,	radeon_atom_hw_i2c_attach),
189254885Sdumbbell	DEVMETHOD(device_detach,	radeon_atom_hw_i2c_detach),
190254885Sdumbbell	DEVMETHOD(iicbus_reset,		radeon_atom_hw_i2c_reset),
191254885Sdumbbell	DEVMETHOD(iicbus_transfer,	radeon_atom_hw_i2c_xfer),
192254885Sdumbbell	DEVMETHOD_END
193254885Sdumbbell};
194254885Sdumbbell
195254885Sdumbbellstatic driver_t radeon_atom_hw_i2c_driver = {
196254885Sdumbbell	"radeon_atom_hw_i2c",
197254885Sdumbbell	radeon_atom_hw_i2c_methods,
198254885Sdumbbell	0
199254885Sdumbbell};
200254885Sdumbbell
201254885Sdumbbellstatic devclass_t radeon_atom_hw_i2c_devclass;
202254885SdumbbellDRIVER_MODULE_ORDERED(radeon_atom_hw_i2c, drmn, radeon_atom_hw_i2c_driver,
203254885Sdumbbell    radeon_atom_hw_i2c_devclass, 0, 0, SI_ORDER_ANY);
204