1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "pad.h"
25
26static void
27nvkm_i2c_pad_mode_locked(struct nvkm_i2c_pad *pad, enum nvkm_i2c_pad_mode mode)
28{
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);
33}
34
35void
36nvkm_i2c_pad_mode(struct nvkm_i2c_pad *pad, enum nvkm_i2c_pad_mode mode)
37{
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);
43}
44
45void
46nvkm_i2c_pad_release(struct nvkm_i2c_pad *pad)
47{
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);
52}
53
54int
55nvkm_i2c_pad_acquire(struct nvkm_i2c_pad *pad, enum nvkm_i2c_pad_mode mode)
56{
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;
63 }
64 nvkm_i2c_pad_mode_locked(pad, mode);
65 }
66 return 0;
67}
68
69void
70nvkm_i2c_pad_fini(struct nvkm_i2c_pad *pad)
71{
72 PAD_TRACE(pad, "fini");
73 nvkm_i2c_pad_mode_locked(pad, NVKM_I2C_PAD_OFF);
74}
75
76void
77nvkm_i2c_pad_init(struct nvkm_i2c_pad *pad)
78{
79 PAD_TRACE(pad, "init");
80 nvkm_i2c_pad_mode_locked(pad, pad->mode);
81}
82
83void
84nvkm_i2c_pad_del(struct nvkm_i2c_pad **ppad)
85{
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;
92 }
93}
94
95void
96nvkm_i2c_pad_ctor(const struct nvkm_i2c_pad_func *func, struct nvkm_i2c *i2c,
97 int id, struct nvkm_i2c_pad *pad)
98{
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");
106}
107
108int
109nvkm_i2c_pad_new_(const struct nvkm_i2c_pad_func *func, struct nvkm_i2c *i2c,
110 int id, struct nvkm_i2c_pad **ppad)
111{
112 if (!(*ppad = kzalloc(sizeof(**ppad), GFP_KERNEL)))
113 return -ENOMEM;
114 nvkm_i2c_pad_ctor(func, i2c, id, *ppad);
115 return 0;
116}
117