1 /* $NetBSD: clk.c,v 1.2 2017/04/16 12:28:21 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: clk.c,v 1.2 2017/04/16 12:28:21 jmcneill Exp $"); 31 32 #include <sys/param.h> 33 34 #include <dev/clk/clk.h> 35 #include <dev/clk/clk_backend.h> 36 37 struct clk * 38 clk_get(struct clk_domain *domain, const char *name) 39 { 40 return domain->funcs->get(domain->priv, name); 41 } 42 43 void 44 clk_put(struct clk *clk) 45 { 46 return clk->domain->funcs->put(clk->domain->priv, clk); 47 } 48 49 u_int 50 clk_get_rate(struct clk *clk) 51 { 52 return clk->domain->funcs->get_rate(clk->domain->priv, clk); 53 } 54 55 int 56 clk_set_rate(struct clk *clk, u_int rate) 57 { 58 if (clk->flags & CLK_SET_RATE_PARENT) { 59 return clk_set_rate(clk_get_parent(clk), rate); 60 } else if (clk->domain->funcs->set_rate) { 61 return clk->domain->funcs->set_rate(clk->domain->priv, 62 clk, rate); 63 } else { 64 return EINVAL; 65 } 66 } 67 68 int 69 clk_enable(struct clk *clk) 70 { 71 if (clk->domain->funcs->enable) 72 return clk->domain->funcs->enable(clk->domain->priv, clk); 73 else 74 return 0; 75 } 76 77 int 78 clk_disable(struct clk *clk) 79 { 80 if (clk->domain->funcs->disable) 81 return clk->domain->funcs->disable(clk->domain->priv, clk); 82 else 83 return EINVAL; 84 } 85 86 int 87 clk_set_parent(struct clk *clk, struct clk *parent_clk) 88 { 89 if (clk->domain->funcs->set_parent) 90 return clk->domain->funcs->set_parent(clk->domain->priv, 91 clk, parent_clk); 92 else 93 return EINVAL; 94 } 95 96 struct clk * 97 clk_get_parent(struct clk *clk) 98 { 99 return clk->domain->funcs->get_parent(clk->domain->priv, clk); 100 } 101