aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/pwm/pwmbus.c
blob: cacd21c07709c6f94b91f79e144a92114f05a691 (plain) (blame)
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
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include "opt_platform.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/endian.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/module.h>

#include <dev/pwm/pwmbus.h>

#include "pwmbus_if.h"

/*
 * bus_if methods...
 */

static device_t
pwmbus_add_child(device_t dev, u_int order, const char *name, int unit)
{
	device_t child;
	struct pwmbus_ivars *ivars;

	child = device_add_child_ordered(dev, order, name, unit);
	if (child == NULL) 
		return (child);

	ivars = malloc(sizeof(struct pwmbus_ivars), M_DEVBUF, M_NOWAIT | M_ZERO);
	if (ivars == NULL) {
		device_delete_child(dev, child);
		return (NULL);
	}
	device_set_ivars(child, ivars);

	return (child);
}

static int
pwmbus_child_location_str(device_t dev, device_t child, char *buf, size_t blen)
{
	struct pwmbus_ivars *ivars;

	ivars = device_get_ivars(child);
	snprintf(buf, blen, "hwdev=%s channel=%u", 
	    device_get_nameunit(device_get_parent(dev)), ivars->pi_channel);

	return (0);
}

static int
pwmbus_child_pnpinfo_str(device_t dev, device_t child, char *buf,
    size_t buflen)
{
	*buf = '\0';
	return (0);
}

static void
pwmbus_hinted_child(device_t dev, const char *dname, int dunit)
{
	struct pwmbus_ivars *ivars;
	device_t child;

	child = pwmbus_add_child(dev, 0, dname, dunit);

	/*
	 * If there is a channel hint, use it.  Otherwise pi_channel was
	 * initialized to zero, so that's the channel we'll use.
	 */
	ivars = device_get_ivars(child);
	resource_int_value(dname, dunit, "channel", &ivars->pi_channel);
}

static int
pwmbus_print_child(device_t dev, device_t child)
{
	struct pwmbus_ivars *ivars;
	int rv;

	ivars = device_get_ivars(child);

	rv  = bus_print_child_header(dev, child);
	rv += printf(" channel %u", ivars->pi_channel);
	rv += bus_print_child_footer(dev, child);

	return (rv);
}

static void
pwmbus_probe_nomatch(device_t dev, device_t child)
{
	struct pwmbus_ivars *ivars;

	ivars = device_get_ivars(child);
	if (ivars != NULL)
		device_printf(dev, "<unknown> on channel %u\n",
		    ivars->pi_channel);

	return;
}

static int
pwmbus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
{
	struct pwmbus_ivars *ivars;

	ivars = device_get_ivars(child);

	switch (which) {
	case PWMBUS_IVAR_CHANNEL:
		*(u_int *)result = ivars->pi_channel;
		break;
	default:
		return (EINVAL);
	}

	return (0);
}

/*
 * device_if methods...
 */

static int
pwmbus_probe(device_t dev)
{
	device_set_desc(dev, "PWM bus");
	return (BUS_PROBE_GENERIC);
}

static int
pwmbus_attach(device_t dev)
{
	struct pwmbus_softc *sc;
	struct pwmbus_ivars *ivars;
	device_t child, parent;
	u_int chan;

	sc = device_get_softc(dev);
	sc->dev = dev;
	parent = device_get_parent(dev);

	if (PWMBUS_CHANNEL_COUNT(parent, &sc->nchannels) != 0 ||
	    sc->nchannels == 0) {
		device_printf(sc->dev, "No channels on parent %s\n",
		    device_get_nameunit(parent));
		return (ENXIO);
	}

	/* Add a pwmc(4) child for each channel. */
	for (chan = 0; chan < sc->nchannels; ++chan) {
		if ((child = pwmbus_add_child(sc->dev, 0, "pwmc", -1)) == NULL) {
			device_printf(dev, "failed to add pwmc child device "
			    "for channel %u\n", chan);
			continue;
		}
		ivars = device_get_ivars(child);
		ivars->pi_channel = chan;
	}

	bus_enumerate_hinted_children(dev);
	bus_generic_probe(dev);

	return (bus_generic_attach(dev));
}

