xref: /linux/drivers/gpu/drm/nouveau/nvkm/subdev/i2c/pad.c (revision 8ca4fc323d2e4ab9dabbdd57633af40b0c7e6af9)
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 "pad.h"
25 
26 static void
27 nvkm_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 
35 void
36 nvkm_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 
45 void
46 nvkm_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 
54 int
55 nvkm_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 
69 void
70 nvkm_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 
76 void
77 nvkm_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 
83 void
84 nvkm_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 
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)
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 
108 int
109 nvkm_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