twl_clks.c revision 257200
1/*-
2 * Copyright (c) 2012
3 *	Ben Gray <bgray@freebsd.org>.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/sys/arm/ti/twl/twl_clks.c 257200 2013-10-27 01:34:10Z ian $");
30
31/*
32 * Texas Instruments TWL4030/TWL5030/TWL60x0/TPS659x0 Power Management.
33 *
34 * This driver covers the external clocks, allows for enabling &
35 * disabling their output.
36 *
37 *
38 *
39 * FLATTENED DEVICE TREE (FDT)
40 * Startup override settings can be specified in the FDT, if they are they
41 * should be under the twl parent device and take the following form:
42 *
43 *    external-clocks = "name1", "state1",
44 *                      "name2", "state2",
45 *                      etc;
46 *
47 * Each override should be a pair, the first entry is the name of the clock
48 * the second is the state to set, possible strings are either "on" or "off".
49 *
50 */
51
52#include <sys/param.h>
53#include <sys/systm.h>
54#include <sys/kernel.h>
55#include <sys/lock.h>
56#include <sys/module.h>
57#include <sys/bus.h>
58#include <sys/resource.h>
59#include <sys/rman.h>
60#include <sys/sysctl.h>
61#include <sys/sx.h>
62#include <sys/malloc.h>
63
64#include <machine/bus.h>
65#include <machine/cpu.h>
66#include <machine/cpufunc.h>
67#include <machine/resource.h>
68#include <machine/intr.h>
69
70#include <dev/ofw/openfirm.h>
71#include <dev/ofw/ofw_bus.h>
72
73#include "twl.h"
74#include "twl_clks.h"
75
76
77static int twl_clks_debug = 1;
78
79
80/*
81 * Power Groups bits for the 4030 and 6030 devices
82 */
83#define TWL4030_P3_GRP		0x80	/* Peripherals, power group */
84#define TWL4030_P2_GRP		0x40	/* Modem power group */
85#define TWL4030_P1_GRP		0x20	/* Application power group (FreeBSD control) */
86
87#define TWL6030_P3_GRP		0x04	/* Modem power group */
88#define TWL6030_P2_GRP		0x02	/* Connectivity power group */
89#define TWL6030_P1_GRP		0x01	/* Application power group (FreeBSD control) */
90
91/*
92 * Register offsets within a clk regulator register set
93 */
94#define TWL_CLKS_GRP		0x00	/* Regulator GRP register */
95#define TWL_CLKS_STATE		0x02	/* TWL6030 only */
96
97
98
99/**
100 *  Support voltage regulators for the different IC's
101 */
102struct twl_clock {
103	const char	*name;
104	uint8_t		subdev;
105	uint8_t		regbase;
106};
107
108static const struct twl_clock twl4030_clocks[] = {
109	{ "32kclkout", 0, 0x8e },
110	{ NULL, 0, 0x00 }
111};
112
113static const struct twl_clock twl6030_clocks[] = {
114	{ "clk32kg",     0, 0xbc },
115	{ "clk32kao",    0, 0xb9 },
116	{ "clk32kaudio", 0, 0xbf },
117	{ NULL, 0, 0x00 }
118};
119
120#define TWL_CLKS_MAX_NAMELEN  32
121
122struct twl_clk_entry {
123	LIST_ENTRY(twl_clk_entry) link;
124	struct sysctl_oid *oid;
125	char		       name[TWL_CLKS_MAX_NAMELEN];
126	uint8_t            sub_dev;  /* the sub-device number for the clock */
127	uint8_t            reg_off;  /* register base address of the clock */
128};
129
130struct twl_clks_softc {
131	device_t           sc_dev;   /* twl_clk device */
132	device_t           sc_pdev;  /* parent device (twl) */
133	struct sx          sc_sx;    /* internal locking */
134	struct intr_config_hook sc_init_hook;
135	LIST_HEAD(twl_clk_list, twl_clk_entry) sc_clks_list;
136};
137
138/**
139 *	Macros for driver shared locking
140 */
141#define TWL_CLKS_XLOCK(_sc)			sx_xlock(&(_sc)->sc_sx)
142#define	TWL_CLKS_XUNLOCK(_sc)		sx_xunlock(&(_sc)->sc_sx)
143#define TWL_CLKS_SLOCK(_sc)			sx_slock(&(_sc)->sc_sx)
144#define	TWL_CLKS_SUNLOCK(_sc)		sx_sunlock(&(_sc)->sc_sx)
145#define TWL_CLKS_LOCK_INIT(_sc)		sx_init(&(_sc)->sc_sx, "twl_clks")
146#define TWL_CLKS_LOCK_DESTROY(_sc)	sx_destroy(&(_sc)->sc_sx);
147
148#define TWL_CLKS_ASSERT_LOCKED(_sc)	sx_assert(&(_sc)->sc_sx, SA_LOCKED);
149
150#define TWL_CLKS_LOCK_UPGRADE(_sc)               \
151	do {                                         \
152		while (!sx_try_upgrade(&(_sc)->sc_sx))   \
153			pause("twl_clks_ex", (hz / 100));    \
154	} while(0)
155#define TWL_CLKS_LOCK_DOWNGRADE(_sc)	sx_downgrade(&(_sc)->sc_sx);
156
157
158
159
160/**
161 *	twl_clks_read_1 - read single register from the TWL device
162 *	twl_clks_write_1 - writes a single register in the TWL device
163 *	@sc: device context
164 *	@clk: the clock device we're reading from / writing to
165 *	@off: offset within the clock's register set
166 *	@val: the value to write or a pointer to a variable to store the result
167 *
168 *	RETURNS:
169 *	Zero on success or an error code on failure.
170 */
171static inline int
172twl_clks_read_1(struct twl_clks_softc *sc, struct twl_clk_entry *clk,
173	uint8_t off, uint8_t *val)
174{
175	return (twl_read(sc->sc_pdev, clk->sub_dev, clk->reg_off + off, val, 1));
176}
177
178static inline int
179twl_clks_write_1(struct twl_clks_softc *sc, struct twl_clk_entry *clk,
180	uint8_t off, uint8_t val)
181{
182	return (twl_write(sc->sc_pdev, clk->sub_dev, clk->reg_off + off, &val, 1));
183}
184
185
186/**
187 *	twl_clks_is_enabled - determines if a clock is enabled
188 *	@dev: TWL CLK device
189 *	@name: the name of the clock
190 *	@enabled: upon return will contain the 'enabled' state
191 *
192 *	LOCKING:
193 *	Internally the function takes and releases the TWL lock.
194 *
195 *	RETURNS:
196 *	Zero on success or a negative error code on failure.
197 */
198int
199twl_clks_is_enabled(device_t dev, const char *name, int *enabled)
200{
201	struct twl_clks_softc *sc = device_get_softc(dev);
202	struct twl_clk_entry *clk;
203	int found = 0;
204	int err;
205	uint8_t grp, state;
206
207	TWL_CLKS_SLOCK(sc);
208
209	LIST_FOREACH(clk, &sc->sc_clks_list, link) {
210		if (strcmp(clk->name, name) == 0) {
211			found = 1;
212			break;
213		}
214	}
215
216	if (!found) {
217		TWL_CLKS_SUNLOCK(sc);
218		return (EINVAL);
219	}
220
221
222	if (twl_is_4030(sc->sc_pdev)) {
223
224		err = twl_clks_read_1(sc, clk, TWL_CLKS_GRP, &grp);
225		if (!err)
226			*enabled = (grp & TWL4030_P1_GRP);
227
228	} else if (twl_is_6030(sc->sc_pdev) || twl_is_6025(sc->sc_pdev)) {
229
230		TWL_CLKS_LOCK_UPGRADE(sc);
231
232		/* Check the clock is in the application group */
233		if (twl_is_6030(sc->sc_pdev)) {
234			err = twl_clks_read_1(sc, clk, TWL_CLKS_GRP, &grp);
235			if (err) {
236				TWL_CLKS_LOCK_DOWNGRADE(sc);
237				goto done;
238			}
239
240			if (!(grp & TWL6030_P1_GRP)) {
241				TWL_CLKS_LOCK_DOWNGRADE(sc);
242				*enabled = 0; /* disabled */
243				goto done;
244			}
245		}
246
247		/* Read the application mode state and verify it's ON */
248		err = twl_clks_read_1(sc, clk, TWL_CLKS_STATE, &state);
249		if (!err)
250			*enabled = ((state & 0x0C) == 0x04);
251
252		TWL_CLKS_LOCK_DOWNGRADE(sc);
253
254	} else {
255		err = EINVAL;
256	}
257
258done:
259	TWL_CLKS_SUNLOCK(sc);
260	return (err);
261}
262
263
264/**
265 *	twl_clks_set_state - enables/disables a clock output
266 *	@sc: device context
267 *	@clk: the clock entry to enable/disable
268 *	@enable: non-zero the clock is enabled, zero the clock is disabled
269 *
270 *	LOCKING:
271 *	The TWL CLK lock must be held before this function is called.
272 *
273 *	RETURNS:
274 *	Zero on success or an error code on failure.
275 */
276static int
277twl_clks_set_state(struct twl_clks_softc *sc, struct twl_clk_entry *clk,
278	int enable)
279{
280	int xlocked;
281	int err;
282	uint8_t grp;
283
284	TWL_CLKS_ASSERT_LOCKED(sc);
285
286	/* Upgrade the lock to exclusive because about to perform read-mod-write */
287	xlocked = sx_xlocked(&sc->sc_sx);
288	if (!xlocked)
289		TWL_CLKS_LOCK_UPGRADE(sc);
290
291	err = twl_clks_read_1(sc, clk, TWL_CLKS_GRP, &grp);
292	if (err)
293		goto done;
294
295	if (twl_is_4030(sc->sc_pdev)) {
296
297		/* On the TWL4030 we just need to ensure the clock is in the right
298		 * power domain, don't need to turn on explicitly like TWL6030.
299		 */
300		if (enable)
301			grp |= TWL4030_P1_GRP;
302		else
303			grp &= ~(TWL4030_P1_GRP | TWL4030_P2_GRP | TWL4030_P3_GRP);
304
305		err = twl_clks_write_1(sc, clk, TWL_CLKS_GRP, grp);
306
307	} else if (twl_is_6030(sc->sc_pdev) || twl_is_6025(sc->sc_pdev)) {
308
309		/* Make sure the clock belongs to at least the APP power group */
310		if (twl_is_6030(sc->sc_pdev) && !(grp & TWL6030_P1_GRP)) {
311			grp |= TWL6030_P1_GRP;
312			err = twl_clks_write_1(sc, clk, TWL_CLKS_GRP, grp);
313			if (err)
314				goto done;
315		}
316
317		/* On TWL6030 we need to make sure we disable power for all groups */
318		if (twl_is_6030(sc->sc_pdev))
319			grp = TWL6030_P1_GRP | TWL6030_P2_GRP | TWL6030_P3_GRP;
320		else
321			grp = 0x00;
322
323		/* Set the state of the clock */
324		if (enable)
325			err = twl_clks_write_1(sc, clk, TWL_CLKS_STATE, (grp << 5) | 0x01);
326		else
327			err = twl_clks_write_1(sc, clk, TWL_CLKS_STATE, (grp << 5));
328
329	} else {
330
331		err = EINVAL;
332	}
333
334done:
335	if (!xlocked)
336		TWL_CLKS_LOCK_DOWNGRADE(sc);
337
338	if ((twl_clks_debug > 1) && !err)
339		device_printf(sc->sc_dev, "%s : %sabled\n", clk->name,
340			enable ? "en" : "dis");
341
342	return (err);
343}
344
345
346/**
347 *	twl_clks_disable - disables a clock output
348 *	@dev: TWL clk device
349*	@name: the name of the clock
350 *
351 *	LOCKING:
352 *	Internally the function takes and releases the TWL lock.
353 *
354 *	RETURNS:
355*	Zero on success or an error code on failure.
356 */
357int
358twl_clks_disable(device_t dev, const char *name)
359{
360	struct twl_clks_softc *sc = device_get_softc(dev);
361	struct twl_clk_entry *clk;
362	int err = EINVAL;
363
364	TWL_CLKS_SLOCK(sc);
365
366	LIST_FOREACH(clk, &sc->sc_clks_list, link) {
367		if (strcmp(clk->name, name) == 0) {
368			err = twl_clks_set_state(sc, clk, 0);
369			break;
370		}
371	}
372
373	TWL_CLKS_SUNLOCK(sc);
374	return (err);
375}
376
377/**
378 *	twl_clks_enable - enables a clock output
379 *	@dev: TWL clk device
380 *	@name: the name of the clock
381 *
382 *	LOCKING:
383 *	Internally the function takes and releases the TWL CLKS lock.
384 *
385 *	RETURNS:
386 *	Zero on success or an error code on failure.
387 */
388int
389twl_clks_enable(device_t dev, const char *name)
390{
391	struct twl_clks_softc *sc = device_get_softc(dev);
392	struct twl_clk_entry *clk;
393	int err = EINVAL;
394
395	TWL_CLKS_SLOCK(sc);
396
397	LIST_FOREACH(clk, &sc->sc_clks_list, link) {
398		if (strcmp(clk->name, name) == 0) {
399			err = twl_clks_set_state(sc, clk, 1);
400			break;
401		}
402	}
403
404	TWL_CLKS_SUNLOCK(sc);
405	return (err);
406}
407
408/**
409 *	twl_clks_sysctl_clock - reads the state of the clock
410 *	@SYSCTL_HANDLER_ARGS: arguments for the callback
411 *
412 *	Returns the clock status; disabled is zero and enabled is non-zero.
413 *
414 *	LOCKING:
415 *	It's expected the TWL lock is held while this function is called.
416 *
417 *	RETURNS:
418 *	EIO if device is not present, otherwise 0 is returned.
419 */
420static int
421twl_clks_sysctl_clock(SYSCTL_HANDLER_ARGS)
422{
423	struct twl_clks_softc *sc = (struct twl_clks_softc*)arg1;
424	int err;
425	int enabled = 0;
426
427	if ((err = twl_clks_is_enabled(sc->sc_dev, oidp->oid_name, &enabled)) != 0)
428		return err;
429
430	return sysctl_handle_int(oidp, &enabled, 0, req);
431}
432
433/**
434 *	twl_clks_add_clock - adds single clock sysctls for the device
435 *	@sc: device soft context
436 *	@name: the name of the regulator
437 *	@nsub: the number of the subdevice
438 *	@regbase: the base address of the clocks registers
439 *
440 *	Adds a single clock to the device and also a sysctl interface for
441 *	querying it's status.
442 *
443 *	LOCKING:
444 *	It's expected the exclusive lock is held while this function is called.
445 *
446 *	RETURNS:
447 *	Pointer to the new clock entry on success, otherwise NULL on failure.
448 */
449static struct twl_clk_entry*
450twl_clks_add_clock(struct twl_clks_softc *sc, const char *name,
451	uint8_t nsub, uint8_t regbase)
452{
453	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
454	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
455	struct twl_clk_entry *new;
456
457	TWL_CLKS_ASSERT_LOCKED(sc);
458
459	new = malloc(sizeof(struct twl_clk_entry), M_DEVBUF, M_NOWAIT | M_ZERO);
460	if (new == NULL)
461		return (NULL);
462
463
464	strncpy(new->name, name, TWL_CLKS_MAX_NAMELEN);
465	new->name[TWL_CLKS_MAX_NAMELEN - 1] = '\0';
466
467	new->sub_dev = nsub;
468	new->reg_off = regbase;
469
470
471
472	/* Add a sysctl entry for the clock */
473	new->oid = SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, name,
474	    CTLTYPE_INT | CTLFLAG_RD, sc, 0,
475	    twl_clks_sysctl_clock, "I", "external clock");
476
477	/* Finally add the regulator to list of supported regulators */
478	LIST_INSERT_HEAD(&sc->sc_clks_list, new, link);
479
480	return (new);
481}
482
483/**
484 *	twl_clks_add_clocks - populates the internal list of clocks
485 *	@sc: device soft context
486 *	@chip: the name of the chip used in the hints
487 *	@clks the list of clocks supported by the device
488 *
489 *	Loops over the list of clocks and adds them to the device context. Also
490 *	scans the FDT to determine if there are any clocks that should be
491 *	enabled/disabled automatically.
492 *
493 *	LOCKING:
494 *	Internally takes the exclusive lock while adding the clocks to the
495 *	device context.
496 *
497 *	RETURNS:
498 *	Always returns 0.
499 */
500static int
501twl_clks_add_clocks(struct twl_clks_softc *sc, const struct twl_clock *clks)
502{
503	int err;
504	const struct twl_clock *walker;
505	struct twl_clk_entry *entry;
506	phandle_t child;
507	char rnames[256];
508	char *name, *state;
509	int len = 0, prop_len;
510	int enable;
511
512
513	TWL_CLKS_XLOCK(sc);
514
515	/* Add the regulators from the list */
516	walker = &clks[0];
517	while (walker->name != NULL) {
518
519		/* Add the regulator to the list */
520		entry = twl_clks_add_clock(sc, walker->name, walker->subdev,
521		    walker->regbase);
522		if (entry == NULL)
523			continue;
524
525		walker++;
526	}
527
528	/* Check for any FDT settings that need to be applied */
529	child = ofw_bus_get_node(sc->sc_pdev);
530	if (child) {
531
532		prop_len = OF_getprop(child, "external-clocks", rnames, sizeof(rnames));
533		while (len < prop_len) {
534			name = rnames + len;
535			len += strlen(name) + 1;
536			if ((len >= prop_len) || (name[0] == '\0'))
537				break;
538
539			state = rnames + len;
540			len += strlen(state) + 1;
541			if (state[0] == '\0')
542				break;
543
544			enable = !strncmp(state, "on", 2);
545
546			LIST_FOREACH(entry, &sc->sc_clks_list, link) {
547				if (strcmp(entry->name, name) == 0) {
548					twl_clks_set_state(sc, entry, enable);
549					break;
550				}
551			}
552		}
553	}
554
555	TWL_CLKS_XUNLOCK(sc);
556
557
558	if (twl_clks_debug) {
559		LIST_FOREACH(entry, &sc->sc_clks_list, link) {
560			err = twl_clks_is_enabled(sc->sc_dev, entry->name, &enable);
561			if (!err)
562				device_printf(sc->sc_dev, "%s : %s\n", entry->name,
563					enable ? "on" : "off");
564		}
565	}
566
567	return (0);
568}
569
570/**
571 *	twl_clks_init - initialises the list of clocks
572 *	@dev: the twl_clks device
573 *
574 *	This function is called as an intrhook once interrupts have been enabled,
575 *	this is done so that the driver has the option to enable/disable a clock
576 *	based on settings providied in the FDT.
577 *
578 *	LOCKING:
579 *	May takes the exclusive lock in the function.
580 */
581static void
582twl_clks_init(void *dev)
583{
584	struct twl_clks_softc *sc;
585
586	sc = device_get_softc((device_t)dev);
587
588	if (twl_is_4030(sc->sc_pdev))
589		twl_clks_add_clocks(sc, twl4030_clocks);
590	else if (twl_is_6030(sc->sc_pdev) || twl_is_6025(sc->sc_pdev))
591		twl_clks_add_clocks(sc, twl6030_clocks);
592
593	config_intrhook_disestablish(&sc->sc_init_hook);
594}
595
596static int
597twl_clks_probe(device_t dev)
598{
599	if (twl_is_4030(device_get_parent(dev)))
600		device_set_desc(dev, "TI TWL4030 PMIC External Clocks");
601	else if (twl_is_6025(device_get_parent(dev)) ||
602	         twl_is_6030(device_get_parent(dev)))
603		device_set_desc(dev, "TI TWL6025/TWL6030 PMIC External Clocks");
604	else
605		return (ENXIO);
606
607	return (0);
608}
609
610static int
611twl_clks_attach(device_t dev)
612{
613	struct twl_clks_softc *sc;
614
615	sc = device_get_softc(dev);
616	sc->sc_dev = dev;
617	sc->sc_pdev = device_get_parent(dev);
618
619	TWL_CLKS_LOCK_INIT(sc);
620
621	LIST_INIT(&sc->sc_clks_list);
622
623
624	sc->sc_init_hook.ich_func = twl_clks_init;
625	sc->sc_init_hook.ich_arg = dev;
626
627	if (config_intrhook_establish(&sc->sc_init_hook) != 0)
628		return (ENOMEM);
629
630	return (0);
631}
632
633static int
634twl_clks_detach(device_t dev)
635{
636	struct twl_clks_softc *sc;
637	struct twl_clk_entry *clk;
638	struct twl_clk_entry *tmp;
639
640	sc = device_get_softc(dev);
641
642	TWL_CLKS_XLOCK(sc);
643
644	LIST_FOREACH_SAFE(clk, &sc->sc_clks_list, link, tmp) {
645		LIST_REMOVE(clk, link);
646		sysctl_remove_oid(clk->oid, 1, 0);
647		free(clk, M_DEVBUF);
648	}
649
650	TWL_CLKS_XUNLOCK(sc);
651
652	TWL_CLKS_LOCK_DESTROY(sc);
653
654	return (0);
655}
656
657static device_method_t twl_clks_methods[] = {
658	DEVMETHOD(device_probe,		twl_clks_probe),
659	DEVMETHOD(device_attach,	twl_clks_attach),
660	DEVMETHOD(device_detach,	twl_clks_detach),
661
662	{0, 0},
663};
664
665static driver_t twl_clks_driver = {
666	"twl_clks",
667	twl_clks_methods,
668	sizeof(struct twl_clks_softc),
669};
670
671static devclass_t twl_clks_devclass;
672
673DRIVER_MODULE(twl_clks, twl, twl_clks_driver, twl_clks_devclass, 0, 0);
674MODULE_VERSION(twl_clks, 1);
675