WIP FPC-III support
[linux/fpc-iii.git] / drivers / gpu / drm / nouveau / nvkm / subdev / i2c / pad.c
blob2c5fcb9c504b2c79652f87c1d26bb0dd296c21b5
1 /*
2 * Copyright 2015 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Ben Skeggs <bskeggs@redhat.com>
24 #include "pad.h"
26 static void
27 nvkm_i2c_pad_mode_locked(struct nvkm_i2c_pad *pad, enum nvkm_i2c_pad_mode mode)
29 PAD_TRACE(pad, "-> %s", (mode == NVKM_I2C_PAD_AUX) ? "aux" :
30 (mode == NVKM_I2C_PAD_I2C) ? "i2c" : "off");
31 if (pad->func->mode)
32 pad->func->mode(pad, mode);
35 void
36 nvkm_i2c_pad_mode(struct nvkm_i2c_pad *pad, enum nvkm_i2c_pad_mode mode)
38 PAD_TRACE(pad, "mode %d", mode);
39 mutex_lock(&pad->mutex);
40 nvkm_i2c_pad_mode_locked(pad, mode);
41 pad->mode = mode;
42 mutex_unlock(&pad->mutex);
45 void
46 nvkm_i2c_pad_release(struct nvkm_i2c_pad *pad)
48 PAD_TRACE(pad, "release");
49 if (pad->mode == NVKM_I2C_PAD_OFF)
50 nvkm_i2c_pad_mode_locked(pad, pad->mode);
51 mutex_unlock(&pad->mutex);
54 int
55 nvkm_i2c_pad_acquire(struct nvkm_i2c_pad *pad, enum nvkm_i2c_pad_mode mode)
57 PAD_TRACE(pad, "acquire");
58 mutex_lock(&pad->mutex);
59 if (pad->mode != mode) {
60 if (pad->mode != NVKM_I2C_PAD_OFF) {
61 mutex_unlock(&pad->mutex);
62 return -EBUSY;
64 nvkm_i2c_pad_mode_locked(pad, mode);
66 return 0;
69 void
70 nvkm_i2c_pad_fini(struct nvkm_i2c_pad *pad)
72 PAD_TRACE(pad, "fini");
73 nvkm_i2c_pad_mode_locked(pad, NVKM_I2C_PAD_OFF);
76 void
77 nvkm_i2c_pad_init(struct nvkm_i2c_pad *pad)
79 PAD_TRACE(pad, "init");
80 nvkm_i2c_pad_mode_locked(pad, pad->mode);
83 void
84 nvkm_i2c_pad_del(struct nvkm_i2c_pad **ppad)
86 struct nvkm_i2c_pad *pad = *ppad;
87 if (pad) {
88 PAD_TRACE(pad, "dtor");
89 list_del(&pad->head);
90 kfree(pad);
91 pad = NULL;
95 void
96 nvkm_i2c_pad_ctor(const struct nvkm_i2c_pad_func *func, struct nvkm_i2c *i2c,
97 int id, struct nvkm_i2c_pad *pad)
99 pad->func = func;
100 pad->i2c = i2c;
101 pad->id = id;
102 pad->mode = NVKM_I2C_PAD_OFF;
103 mutex_init(&pad->mutex);
104 list_add_tail(&pad->head, &i2c->pad);
105 PAD_TRACE(pad, "ctor");
109 nvkm_i2c_pad_new_(const struct nvkm_i2c_pad_func *func, struct nvkm_i2c *i2c,
110 int id, struct nvkm_i2c_pad **ppad)
112 if (!(*ppad = kzalloc(sizeof(**ppad), GFP_KERNEL)))
113 return -ENOMEM;
114 nvkm_i2c_pad_ctor(func, i2c, id, *ppad);
115 return 0;