static int
pwmbus_detach(device_t dev)
{
	int rv;

	if ((rv = bus_generic_detach(dev)) == 0)
		rv = device_delete_children(dev);

	return (rv);
}

/*
 * pwmbus_if methods...
 */

static int
pwmbus_channel_config(device_t dev, u_int chan, u_int period, u_int duty)
{
	return (PWMBUS_CHANNEL_CONFIG(device_get_parent(dev), chan, period, duty));
}

static int
pwmbus_channel_get_config(device_t dev, u_int chan, u_int *period, u_int *duty)
{
	return (PWMBUS_CHANNEL_GET_CONFIG(device_get_parent(dev), chan, period, duty));
}

static int
pwmbus_channel_get_flags(device_t dev, u_int chan, uint32_t *flags)
{
	return (PWMBUS_CHANNEL_GET_FLAGS(device_get_parent(dev), chan, flags));
}

static int
pwmbus_channel_enable(device_t dev, u_int chan, bool enable)
{
	return (PWMBUS_CHANNEL_ENABLE(device_get_parent(dev), chan, enable));
}

static int
pwmbus_channel_set_flags(device_t dev, u_int chan, uint32_t flags)
{
	return (PWMBUS_CHANNEL_SET_FLAGS(device_get_parent(dev), chan, flags));
}

static int
pwmbus_channel_is_enabled(device_t dev, u_int chan, bool *enable)
{
	return (PWMBUS_CHANNEL_IS_ENABLED(device_get_parent(dev), chan, enable));
}

static int
pwmbus_channel_count(device_t dev, u_int *nchannel)
{
	return (PWMBUS_CHANNEL_COUNT(device_get_parent(dev), nchannel));
}

static device_method_t pwmbus_methods[] = {
	/* device_if */
	DEVMETHOD(device_probe,  pwmbus_probe),
	DEVMETHOD(device_attach, pwmbus_attach),
	DEVMETHOD(device_detach, pwmbus_detach),

        /* bus_if */
	DEVMETHOD(bus_add_child,		pwmbus_add_child),
	DEVMETHOD(bus_child_location_str,	pwmbus_child_location_str),
	DEVMETHOD(bus_child_pnpinfo_str,	pwmbus_child_pnpinfo_str),
	DEVMETHOD(bus_hinted_child,		pwmbus_hinted_child),
	DEVMETHOD(bus_print_child,		pwmbus_print_child),
	DEVMETHOD(bus_probe_nomatch,		pwmbus_probe_nomatch),
	DEVMETHOD(bus_read_ivar,		pwmbus_read_ivar),

        /* pwmbus_if  */
	DEVMETHOD(pwmbus_channel_count,		pwmbus_channel_count),
	DEVMETHOD(pwmbus_channel_config,	pwmbus_channel_config),
	DEVMETHOD(pwmbus_channel_get_config,	pwmbus_channel_get_config),
	DEVMETHOD(pwmbus_channel_set_flags,	pwmbus_channel_set_flags),
	DEVMETHOD(pwmbus_channel_get_flags,	pwmbus_channel_get_flags),
	DEVMETHOD(pwmbus_channel_enable,	pwmbus_channel_enable),
	DEVMETHOD(pwmbus_channel_is_enabled,	pwmbus_channel_is_enabled),

	DEVMETHOD_END
};

driver_t pwmbus_driver = {
	"pwmbus",
	pwmbus_methods,
	sizeof(struct pwmbus_softc),
};
devclass_t pwmbus_devclass;

EARLY_DRIVER_MODULE(pwmbus, pwm, pwmbus_driver, pwmbus_devclass, 0, 0,
  BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
MODULE_VERSION(pwmbus, 1);