1296906Smmel/*-
2296906Smmel * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
3296906Smmel * All rights reserved.
4296906Smmel *
5296906Smmel * Redistribution and use in source and binary forms, with or without
6296906Smmel * modification, are permitted provided that the following conditions
7296906Smmel * are met:
8296906Smmel * 1. Redistributions of source code must retain the above copyright
9296906Smmel *    notice, this list of conditions and the following disclaimer.
10296906Smmel * 2. Redistributions in binary form must reproduce the above copyright
11296906Smmel *    notice, this list of conditions and the following disclaimer in the
12296906Smmel *    documentation and/or other materials provided with the distribution.
13296906Smmel *
14296906Smmel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15296906Smmel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16296906Smmel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17296906Smmel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18296906Smmel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19296906Smmel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20296906Smmel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21296906Smmel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22296906Smmel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23296906Smmel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24296906Smmel * SUCH DAMAGE.
25296906Smmel *
26296906Smmel * $FreeBSD: stable/11/sys/dev/extres/regulator/regulator.h 308328 2016-11-05 04:40:58Z mmel $
27296906Smmel */
28296906Smmel
29296906Smmel#ifndef _DEV_EXTRES_REGULATOR_H_
30296906Smmel#define _DEV_EXTRES_REGULATOR_H_
31296906Smmel#include "opt_platform.h"
32296906Smmel
33296906Smmel#include <sys/kobj.h>
34296906Smmel#ifdef FDT
35296906Smmel#include <dev/ofw/ofw_bus.h>
36296906Smmel#endif
37296906Smmel#include "regnode_if.h"
38296906Smmel
39296906Smmel#define REGULATOR_FLAGS_STATIC		0x00000001  /* Static strings */
40296906Smmel#define REGULATOR_FLAGS_NOT_DISABLE	0x00000002  /* Cannot be disabled */
41296906Smmel
42296906Smmel#define REGULATOR_STATUS_ENABLED	0x00000001
43296906Smmel#define REGULATOR_STATUS_OVERCURRENT	0x00000002
44296906Smmel
45296906Smmeltypedef struct regulator *regulator_t;
46296906Smmel
47296906Smmel/* Standard regulator parameters. */
48296906Smmelstruct regnode_std_param {
49296906Smmel	int 			min_uvolt;	/* In uV */
50296906Smmel	int 			max_uvolt;	/* In uV */
51296906Smmel	int 			min_uamp;	/* In uA */
52296906Smmel	int 			max_uamp;	/* In uA */
53296906Smmel	int 			ramp_delay;	/* In uV/usec */
54296906Smmel	int 			enable_delay;	/* In usec */
55296906Smmel	bool 			boot_on;	/* Is enabled on boot */
56296906Smmel	bool 			always_on;	/* Must be enabled */
57296906Smmel	int			enable_active_high;
58296906Smmel};
59296906Smmel
60296906Smmel/* Initialization parameters. */
61296906Smmelstruct regnode_init_def {
62296906Smmel	char			*name;		/* Regulator name */
63296906Smmel	char			*parent_name;	/* Name of parent regulator */
64296906Smmel	struct regnode_std_param std_param;	/* Standard parameters */
65296906Smmel	intptr_t		id;		/* Regulator ID */
66296906Smmel	int			flags;		/* Flags */
67296906Smmel#ifdef FDT
68296906Smmel	 phandle_t 		ofw_node;	/* OFW node of regulator */
69296906Smmel#endif
70308328Smmel};
71296906Smmel
72308328Smmelstruct regulator_range {
73308328Smmel	int		min_uvolt;
74308328Smmel	int		step_uvolt;
75308328Smmel	uint8_t		min_sel;
76308328Smmel	uint8_t		max_sel;
77296906Smmel};
78296906Smmel
79308328Smmel#define	REG_RANGE_INIT(_min_sel, _max_sel, _min_uvolt, _step_uvolt) {	\
80308328Smmel	.min_sel	= _min_sel,					\
81308328Smmel	.max_sel	= _max_sel,					\
82308328Smmel	.min_uvolt	= _min_uvolt,					\
83308328Smmel	.step_uvolt	= _step_uvolt,					\
84308328Smmel}
85308328Smmel
86296906Smmel/*
87296906Smmel * Shorthands for constructing method tables.
88296906Smmel */
89296906Smmel#define	REGNODEMETHOD		KOBJMETHOD
90296906Smmel#define	REGNODEMETHOD_END	KOBJMETHOD_END
91296906Smmel#define regnode_method_t	kobj_method_t
92296906Smmel#define regnode_class_t		kobj_class_t
93296906SmmelDECLARE_CLASS(regnode_class);
94296906Smmel
95296906Smmel/* Providers interface. */
96296906Smmelstruct regnode *regnode_create(device_t pdev, regnode_class_t regnode_class,
97296906Smmel    struct regnode_init_def *def);
98296906Smmelstruct regnode *regnode_register(struct regnode *regnode);
99296906Smmelconst char *regnode_get_name(struct regnode *regnode);
100296906Smmelconst char *regnode_get_parent_name(struct regnode *regnode);
101296906Smmelstruct regnode *regnode_get_parent(struct regnode *regnode);
102296906Smmelint regnode_get_flags(struct regnode *regnode);
103296906Smmelvoid *regnode_get_softc(struct regnode *regnode);
104296906Smmeldevice_t regnode_get_device(struct regnode *regnode);
105296906Smmelstruct regnode_std_param *regnode_get_stdparam(struct regnode *regnode);
106296906Smmelvoid regnode_topo_unlock(void);
107296906Smmelvoid regnode_topo_xlock(void);
108296906Smmelvoid regnode_topo_slock(void);
109296906Smmel
110296906Smmelint regnode_enable(struct regnode *regnode);
111296906Smmelint regnode_disable(struct regnode *regnode);
112296906Smmelint regnode_stop(struct regnode *regnode, int depth);
113296906Smmelint regnode_status(struct regnode *regnode, int *status);
114296906Smmelint regnode_get_voltage(struct regnode *regnode, int *uvolt);
115296906Smmelint regnode_set_voltage(struct regnode *regnode, int min_uvolt, int max_uvolt);
116296906Smmel#ifdef FDT
117296906Smmelphandle_t regnode_get_ofw_node(struct regnode *regnode);
118296906Smmel#endif
119296906Smmel
120296906Smmel/* Consumers interface. */
121296906Smmelint regulator_get_by_name(device_t cdev, const char *name,
122296906Smmel     regulator_t *regulator);
123296906Smmelint regulator_get_by_id(device_t cdev, device_t pdev, intptr_t id,
124296906Smmel    regulator_t *regulator);
125296906Smmelint regulator_release(regulator_t regulator);
126296906Smmelconst char *regulator_get_name(regulator_t regulator);
127296906Smmelint regulator_enable(regulator_t reg);
128296906Smmelint regulator_disable(regulator_t reg);
129296906Smmelint regulator_stop(regulator_t reg);
130296906Smmelint regulator_status(regulator_t reg, int *status);
131296906Smmelint regulator_get_voltage(regulator_t reg, int *uvolt);
132296906Smmelint regulator_set_voltage(regulator_t reg, int min_uvolt, int max_uvolt);
133296906Smmel
134296906Smmel#ifdef FDT
135308324Smmelint regulator_get_by_ofw_property(device_t dev, phandle_t node, char *name,
136308324Smmel    regulator_t *reg);
137296906Smmelint regulator_parse_ofw_stdparam(device_t dev, phandle_t node,
138296906Smmel    struct regnode_init_def *def);
139296906Smmel#endif
140296906Smmel
141308328Smmel/* Utility functions */
142308328Smmelint regulator_range_volt_to_sel8(struct regulator_range *ranges, int nranges,
143308328Smmel    int min_uvolt, int max_uvolt, uint8_t *out_sel);
144308328Smmelint regulator_range_sel8_to_volt(struct regulator_range *ranges, int nranges,
145308328Smmel   uint8_t sel, int *volt);
146308328Smmel
147296906Smmel#endif /* _DEV_EXTRES_REGULATOR_H_ */
148