root/drivers/gpu/drm/nouveau/nvkm/subdev/i2c/bus.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. nvkm_i2c_bus_pre_xfer
  2. nvkm_i2c_bus_post_xfer
  3. nvkm_i2c_bus_setscl
  4. nvkm_i2c_bus_setsda
  5. nvkm_i2c_bus_getscl
  6. nvkm_i2c_bus_getsda
  7. nvkm_i2c_bus_xfer
  8. nvkm_i2c_bus_func
  9. nvkm_i2c_bus_init
  10. nvkm_i2c_bus_fini
  11. nvkm_i2c_bus_release
  12. nvkm_i2c_bus_acquire
  13. nvkm_i2c_bus_probe
  14. nvkm_i2c_bus_del
  15. nvkm_i2c_bus_ctor
  16. nvkm_i2c_bus_new_

   1 /*
   2  * Copyright 2015 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 <bskeggs@redhat.com>
  23  */
  24 #include "bus.h"
  25 #include "pad.h"
  26 
  27 #include <core/option.h>
  28 
  29 /*******************************************************************************
  30  * i2c-algo-bit
  31  ******************************************************************************/
  32 static int
  33 nvkm_i2c_bus_pre_xfer(struct i2c_adapter *adap)
  34 {
  35         struct nvkm_i2c_bus *bus = container_of(adap, typeof(*bus), i2c);
  36         return nvkm_i2c_bus_acquire(bus);
  37 }
  38 
  39 static void
  40 nvkm_i2c_bus_post_xfer(struct i2c_adapter *adap)
  41 {
  42         struct nvkm_i2c_bus *bus = container_of(adap, typeof(*bus), i2c);
  43         return nvkm_i2c_bus_release(bus);
  44 }
  45 
  46 static void
  47 nvkm_i2c_bus_setscl(void *data, int state)
  48 {
  49         struct nvkm_i2c_bus *bus = data;
  50         bus->func->drive_scl(bus, state);
  51 }
  52 
  53 static void
  54 nvkm_i2c_bus_setsda(void *data, int state)
  55 {
  56         struct nvkm_i2c_bus *bus = data;
  57         bus->func->drive_sda(bus, state);
  58 }
  59 
  60 static int
  61 nvkm_i2c_bus_getscl(void *data)
  62 {
  63         struct nvkm_i2c_bus *bus = data;
  64         return bus->func->sense_scl(bus);
  65 }
  66 
  67 static int
  68 nvkm_i2c_bus_getsda(void *data)
  69 {
  70         struct nvkm_i2c_bus *bus = data;
  71         return bus->func->sense_sda(bus);
  72 }
  73 
  74 /*******************************************************************************
  75  * !i2c-algo-bit (off-chip i2c bus / hw i2c / internal bit-banging algo)
  76  ******************************************************************************/
  77 static int
  78 nvkm_i2c_bus_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  79 {
  80         struct nvkm_i2c_bus *bus = container_of(adap, typeof(*bus), i2c);
  81         int ret;
  82 
  83         ret = nvkm_i2c_bus_acquire(bus);
  84         if (ret)
  85                 return ret;
  86 
  87         ret = bus->func->xfer(bus, msgs, num);
  88         nvkm_i2c_bus_release(bus);
  89         return ret;
  90 }
  91 
  92 static u32
  93 nvkm_i2c_bus_func(struct i2c_adapter *adap)
  94 {
  95         return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  96 }
  97 
  98 static const struct i2c_algorithm
  99 nvkm_i2c_bus_algo = {
 100         .master_xfer = nvkm_i2c_bus_xfer,
 101         .functionality = nvkm_i2c_bus_func,
 102 };
 103 
 104 /*******************************************************************************
 105  * nvkm_i2c_bus base
 106  ******************************************************************************/
 107 void
 108 nvkm_i2c_bus_init(struct nvkm_i2c_bus *bus)
 109 {
 110         BUS_TRACE(bus, "init");
 111         if (bus->func->init)
 112                 bus->func->init(bus);
 113 
 114         mutex_lock(&bus->mutex);
 115         bus->enabled = true;
 116         mutex_unlock(&bus->mutex);
 117 }
 118 
 119 void
 120 nvkm_i2c_bus_fini(struct nvkm_i2c_bus *bus)
 121 {
 122         BUS_TRACE(bus, "fini");
 123         mutex_lock(&bus->mutex);
 124         bus->enabled = false;
 125         mutex_unlock(&bus->mutex);
 126 }
 127 
 128 void
 129 nvkm_i2c_bus_release(struct nvkm_i2c_bus *bus)
 130 {
 131         struct nvkm_i2c_pad *pad = bus->pad;
 132         BUS_TRACE(bus, "release");
 133         nvkm_i2c_pad_release(pad);
 134         mutex_unlock(&bus->mutex);
 135 }
 136 
 137 int
 138 nvkm_i2c_bus_acquire(struct nvkm_i2c_bus *bus)
 139 {
 140         struct nvkm_i2c_pad *pad = bus->pad;
 141         int ret;
 142 
 143         BUS_TRACE(bus, "acquire");
 144         mutex_lock(&bus->mutex);
 145 
 146         if (bus->enabled)
 147                 ret = nvkm_i2c_pad_acquire(pad, NVKM_I2C_PAD_I2C);
 148         else
 149                 ret = -EIO;
 150 
 151         if (ret)
 152                 mutex_unlock(&bus->mutex);
 153         return ret;
 154 }
 155 
 156 int
 157 nvkm_i2c_bus_probe(struct nvkm_i2c_bus *bus, const char *what,
 158                    struct nvkm_i2c_bus_probe *info,
 159                    bool (*match)(struct nvkm_i2c_bus *,
 160                                  struct i2c_board_info *, void *), void *data)
 161 {
 162         int i;
 163 
 164         BUS_DBG(bus, "probing %ss", what);
 165         for (i = 0; info[i].dev.addr; i++) {
 166                 u8 orig_udelay = 0;
 167 
 168                 if ((bus->i2c.algo == &i2c_bit_algo) && (info[i].udelay != 0)) {
 169                         struct i2c_algo_bit_data *algo = bus->i2c.algo_data;
 170                         BUS_DBG(bus, "%dms delay instead of %dms",
 171                                      info[i].udelay, algo->udelay);
 172                         orig_udelay = algo->udelay;
 173                         algo->udelay = info[i].udelay;
 174                 }
 175 
 176                 if (nvkm_probe_i2c(&bus->i2c, info[i].dev.addr) &&
 177                     (!match || match(bus, &info[i].dev, data))) {
 178                         BUS_DBG(bus, "detected %s: %s",
 179                                 what, info[i].dev.type);
 180                         return i;
 181                 }
 182 
 183                 if (orig_udelay) {
 184                         struct i2c_algo_bit_data *algo = bus->i2c.algo_data;
 185                         algo->udelay = orig_udelay;
 186                 }
 187         }
 188 
 189         BUS_DBG(bus, "no devices found.");
 190         return -ENODEV;
 191 }
 192 
 193 void
 194 nvkm_i2c_bus_del(struct nvkm_i2c_bus **pbus)
 195 {
 196         struct nvkm_i2c_bus *bus = *pbus;
 197         if (bus && !WARN_ON(!bus->func)) {
 198                 BUS_TRACE(bus, "dtor");
 199                 list_del(&bus->head);
 200                 i2c_del_adapter(&bus->i2c);
 201                 kfree(bus->i2c.algo_data);
 202                 kfree(*pbus);
 203                 *pbus = NULL;
 204         }
 205 }
 206 
 207 int
 208 nvkm_i2c_bus_ctor(const struct nvkm_i2c_bus_func *func,
 209                   struct nvkm_i2c_pad *pad, int id,
 210                   struct nvkm_i2c_bus *bus)
 211 {
 212         struct nvkm_device *device = pad->i2c->subdev.device;
 213         struct i2c_algo_bit_data *bit;
 214 #ifndef CONFIG_NOUVEAU_I2C_INTERNAL_DEFAULT
 215         const bool internal = false;
 216 #else
 217         const bool internal = true;
 218 #endif
 219         int ret;
 220 
 221         bus->func = func;
 222         bus->pad = pad;
 223         bus->id = id;
 224         mutex_init(&bus->mutex);
 225         list_add_tail(&bus->head, &pad->i2c->bus);
 226         BUS_TRACE(bus, "ctor");
 227 
 228         snprintf(bus->i2c.name, sizeof(bus->i2c.name), "nvkm-%s-bus-%04x",
 229                  dev_name(device->dev), id);
 230         bus->i2c.owner = THIS_MODULE;
 231         bus->i2c.dev.parent = device->dev;
 232 
 233         if ( bus->func->drive_scl &&
 234             !nvkm_boolopt(device->cfgopt, "NvI2C", internal)) {
 235                 if (!(bit = kzalloc(sizeof(*bit), GFP_KERNEL)))
 236                         return -ENOMEM;
 237                 bit->udelay = 10;
 238                 bit->timeout = usecs_to_jiffies(2200);
 239                 bit->data = bus;
 240                 bit->pre_xfer = nvkm_i2c_bus_pre_xfer;
 241                 bit->post_xfer = nvkm_i2c_bus_post_xfer;
 242                 bit->setscl = nvkm_i2c_bus_setscl;
 243                 bit->setsda = nvkm_i2c_bus_setsda;
 244                 bit->getscl = nvkm_i2c_bus_getscl;
 245                 bit->getsda = nvkm_i2c_bus_getsda;
 246                 bus->i2c.algo_data = bit;
 247                 ret = i2c_bit_add_bus(&bus->i2c);
 248         } else {
 249                 bus->i2c.algo = &nvkm_i2c_bus_algo;
 250                 ret = i2c_add_adapter(&bus->i2c);
 251         }
 252 
 253         return ret;
 254 }
 255 
 256 int
 257 nvkm_i2c_bus_new_(const struct nvkm_i2c_bus_func *func,
 258                   struct nvkm_i2c_pad *pad, int id,
 259                   struct nvkm_i2c_bus **pbus)
 260 {
 261         if (!(*pbus = kzalloc(sizeof(**pbus), GFP_KERNEL)))
 262                 return -ENOMEM;
 263         return nvkm_i2c_bus_ctor(func, pad, id, *pbus);
 264 }

/* [<][>][^][v][top][bottom][index][help] */