summaryrefslogtreecommitdiff
path: root/usr/src/uts/sun4u/io/i2c/misc/i2c_svc.c
blob: ef902b31f435e6e559730a684b2f5c03c4c706a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/types.h>
#include <sys/conf.h>
#include <sys/kmem.h>
#include <sys/open.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/file.h>
#include <sys/modctl.h>
#include <sys/i2c/misc/i2c_svc.h>
#include <sys/i2c/misc/i2c_svc_impl.h>

kmutex_t i2c_svc_mutex;

static struct modldrv i2c_modldrv = {
	&mod_miscops,		/* type of module - misc */
	"I2C module",
	NULL,
};

static struct modlinkage i2c_modlinkage = {
	MODREV_1,
	&i2c_modldrv,
	0
};

i2c_nexus_reg_list_t *nexus_reg_list_head = NULL;

int i2csvcdebug = 0;

int
_init(void)
{
	int error;

	if ((error = mod_install(&i2c_modlinkage)) == 0) {
		mutex_init(&i2c_svc_mutex, NULL, MUTEX_DRIVER, NULL);
	}

	return (error);
}

int
_fini(void)
{
	int error;

	if ((error = mod_remove(&i2c_modlinkage)) == 0) {
		mutex_destroy(&i2c_svc_mutex);
	}

	return (error);
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&i2c_modlinkage, modinfop));
}

/*
 * i2c_client_register is called by I2C client drivers,
 * typically in attach, but before starting any bus transfers.
 *
 * dip	   - the client device's dip.
 * i2c_hdl - pointer to a handle returned on success.
 *
 */
int
i2c_client_register(dev_info_t *dip, i2c_client_hdl_t *i2c_hdl)
{
	dev_info_t *pdip;
	i2c_client_hdl_t hdl;
	i2c_nexus_reg_list_t *reg_list;

	pdip = ddi_get_parent(dip);

	mutex_enter(&i2c_svc_mutex);

	reg_list = nexus_reg_list_head;
	/*
	 * search parent reg list to find dip's parent.
	 */
	for (; reg_list != NULL; reg_list = reg_list->next) {
		if (reg_list->dip == pdip) {
			break;
		}
	}

	mutex_exit(&i2c_svc_mutex);

	if (reg_list == NULL) {

		return (I2C_FAILURE);
	}

	hdl = kmem_alloc(sizeof (struct i2c_client_hdl_impl), KM_SLEEP);

	CHDL(hdl)->chdl_dip = dip;
	CHDL(hdl)->chdl_nexus_reg = &reg_list->nexus_reg;
	*i2c_hdl = hdl;

	return (I2C_SUCCESS);
}

/*
 * i2c_client_unregister() is called by the I2C client driver
 * when it no longer wishes to transmit on the I2C bus, typically
 * during its detach routine.
 *
 * hdl - handle previously returned by i2c_client_register().
 *
 */
void
i2c_client_unregister(i2c_client_hdl_t hdl)
{
	kmem_free(hdl, sizeof (struct i2c_client_hdl_impl));
}

/*
 * i2c_transfer() is called by client drivers to handle
 * I2C data transfers.  It performs some basic sanity checking of
 * flags vs. i2c_len and i2c_wlen values, and then calls the
 * parent's i2c_transfer() function to handle the actual transfer.
 */
int
i2c_transfer(i2c_client_hdl_t hdl, i2c_transfer_t *i2c_tran)
{
	switch (i2c_tran->i2c_flags) {
	case I2C_WR:
		if (i2c_tran->i2c_wlen == 0) {

			return (EINVAL);
		}
		break;
	case I2C_RD:
		if (i2c_tran->i2c_rlen == 0) {

			return (EINVAL);
		}
		break;
	case I2C_WR_RD:
		if (i2c_tran->i2c_wlen == 0 || i2c_tran->i2c_rlen == 0) {

			return (EINVAL);
		}
		break;
	default:

		return (EINVAL);
	}

	if (CHDL(hdl)->chdl_nexus_reg->i2c_nexus_transfer != NULL) {
		(*CHDL(hdl)->chdl_nexus_reg->i2c_nexus_transfer)
				(CHDL(hdl)->chdl_dip, i2c_tran);

		return (i2c_tran->i2c_result);
	} else {

		return (ENOTSUP);
	}
}

/*
 * i2c_transfer_alloc() allocates a i2c_transfer structure along
 * with read and write buffers of size rlen and wlen respectively.
 *
 * i2c_hdl - handle returned previously by i2c_client_register()
 * i2c - address of pointer to allocated buffer returned on success.
 * wlen - write size buffer to allocate.  May be 0.
 * rlen - read size buffer to allocate.  May be 0.
 * flags - I2C_SLEEP or I2C_NOSLEEP
 */
