xref: /freebsd/sys/dev/dialog/da9063/da9063_iic.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2022 Jessica Clarke <jrtc27@FreeBSD.org>
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 THE 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 THE 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 /* Dialog Semiconductor DA9063 PMIC, 2-WIRE */
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/bus.h>
33 #include <sys/lock.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
36 
37 #include <dev/dialog/da9063/da9063reg.h>
38 #include <dev/fdt/simplebus.h>
39 #include <dev/iicbus/iiconf.h>
40 #include <dev/ofw/ofw_bus_subr.h>
41 
42 #include "da9063_if.h"
43 
44 #define	DA9063_IIC_PAGE_SHIFT	8
45 #define	DA9063_IIC_PAGE_SIZE	(1 << DA9063_IIC_PAGE_SHIFT)
46 #define	DA9063_IIC_PAGE(_a)	((_a) >> DA9063_IIC_PAGE_SHIFT)
47 #define	DA9063_IIC_PAGE_OFF(_a)	((_a) & (DA9063_IIC_PAGE_SIZE - 1))
48 #define	DA9063_IIC_ADDR(_p, _o)	(((_p) << DA9063_IIC_PAGE_SHIFT) | (_o))
49 
50 /*
51  * For 2-WIRE (I2C) operation pages are 256 registers but PAGE_CON is in units
52  * of 128 registers with the LSB ignored so scale the page when writing to it.
53  */
54 #define	DA9063_IIC_PAGE_CON_REG_PAGE_SHIFT	1
55 
56 struct da9063_iic_softc {
57 	struct simplebus_softc	simplebus_sc;
58 	device_t		dev;
59 	struct mtx		mtx;
60 	uint8_t			page;
61 };
62 
63 #define	DA9063_IIC_LOCK(sc)		mtx_lock(&(sc)->mtx)
64 #define	DA9063_IIC_UNLOCK(sc)		mtx_unlock(&(sc)->mtx)
65 #define	DA9063_IIC_ASSERT_LOCKED(sc)	mtx_assert(&(sc)->mtx, MA_OWNED);
66 #define	DA9063_IIC_ASSERT_UNLOCKED(sc)	mtx_assert(&(sc)->mtx, MA_NOTOWNED);
67 
68 static struct ofw_compat_data compat_data[] = {
69 	{ "dlg,da9063",	1 },
70 	{ NULL,		0 }
71 };
72 
73 static int
74 da9063_iic_select_page(struct da9063_iic_softc *sc, uint16_t page)
75 {
76 	uint8_t reg;
77 	int error;
78 
79 	DA9063_IIC_ASSERT_LOCKED(sc);
80 
81 	if (page == sc->page)
82 		return (0);
83 
84 	error = iicdev_readfrom(sc->dev, DA9063_PAGE_CON, &reg, 1, IIC_WAIT);
85 	if (error != 0)
86 		return (iic2errno(error));
87 
88 	reg &= ~(DA9063_PAGE_CON_REG_PAGE_MASK <<
89 	    DA9063_PAGE_CON_REG_PAGE_SHIFT);
90 	reg |= (page << DA9063_IIC_PAGE_CON_REG_PAGE_SHIFT) <<
91 	    DA9063_PAGE_CON_REG_PAGE_SHIFT;
92 
93 	error = iicdev_writeto(sc->dev, DA9063_PAGE_CON, &reg, 1, IIC_WAIT);
94 	if (error != 0)
95 		return (iic2errno(error));
96 
97 	sc->page = page;
98 
99 	return (0);
100 }
101 
102 static int
103 da9063_iic_read(device_t dev, uint16_t addr, uint8_t *val)
104 {
105 	struct da9063_iic_softc *sc;
106 	int error;
107 
108 	sc = device_get_softc(dev);
109 
110 	DA9063_IIC_LOCK(sc);
111 
112 	error = da9063_iic_select_page(sc, DA9063_IIC_PAGE(addr));
113 	if (error != 0)
114 		goto error;
115 
116 	error = iicdev_readfrom(dev, DA9063_IIC_PAGE_OFF(addr), val, 1,
117 	    IIC_WAIT);
118 	if (error != 0)
119 		error = iic2errno(error);
120 
121 error:
122 	DA9063_IIC_UNLOCK(sc);
123 
124 	return (error);
125 }
126 
127 static int
128 da9063_iic_write(device_t dev, uint16_t addr, uint8_t val)
129 {
130 	struct da9063_iic_softc *sc;
131 	int error;
132 
133 	sc = device_get_softc(dev);
134 
135 	DA9063_IIC_LOCK(sc);
136 
137 	error = da9063_iic_select_page(sc, DA9063_IIC_PAGE(addr));
138 	if (error != 0)
139 		goto error;
140 
141 	error = iicdev_writeto(dev, DA9063_IIC_PAGE_OFF(addr), &val, 1,
142 	    IIC_WAIT);
143 	if (error != 0)
144 		error = iic2errno(error);
145 
146 error:
147 	DA9063_IIC_UNLOCK(sc);
148 
149 	return (error);
150 }
151 
152 static int
153 da9063_iic_modify(device_t dev, uint16_t addr, uint8_t clear_mask,
154     uint8_t set_mask)
155 {
156 	struct da9063_iic_softc *sc;
157 	uint8_t reg;
158 	int error;
159 
160 	sc = device_get_softc(dev);
161 
162 	DA9063_IIC_LOCK(sc);
163 
164 	error = da9063_iic_select_page(sc, DA9063_IIC_PAGE(addr));
165 	if (error != 0)
166 		goto error;
167 
168 	error = iicdev_readfrom(dev, DA9063_IIC_PAGE_OFF(addr), &reg, 1,
169 	    IIC_WAIT);
170 	if (error != 0) {
171 		error = iic2errno(error);
172 		goto error;
173 	}
174 
175 	reg &= ~clear_mask;
176 	reg |= set_mask;
177 
178 	error = iicdev_writeto(dev, DA9063_IIC_PAGE_OFF(addr), &reg, 1,
179 	    IIC_WAIT);
180 	if (error != 0)
181 		error = iic2errno(error);
182 
183 error:
184 	DA9063_IIC_UNLOCK(sc);
185 
186 	return (error);
187 }
188 
189 static int
190 da9063_iic_probe(device_t dev)
191 {
192 	if (!ofw_bus_status_okay(dev))
193 		return (ENXIO);
194 
195 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
196 		return (ENXIO);
197 
198 	device_set_desc(dev, "Dialog DA9063 PMIC");
199 
200 	return (BUS_PROBE_DEFAULT);
201 }
202 
203 static int
204 da9063_iic_attach(device_t dev)
205 {
206 	struct da9063_iic_softc *sc;
207 	uint8_t reg;
208 	int error;
209 
210 	sc = device_get_softc(dev);
211 
212 	sc->dev = dev;
213 
214 	error = iicdev_readfrom(dev, DA9063_PAGE_CON, &reg, 1, IIC_WAIT);
215 	if (error != 0)
216 		return (iic2errno(error));
217 
218 	sc->page = ((reg >> DA9063_PAGE_CON_REG_PAGE_SHIFT) &
219 	    DA9063_PAGE_CON_REG_PAGE_MASK) >> DA9063_IIC_PAGE_CON_REG_PAGE_SHIFT;
220 	mtx_init(&sc->mtx, device_get_nameunit(sc->dev), NULL, MTX_DEF);
221 
222 	sc->simplebus_sc.flags |= SB_FLAG_NO_RANGES;
223 
224 	return (simplebus_attach(dev));
225 }
226 
227 static int
228 da9063_iic_detach(device_t dev)
229 {
230 	struct da9063_iic_softc *sc;
231 	int error;
232 
233 	sc = device_get_softc(dev);
234 
235 	error = simplebus_detach(dev);
236 	if (error != 0)
237 		return (error);
238 
239 	mtx_destroy(&sc->mtx);
240 
241 	return (0);
242 }
243 
244 static device_method_t da9063_iic_methods[] = {
245 	/* Device interface */
246 	DEVMETHOD(device_probe,		da9063_iic_probe),
247 	DEVMETHOD(device_attach,	da9063_iic_attach),
248 	DEVMETHOD(device_detach,	da9063_iic_detach),
249 
250 	/* DA9063 interface */
251 	DEVMETHOD(da9063_read,		da9063_iic_read),
252 	DEVMETHOD(da9063_write,		da9063_iic_write),
253 	DEVMETHOD(da9063_modify,	da9063_iic_modify),
254 
255 	DEVMETHOD_END
256 };
257 
258 DEFINE_CLASS_1(da9063_pmic, da9063_iic_driver, da9063_iic_methods,
259     sizeof(struct da9063_iic_softc), simplebus_driver);
260 
261 DRIVER_MODULE(da9063_iic, iicbus, da9063_iic_driver, NULL, NULL);
262