linux/drivers/gpu/drm/nouveau/nvkm/subdev/i2c/aux.c
<<
>>
Prefs
   1/*
   2 * Copyright 2009 Red Hat Inc.
   3 *
   4 * Permission is hereby granted, free of charge, to any person obtaining a
   5 * copy of this software and associated documentation files (the "Software"),
   6 * to deal in the Software without restriction, including without limitation
   7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
   8 * and/or sell copies of the Software, and to permit persons to whom the
   9 * Software is furnished to do so, subject to the following conditions:
  10 *
  11 * The above copyright notice and this permission notice shall be included in
  12 * all copies or substantial portions of the Software.
  13 *
  14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20 * OTHER DEALINGS IN THE SOFTWARE.
  21 *
  22 * Authors: Ben Skeggs
  23 */
  24#include "aux.h"
  25#include "pad.h"
  26
  27static int
  28nvkm_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        struct i2c_msg *msg = msgs;
  32        int ret, mcnt = num;
  33
  34        ret = nvkm_i2c_aux_acquire(aux);
  35        if (ret)
  36                return ret;
  37
  38        while (mcnt--) {
  39                u8 remaining = msg->len;
  40                u8 *ptr = msg->buf;
  41
  42                while (remaining) {
  43                        u8 cnt = (remaining > 16) ? 16 : remaining;
  44                        u8 cmd;
  45
  46                        if (msg->flags & I2C_M_RD)
  47                                cmd = 1;
  48                        else
  49                                cmd = 0;
  50
  51                        if (mcnt || remaining > 16)
  52                                cmd |= 4; /* MOT */
  53
  54                        ret = aux->func->xfer(aux, true, cmd, msg->addr, ptr, &cnt);
  55                        if (ret < 0) {
  56                                nvkm_i2c_aux_release(aux);
  57                                return ret;
  58                        }
  59
  60                        ptr += cnt;
  61                        remaining -= cnt;
  62                }
  63
  64                msg++;
  65        }
  66
  67        nvkm_i2c_aux_release(aux);
  68        return num;
  69}
  70
  71static u32
  72nvkm_i2c_aux_i2c_func(struct i2c_adapter *adap)
  73{
  74        return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  75}
  76
  77static const struct i2c_algorithm
  78nvkm_i2c_aux_i2c_algo = {
  79        .master_xfer = nvkm_i2c_aux_i2c_xfer,
  80        .functionality = nvkm_i2c_aux_i2c_func
  81};
  82
  83void
  84nvkm_i2c_aux_monitor(struct nvkm_i2c_aux *aux, bool monitor)
  85{
  86        struct nvkm_i2c_pad *pad = aux->pad;
  87        AUX_TRACE(aux, "monitor: %s", monitor ? "yes" : "no");
  88        if (monitor)
  89                nvkm_i2c_pad_mode(pad, NVKM_I2C_PAD_AUX);
  90        else
  91                nvkm_i2c_pad_mode(pad, NVKM_I2C_PAD_OFF);
  92}
  93
  94void
  95nvkm_i2c_aux_release(struct nvkm_i2c_aux *aux)
  96{
  97        struct nvkm_i2c_pad *pad = aux->pad;
  98        AUX_TRACE(aux, "release");
  99        nvkm_i2c_pad_release(pad);
 100        mutex_unlock(&aux->mutex);
 101}
 102
 103int
 104nvkm_i2c_aux_acquire(struct nvkm_i2c_aux *aux)
 105{
 106        struct nvkm_i2c_pad *pad = aux->pad;
 107        int ret;
 108
 109        AUX_TRACE(aux, "acquire");
 110        mutex_lock(&aux->mutex);
 111
 112        if (aux->enabled)
 113                ret = nvkm_i2c_pad_acquire(pad, NVKM_I2C_PAD_AUX);
 114        else
 115                ret = -EIO;
 116
 117        if (ret)
 118                mutex_unlock(&aux->mutex);
 119        return ret;
 120}
 121
 122int
 123nvkm_i2c_aux_xfer(struct nvkm_i2c_aux *aux, bool retry, u8 type,
 124                  u32 addr, u8 *data, u8 *size)
 125{
 126        if (!*size && !aux->func->address_only) {
 127                AUX_ERR(aux, "address-only transaction dropped");
 128                return -ENOSYS;
 129        }
 130        return aux->func->xfer(aux, retry, type, addr, data, size);
 131}
 132
 133int
 134nvkm_i2c_aux_lnk_ctl(struct nvkm_i2c_aux *aux, int nr, int bw, bool ef)
 135{
 136        if (aux->func->lnk_ctl)
 137                return aux->func->lnk_ctl(aux, nr, bw, ef);
 138        return -ENODEV;
 139}
 140
 141void
 142nvkm_i2c_aux_del(struct nvkm_i2c_aux **paux)
 143{
 144        struct nvkm_i2c_aux *aux = *paux;
 145        if (aux && !WARN_ON(!aux->func)) {
 146                AUX_TRACE(aux, "dtor");
 147                list_del(&aux->head);
 148                i2c_del_adapter(&aux->i2c);
 149                kfree(*paux);
 150                *paux = NULL;
 151        }
 152}
 153
 154void
 155nvkm_i2c_aux_init(struct nvkm_i2c_aux *aux)
 156{
 157        AUX_TRACE(aux, "init");
 158        mutex_lock(&aux->mutex);
 159        aux->enabled = true;
 160        mutex_unlock(&aux->mutex);
 161}
 162
 163void
 164nvkm_i2c_aux_fini(struct nvkm_i2c_aux *aux)
 165{
 166        AUX_TRACE(aux, "fini");
 167        mutex_lock(&aux->mutex);
 168        aux->enabled = false;
 169        mutex_unlock(&aux->mutex);
 170}
 171
 172int
 173nvkm_i2c_aux_ctor(const struct nvkm_i2c_aux_func *func,
 174                  struct nvkm_i2c_pad *pad, int id,
 175                  struct nvkm_i2c_aux *aux)
 176{
 177        struct nvkm_device *device = pad->i2c->subdev.device;
 178
 179        aux->func = func;
 180        aux->pad = pad;
 181        aux->id = id;
 182        mutex_init(&aux->mutex);
 183        list_add_tail(&aux->head, &pad->i2c->aux);
 184        AUX_TRACE(aux, "ctor");
 185
 186        snprintf(aux->i2c.name, sizeof(aux->i2c.name), "nvkm-%s-aux-%04x",
 187                 dev_name(device->dev), id);
 188        aux->i2c.owner = THIS_MODULE;
 189        aux->i2c.dev.parent = device->dev;
 190        aux->i2c.algo = &nvkm_i2c_aux_i2c_algo;
 191        return i2c_add_adapter(&aux->i2c);
 192}
 193
 194int
 195nvkm_i2c_aux_new_(const struct nvkm_i2c_aux_func *func,
 196                  struct nvkm_i2c_pad *pad, int id,
 197                  struct nvkm_i2c_aux **paux)
 198{
 199        if (!(*paux = kzalloc(sizeof(**paux), GFP_KERNEL)))
 200                return -ENOMEM;
 201        return nvkm_i2c_aux_ctor(func, pad, id, *paux);
 202}
 203