/*ARGSUSED*/
int
i2c_transfer_alloc(i2c_client_hdl_t hdl,
			i2c_transfer_t **i2c,
			ushort_t wlen,
			ushort_t rlen,
			uint_t flags)
{
	i2c_transfer_alloc_t *i2cw;
	int sleep;
	int size;

	/*
	 * set i2c to NULL in case the caller just checks i2c
	 * to determine failures.
	 */
	*i2c = NULL;

	if (flags & I2C_SLEEP) {
		sleep = KM_SLEEP;
	} else if (flags & I2C_NOSLEEP) {
		sleep = KM_NOSLEEP;
	} else {
		sleep = KM_NOSLEEP;
	}

	size = sizeof (i2c_transfer_alloc_t) + rlen + wlen;

	if ((i2cw = kmem_zalloc(size, sleep)) == NULL) {

		return (I2C_FAILURE);
	}

	i2cw->i2cw_size = size;
	i2cw->i2cw_i2ct.i2c_wlen = wlen;
	i2cw->i2cw_i2ct.i2c_rlen = rlen;
	if (wlen != 0) {
		i2cw->i2cw_i2ct.i2c_wbuf = (uchar_t *)i2cw +
			sizeof (i2c_transfer_alloc_t);
	}
	if (rlen != 0) {
		i2cw->i2cw_i2ct.i2c_rbuf = (uchar_t *)i2cw +
			sizeof (i2c_transfer_alloc_t) + wlen;
	}
	*i2c = (i2c_transfer_t *)i2cw;

	return (I2C_SUCCESS);
}

/*
 * i2c_transfer_free() is called to free a buffer previously
 * allocated by i2c_transfer_allocate().
 *
 * i2c_hdl - handle returned previously by i2c_client_register()
 * i2c - buffer previously allocated by i2c_transfer_allocate()
 */
/*ARGSUSED*/
void
i2c_transfer_free(i2c_client_hdl_t hdl, i2c_transfer_t *i2c_tran)
{
	i2c_transfer_alloc_t *i2cw = (i2c_transfer_alloc_t *)i2c_tran;

	kmem_free(i2cw, i2cw->i2cw_size);
}

/*
 * i2c_nexus_register() is called by the nexus driver to inform
 * I2C services that it is ready to accept transactions, and
 * give the I2C services a vector of functions.
 *
 * dip - dip of the bus controller
 * nexus_reg - pointer to reg structure of vector functions
 */
void
i2c_nexus_register(dev_info_t *dip, i2c_nexus_reg_t *nexus_reg)
{
	i2c_nexus_reg_list_t *nexus_reglist;

	nexus_reglist = kmem_alloc(sizeof (struct i2c_nexus_reg_list),
		KM_SLEEP);

	mutex_enter(&i2c_svc_mutex);
	nexus_reglist->next = nexus_reg_list_head;
	nexus_reg_list_head = nexus_reglist;
	mutex_exit(&i2c_svc_mutex);

	nexus_reglist->nexus_reg = *nexus_reg;
	nexus_reglist->dip = dip;
}

/*
 * i2c_nexus_unregister() is called by the nexus driver when
 * it is no longer able to accept transactions for its I2C
 * children.
 *
 * dip - dev_info pointer passed to i2c_nexus_register().
 */
void
i2c_nexus_unregister(dev_info_t *dip)
{
	i2c_nexus_reg_list_t **reg_list;
	i2c_nexus_reg_list_t *save = NULL;

	mutex_enter(&i2c_svc_mutex);

	reg_list = &nexus_reg_list_head;

	/*
	 * reg_list is the address of the pointer to an element on
	 * the reg list.  It starts out being the address of the
	 * list head, but then is changed to the address of the
	 * next pointer in a list element.  Once the element to
	 * delete is found, then we change the pointer to the
	 * address found in the next pointer of the element to
	 * be deleted.
	 */
	for (; *reg_list != NULL; reg_list = &(*reg_list)->next) {
		if ((*reg_list)->dip == dip) {
			save = *reg_list;
			/* prev next pointer adjusted to point */
			*reg_list = (*reg_list)->next;
			break;
		}
	}
	mutex_exit(&i2c_svc_mutex);
	if (save != NULL) {
		kmem_free(save, sizeof (i2c_nexus_reg_list_t));
	} else {
		cmn_err(CE_WARN, "could not find nexus reg to free");
	}
}