aux.c 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright 2009 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Ben Skeggs
 */
24 25
#include "aux.h"
#include "pad.h"
26 27

static int
28
nvkm_i2c_aux_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
29
{
30
	struct nvkm_i2c_aux *aux = container_of(adap, typeof(*aux), i2c);
31 32 33
	struct i2c_msg *msg = msgs;
	int ret, mcnt = num;

34
	ret = nvkm_i2c_aux_acquire(aux);
35 36
	if (ret)
		return ret;
37

38 39 40 41 42
	while (mcnt--) {
		u8 remaining = msg->len;
		u8 *ptr = msg->buf;

		while (remaining) {
43
			u8 cnt, retries, cmd;
44 45 46 47 48 49 50 51 52

			if (msg->flags & I2C_M_RD)
				cmd = 1;
			else
				cmd = 0;

			if (mcnt || remaining > 16)
				cmd |= 4; /* MOT */

53 54 55 56 57 58 59 60 61 62 63 64 65
			for (retries = 0, cnt = 0;
			     retries < 32 && !cnt;
			     retries++) {
				cnt = min_t(u8, remaining, 16);
				ret = aux->func->xfer(aux, true, cmd,
						      msg->addr, ptr, &cnt);
				if (ret < 0)
					goto out;
			}
			if (!cnt) {
				AUX_TRACE(aux, "no data after 32 retries");
				ret = -EIO;
				goto out;
66
			}
67 68 69 70 71 72 73 74

			ptr += cnt;
			remaining -= cnt;
		}

		msg++;
	}

75 76
	ret = num;
out:
77
	nvkm_i2c_aux_release(aux);
78
	return ret;
79 80 81
}

static u32
82
nvkm_i2c_aux_i2c_func(struct i2c_adapter *adap)
83 84 85 86
{
	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
}

87
static const struct i2c_algorithm
88 89 90
nvkm_i2c_aux_i2c_algo = {
	.master_xfer = nvkm_i2c_aux_i2c_xfer,
	.functionality = nvkm_i2c_aux_i2c_func
91
};
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

void
nvkm_i2c_aux_monitor(struct nvkm_i2c_aux *aux, bool monitor)
{
	struct nvkm_i2c_pad *pad = aux->pad;
	AUX_TRACE(aux, "monitor: %s", monitor ? "yes" : "no");
	if (monitor)
		nvkm_i2c_pad_mode(pad, NVKM_I2C_PAD_AUX);
	else
		nvkm_i2c_pad_mode(pad, NVKM_I2C_PAD_OFF);
}

void
nvkm_i2c_aux_release(struct nvkm_i2c_aux *aux)
{
	struct nvkm_i2c_pad *pad = aux->pad;
	AUX_TRACE(aux, "release");
	nvkm_i2c_pad_release(pad);
	mutex_unlock(&aux->mutex);
}

int
nvkm_i2c_aux_acquire(struct nvkm_i2c_aux *aux)
{
	struct nvkm_i2c_pad *pad = aux->pad;
	int ret;
118

119 120
	AUX_TRACE(aux, "acquire");
	mutex_lock(&aux->mutex);
121 122 123 124 125 126

	if (aux->enabled)
		ret = nvkm_i2c_pad_acquire(pad, NVKM_I2C_PAD_AUX);
	else
		ret = -EIO;

127 128 129 130 131 132 133
	if (ret)
		mutex_unlock(&aux->mutex);
	return ret;
}

int
nvkm_i2c_aux_xfer(struct nvkm_i2c_aux *aux, bool retry, u8 type,
134
		  u32 addr, u8 *data, u8 *size)
135
{
136 137 138 139
	if (!*size && !aux->func->address_only) {
		AUX_ERR(aux, "address-only transaction dropped");
		return -ENOSYS;
	}
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
	return aux->func->xfer(aux, retry, type, addr, data, size);
}

int
nvkm_i2c_aux_lnk_ctl(struct nvkm_i2c_aux *aux, int nr, int bw, bool ef)
{
	if (aux->func->lnk_ctl)
		return aux->func->lnk_ctl(aux, nr, bw, ef);
	return -ENODEV;
}

void
nvkm_i2c_aux_del(struct nvkm_i2c_aux **paux)
{
	struct nvkm_i2c_aux *aux = *paux;
	if (aux && !WARN_ON(!aux->func)) {
		AUX_TRACE(aux, "dtor");
		list_del(&aux->head);
		i2c_del_adapter(&aux->i2c);
		kfree(*paux);
		*paux = NULL;
	}
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
void
nvkm_i2c_aux_init(struct nvkm_i2c_aux *aux)
{
	AUX_TRACE(aux, "init");
	mutex_lock(&aux->mutex);
	aux->enabled = true;
	mutex_unlock(&aux->mutex);
}

void
nvkm_i2c_aux_fini(struct nvkm_i2c_aux *aux)
{
	AUX_TRACE(aux, "fini");
	mutex_lock(&aux->mutex);
	aux->enabled = false;
	mutex_unlock(&aux->mutex);
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
int
nvkm_i2c_aux_ctor(const struct nvkm_i2c_aux_func *func,
		  struct nvkm_i2c_pad *pad, int id,
		  struct nvkm_i2c_aux *aux)
{
	struct nvkm_device *device = pad->i2c->subdev.device;

	aux->func = func;
	aux->pad = pad;
	aux->id = id;
	mutex_init(&aux->mutex);
	list_add_tail(&aux->head, &pad->i2c->aux);
	AUX_TRACE(aux, "ctor");

	snprintf(aux->i2c.name, sizeof(aux->i2c.name), "nvkm-%s-aux-%04x",
		 dev_name(device->dev), id);
	aux->i2c.owner = THIS_MODULE;
	aux->i2c.dev.parent = device->dev;
	aux->i2c.algo = &nvkm_i2c_aux_i2c_algo;
	return i2c_add_adapter(&aux->i2c);
}

int
nvkm_i2c_aux_new_(const struct nvkm_i2c_aux_func *func,
		  struct nvkm_i2c_pad *pad, int id,
		  struct nvkm_i2c_aux **paux)
{
	if (!(*paux = kzalloc(sizeof(**paux), GFP_KERNEL)))
		return -ENOMEM;
	return nvkm_i2c_aux_ctor(func, pad, id, *paux);
}