1248557Sray/*-
2248557Sray * Copyright (C) 2008-2009 Semihalf, Michal Hajduk
3250357Sray * Copyright (c) 2012, 2013 The FreeBSD Foundation
4289666Sian * Copyright (c) 2015 Ian Lepore <ian@FreeBSD.org>
5248557Sray * All rights reserved.
6248557Sray *
7248557Sray * Portions of this software were developed by Oleksandr Rybalko
8248557Sray * under sponsorship from the FreeBSD Foundation.
9248557Sray *
10248557Sray * Redistribution and use in source and binary forms, with or without
11248557Sray * modification, are permitted provided that the following conditions
12248557Sray * are met:
13248557Sray * 1. Redistributions of source code must retain the above copyright
14248557Sray *    notice, this list of conditions and the following disclaimer.
15248557Sray * 2. Redistributions in binary form must reproduce the above copyright
16248557Sray *    notice, this list of conditions and the following disclaimer in the
17248557Sray *    documentation and/or other materials provided with the distribution.
18248557Sray *
19248557Sray * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20248557Sray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21248557Sray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22248557Sray * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23248557Sray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24248557Sray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25248557Sray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26248557Sray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27248557Sray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28248557Sray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29248557Sray * SUCH DAMAGE.
30248557Sray */
31248557Sray
32289666Sian/*
33289666Sian * I2C driver for Freescale i.MX hardware.
34289666Sian *
35289666Sian * Note that the hardware is capable of running as both a master and a slave.
36289666Sian * This driver currently implements only master-mode operations.
37289666Sian *
38289666Sian * This driver supports multi-master i2c busses, by detecting bus arbitration
39289666Sian * loss and returning IIC_EBUSBSY status.  Notably, it does not do any kind of
40289666Sian * retries if some other master jumps onto the bus and interrupts one of our
41289666Sian * transfer cycles resulting in arbitration loss in mid-transfer.  The caller
42289666Sian * must handle retries in a way that makes sense for the slave being addressed.
43289666Sian */
44289666Sian
45248557Sray#include <sys/cdefs.h>
46248557Sray__FBSDID("$FreeBSD: releng/10.3/sys/arm/freescale/imx/imx_i2c.c 289666 2015-10-20 21:20:34Z ian $");
47248557Sray
48248557Sray#include <sys/param.h>
49248557Sray#include <sys/systm.h>
50248557Sray#include <sys/bus.h>
51248557Sray#include <sys/kernel.h>
52276278Sian#include <sys/limits.h>
53248557Sray#include <sys/module.h>
54248557Sray#include <sys/resource.h>
55248557Sray
56248557Sray#include <machine/bus.h>
57248557Sray#include <machine/resource.h>
58248557Sray#include <sys/rman.h>
59248557Sray
60276278Sian#include <arm/freescale/imx/imx_ccmvar.h>
61276278Sian
62248557Sray#include <dev/iicbus/iiconf.h>
63248557Sray#include <dev/iicbus/iicbus.h>
64248557Sray#include "iicbus_if.h"
65248557Sray
66248557Sray#include <dev/fdt/fdt_common.h>
67248557Sray#include <dev/ofw/openfirm.h>
68248557Sray#include <dev/ofw/ofw_bus.h>
69248557Sray#include <dev/ofw/ofw_bus_subr.h>
70248557Sray
71248557Sray#define I2C_ADDR_REG		0x00 /* I2C slave address register */
72248557Sray#define I2C_FDR_REG		0x04 /* I2C frequency divider register */
73248557Sray#define I2C_CONTROL_REG		0x08 /* I2C control register */
74248557Sray#define I2C_STATUS_REG		0x0C /* I2C status register */
75248557Sray#define I2C_DATA_REG		0x10 /* I2C data register */
76248557Sray#define I2C_DFSRR_REG		0x14 /* I2C Digital Filter Sampling rate */
77248557Sray
78248557Sray#define I2CCR_MEN		(1 << 7) /* Module enable */
79248557Sray#define I2CCR_MSTA		(1 << 5) /* Master/slave mode */
80248557Sray#define I2CCR_MTX		(1 << 4) /* Transmit/receive mode */
81248557Sray#define I2CCR_TXAK		(1 << 3) /* Transfer acknowledge */
82248557Sray#define I2CCR_RSTA		(1 << 2) /* Repeated START */
83248557Sray
84248557Sray#define I2CSR_MCF		(1 << 7) /* Data transfer */
85248557Sray#define I2CSR_MASS		(1 << 6) /* Addressed as a slave */
86248557Sray#define I2CSR_MBB		(1 << 5) /* Bus busy */
87248557Sray#define I2CSR_MAL		(1 << 4) /* Arbitration lost */
88248557Sray#define I2CSR_SRW		(1 << 2) /* Slave read/write */
89248557Sray#define I2CSR_MIF		(1 << 1) /* Module interrupt */
90248557Sray#define I2CSR_RXAK		(1 << 0) /* Received acknowledge */
91248557Sray
92248557Sray#define I2C_BAUD_RATE_FAST	0x31
93248557Sray#define I2C_BAUD_RATE_DEF	0x3F
94248557Sray#define I2C_DFSSR_DIV		0x10
95248557Sray
96276278Sian/*
97276278Sian * A table of available divisors and the associated coded values to put in the
98276278Sian * FDR register to achieve that divisor.. There is no algorithmic relationship I
99276278Sian * can see between divisors and the codes that go into the register.  The table
100276278Sian * begins and ends with entries that handle insane configuration values.
101276278Sian */
102276278Sianstruct clkdiv {
103276278Sian	u_int divisor;
104276278Sian	u_int regcode;
105276278Sian};
106276278Sianstatic struct clkdiv clkdiv_table[] = {
107276278Sian        {    0, 0x20 }, {   22, 0x20 }, {   24, 0x21 }, {   26, 0x22 },
108276278Sian        {   28, 0x23 }, {   30, 0x00 }, {   32, 0x24 }, {   36, 0x25 },
109276278Sian        {   40, 0x26 }, {   42, 0x03 }, {   44, 0x27 }, {   48, 0x28 },
110276278Sian        {   52, 0x05 }, {   56, 0x29 }, {   60, 0x06 }, {   64, 0x2a },
111276278Sian        {   72, 0x2b }, {   80, 0x2c }, {   88, 0x09 }, {   96, 0x2d },
112276278Sian        {  104, 0x0a }, {  112, 0x2e }, {  128, 0x2f }, {  144, 0x0c },
113276278Sian        {  160, 0x30 }, {  192, 0x31 }, {  224, 0x32 }, {  240, 0x0f },
114276278Sian        {  256, 0x33 }, {  288, 0x10 }, {  320, 0x34 }, {  384, 0x35 },
115276278Sian        {  448, 0x36 }, {  480, 0x13 }, {  512, 0x37 }, {  576, 0x14 },
116276278Sian        {  640, 0x38 }, {  768, 0x39 }, {  896, 0x3a }, {  960, 0x17 },
117276278Sian        { 1024, 0x3b }, { 1152, 0x18 }, { 1280, 0x3c }, { 1536, 0x3d },
118276278Sian        { 1792, 0x3e }, { 1920, 0x1b }, { 2048, 0x3f }, { 2304, 0x1c },
119276278Sian        { 2560, 0x1d }, { 3072, 0x1e }, { 3840, 0x1f }, {UINT_MAX, 0x1f}
120276278Sian};
121276278Sian
122273662Sianstatic struct ofw_compat_data compat_data[] = {
123273662Sian	{"fsl,imx6q-i2c",  1},
124273662Sian	{"fsl,imx-i2c",	   1},
125273662Sian	{NULL,             0}
126273662Sian};
127273662Sian
128248557Sraystruct i2c_softc {
129248557Sray	device_t		dev;
130248557Sray	device_t		iicbus;
131248557Sray	struct resource		*res;
132248557Sray	int			rid;
133289666Sian	sbintime_t		byte_time_sbt;
134248557Sray};
135248557Sray
136248557Sraystatic phandle_t i2c_get_node(device_t, device_t);
137248557Sraystatic int i2c_probe(device_t);
138248557Sraystatic int i2c_attach(device_t);
139248557Sray
140248557Sraystatic int i2c_repeated_start(device_t, u_char, int);
141248557Sraystatic int i2c_start(device_t, u_char, int);
142248557Sraystatic int i2c_stop(device_t);
143248557Sraystatic int i2c_reset(device_t, u_char, u_char, u_char *);
144248557Sraystatic int i2c_read(device_t, char *, int, int *, int, int);
145248557Sraystatic int i2c_write(device_t, const char *, int, int *, int);
146248557Sray
147248557Sraystatic device_method_t i2c_methods[] = {
148248557Sray	DEVMETHOD(device_probe,			i2c_probe),
149248557Sray	DEVMETHOD(device_attach,		i2c_attach),
150248557Sray
151248557Sray	/* OFW methods */
152248557Sray	DEVMETHOD(ofw_bus_get_node,		i2c_get_node),
153248557Sray
154248557Sray	DEVMETHOD(iicbus_callback,		iicbus_null_callback),
155248557Sray	DEVMETHOD(iicbus_repeated_start,	i2c_repeated_start),
156248557Sray	DEVMETHOD(iicbus_start,			i2c_start),
157248557Sray	DEVMETHOD(iicbus_stop,			i2c_stop),
158248557Sray	DEVMETHOD(iicbus_reset,			i2c_reset),
159248557Sray	DEVMETHOD(iicbus_read,			i2c_read),
160248557Sray	DEVMETHOD(iicbus_write,			i2c_write),
161248557Sray	DEVMETHOD(iicbus_transfer,		iicbus_transfer_gen),
162248557Sray
163289666Sian	DEVMETHOD_END
164248557Sray};
165248557Sray
166248557Sraystatic driver_t i2c_driver = {
167248557Sray	"iichb",
168248557Sray	i2c_methods,
169248557Sray	sizeof(struct i2c_softc),
170248557Sray};
171248557Sraystatic devclass_t  i2c_devclass;
172248557Sray
173248557SrayDRIVER_MODULE(i2c, simplebus, i2c_driver, i2c_devclass, 0, 0);
174248557SrayDRIVER_MODULE(iicbus, i2c, iicbus_driver, iicbus_devclass, 0, 0);
175248557Sray
176248557Sraystatic phandle_t
177248557Srayi2c_get_node(device_t bus, device_t dev)
178248557Sray{
179248557Sray	/*
180248557Sray	 * Share controller node with iicbus device
181248557Sray	 */
182248557Sray	return ofw_bus_get_node(bus);
183248557Sray}
184248557Sray
185248557Sraystatic __inline void
186248557Srayi2c_write_reg(struct i2c_softc *sc, bus_size_t off, uint8_t val)
187248557Sray{
188248557Sray
189289666Sian	bus_write_1(sc->res, off, val);
190248557Sray}
191248557Sray
192248557Sraystatic __inline uint8_t
193248557Srayi2c_read_reg(struct i2c_softc *sc, bus_size_t off)
194248557Sray{
195248557Sray
196289666Sian	return (bus_read_1(sc->res, off));
197248557Sray}
198248557Sray
199248557Sraystatic __inline void
200248557Srayi2c_flag_set(struct i2c_softc *sc, bus_size_t off, uint8_t mask)
201248557Sray{
202248557Sray	uint8_t status;
203248557Sray
204248557Sray	status = i2c_read_reg(sc, off);
205248557Sray	status |= mask;
206248557Sray	i2c_write_reg(sc, off, status);
207248557Sray}
208248557Sray
209289666Sian/* Wait for bus to become busy or not-busy. */
210248557Sraystatic int
211289666Sianwait_for_busbusy(struct i2c_softc *sc, int wantbusy)
212248557Sray{
213289666Sian	int retry, srb;
214248557Sray
215248557Sray	retry = 1000;
216248557Sray	while (retry --) {
217289666Sian		srb = i2c_read_reg(sc, I2C_STATUS_REG) & I2CSR_MBB;
218289666Sian		if ((srb && wantbusy) || (!srb && !wantbusy))
219248557Sray			return (IIC_NOERR);
220289666Sian		DELAY(1);
221248557Sray	}
222248557Sray	return (IIC_ETIMEOUT);
223248557Sray}
224248557Sray
225289666Sian/* Wait for transfer to complete, optionally check RXAK. */
226248557Sraystatic int
227289666Sianwait_for_xfer(struct i2c_softc *sc, int checkack)
228248557Sray{
229289666Sian	int retry, sr;
230248557Sray
231289666Sian	/*
232289666Sian	 * Sleep for about the time it takes to transfer a byte (with precision
233289666Sian	 * set to tolerate 5% oversleep).  We calculate the approximate byte
234289666Sian	 * transfer time when we set the bus speed divisor.  Slaves are allowed
235289666Sian	 * to do clock-stretching so the actual transfer time can be larger, but
236289666Sian	 * this gets the bulk of the waiting out of the way without tying up the
237289666Sian	 * processor the whole time.
238289666Sian	 */
239289666Sian	pause_sbt("imxi2c", sc->byte_time_sbt, sc->byte_time_sbt / 20, 0);
240289666Sian
241289666Sian	retry = 10000;
242248557Sray	while (retry --) {
243289666Sian		sr = i2c_read_reg(sc, I2C_STATUS_REG);
244289666Sian		if (sr & I2CSR_MIF) {
245289666Sian                        if (sr & I2CSR_MAL)
246289666Sian				return (IIC_EBUSERR);
247289666Sian			else if (checkack && (sr & I2CSR_RXAK))
248289666Sian				return (IIC_ENOACK);
249289666Sian			else
250289666Sian				return (IIC_NOERR);
251289666Sian		}
252289666Sian		DELAY(1);
253248557Sray	}
254248557Sray	return (IIC_ETIMEOUT);
255248557Sray}
256248557Sray
257289666Sian/*
258289666Sian * Implement the error handling shown in the state diagram of the imx6 reference
259289666Sian * manual.  If there was an error, then:
260289666Sian *  - Clear master mode (MSTA and MTX).
261289666Sian *  - Wait for the bus to become free or for a timeout to happen.
262289666Sian *  - Disable the controller.
263289666Sian */
264248557Sraystatic int
265289666Siani2c_error_handler(struct i2c_softc *sc, int error)
266248557Sray{
267248557Sray
268289666Sian	if (error != 0) {
269289666Sian		i2c_write_reg(sc, I2C_STATUS_REG, 0);
270289666Sian		i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN);
271289666Sian		wait_for_busbusy(sc, false);
272289666Sian		i2c_write_reg(sc, I2C_CONTROL_REG, 0);
273248557Sray	}
274289666Sian	return (error);
275248557Sray}
276248557Sray
277248557Sraystatic int
278248557Srayi2c_probe(device_t dev)
279248557Sray{
280248557Sray
281261410Sian	if (!ofw_bus_status_okay(dev))
282261410Sian		return (ENXIO);
283261410Sian
284273662Sian	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
285248557Sray		return (ENXIO);
286248557Sray
287289666Sian	device_set_desc(dev, "Freescale i.MX I2C");
288248557Sray
289248557Sray	return (BUS_PROBE_DEFAULT);
290248557Sray}
291248557Sray
292248557Sraystatic int
293248557Srayi2c_attach(device_t dev)
294248557Sray{
295248557Sray	struct i2c_softc *sc;
296248557Sray
297248557Sray	sc = device_get_softc(dev);
298248557Sray	sc->dev = dev;
299248557Sray	sc->rid = 0;
300248557Sray
301248557Sray	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
302248557Sray	    RF_ACTIVE);
303248557Sray	if (sc->res == NULL) {
304248557Sray		device_printf(dev, "could not allocate resources");
305248557Sray		return (ENXIO);
306248557Sray	}
307248557Sray
308248557Sray	sc->iicbus = device_add_child(dev, "iicbus", -1);
309248557Sray	if (sc->iicbus == NULL) {
310248557Sray		device_printf(dev, "could not add iicbus child");
311248557Sray		return (ENXIO);
312248557Sray	}
313248557Sray
314248557Sray	bus_generic_attach(dev);
315289666Sian	return (0);
316248557Sray}
317248557Sray
318248557Sraystatic int
319248557Srayi2c_repeated_start(device_t dev, u_char slave, int timeout)
320248557Sray{
321248557Sray	struct i2c_softc *sc;
322248557Sray	int error;
323248557Sray
324248557Sray	sc = device_get_softc(dev);
325248557Sray
326248557Sray	if ((i2c_read_reg(sc, I2C_STATUS_REG) & I2CSR_MBB) == 0) {
327289666Sian		return (IIC_EBUSERR);
328248557Sray	}
329248557Sray
330289666Sian	/*
331289666Sian	 * Set repeated start condition, delay (per reference manual, min 156nS)
332289666Sian	 * before writing slave address, wait for ack after write.
333289666Sian	 */
334248557Sray	i2c_flag_set(sc, I2C_CONTROL_REG, I2CCR_RSTA);
335289666Sian	DELAY(1);
336248557Sray	i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
337248557Sray	i2c_write_reg(sc, I2C_DATA_REG, slave);
338289666Sian	error = wait_for_xfer(sc, true);
339289666Sian	return (i2c_error_handler(sc, error));
340248557Sray}
341248557Sray
342248557Sraystatic int
343248557Srayi2c_start(device_t dev, u_char slave, int timeout)
344248557Sray{
345248557Sray	struct i2c_softc *sc;
346248557Sray	int error;
347248557Sray
348248557Sray	sc = device_get_softc(dev);
349248557Sray
350289666Sian	i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN);
351289666Sian	DELAY(10); /* Delay for controller to sample bus state. */
352248557Sray	if (i2c_read_reg(sc, I2C_STATUS_REG) & I2CSR_MBB) {
353289666Sian		return (i2c_error_handler(sc, IIC_EBUSERR));
354248557Sray	}
355289666Sian	i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN | I2CCR_MSTA | I2CCR_MTX);
356289666Sian	if ((error = wait_for_busbusy(sc, true)) != IIC_NOERR)
357289666Sian		return (i2c_error_handler(sc, error));
358289666Sian	i2c_write_reg(sc, I2C_STATUS_REG, 0);
359248557Sray	i2c_write_reg(sc, I2C_DATA_REG, slave);
360289666Sian	error = wait_for_xfer(sc, true);
361289666Sian	return (i2c_error_handler(sc, error));
362248557Sray}
363248557Sray
364248557Sraystatic int
365248557Srayi2c_stop(device_t dev)
366248557Sray{
367248557Sray	struct i2c_softc *sc;
368248557Sray
369248557Sray	sc = device_get_softc(dev);
370248557Sray
371289666Sian	i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN);
372289666Sian	wait_for_busbusy(sc, false);
373289666Sian	i2c_write_reg(sc, I2C_CONTROL_REG, 0);
374248557Sray	return (IIC_NOERR);
375248557Sray}
376248557Sray
377248557Sraystatic int
378248557Srayi2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldadr)
379248557Sray{
380248557Sray	struct i2c_softc *sc;
381276278Sian	u_int busfreq, div, i, ipgfreq;
382248557Sray
383248557Sray	sc = device_get_softc(dev);
384248557Sray
385276278Sian	/*
386276278Sian	 * Look up the divisor that gives the nearest speed that doesn't exceed
387276278Sian	 * the configured value for the bus.
388276278Sian	 */
389276278Sian	ipgfreq = imx_ccm_ipg_hz();
390276278Sian	busfreq = IICBUS_GET_FREQUENCY(sc->iicbus, speed);
391276278Sian	div = (ipgfreq + busfreq - 1) / busfreq;
392276278Sian	for (i = 0; i < nitems(clkdiv_table); i++) {
393276278Sian		if (clkdiv_table[i].divisor >= div)
394276278Sian			break;
395248557Sray	}
396248557Sray
397289666Sian	/*
398289666Sian	 * Calculate roughly how long it will take to transfer a byte (which
399289666Sian	 * requires 9 clock cycles) at the new bus speed.  This value is used to
400289666Sian	 * pause() while waiting for transfer-complete.  With a 66MHz IPG clock
401289666Sian	 * and the actual i2c bus speeds that leads to, for nominal 100KHz and
402289666Sian	 * 400KHz bus speeds the transfer times are roughly 104uS and 22uS.
403289666Sian	 */
404289666Sian	busfreq = ipgfreq / clkdiv_table[i].divisor;
405289666Sian	sc->byte_time_sbt = SBT_1US * (9000000 / busfreq);
406248557Sray
407289666Sian	/*
408289666Sian	 * Disable the controller (do the reset), and set the new clock divisor.
409289666Sian	 */
410248557Sray	i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
411289666Sian	i2c_write_reg(sc, I2C_CONTROL_REG, 0x0);
412289666Sian	i2c_write_reg(sc, I2C_FDR_REG, (uint8_t)clkdiv_table[i].regcode);
413248557Sray	return (IIC_NOERR);
414248557Sray}
415248557Sray
416248557Sraystatic int
417248557Srayi2c_read(device_t dev, char *buf, int len, int *read, int last, int delay)
418248557Sray{
419248557Sray	struct i2c_softc *sc;
420248557Sray	int error, reg;
421248557Sray
422248557Sray	sc = device_get_softc(dev);
423248557Sray	*read = 0;
424248557Sray
425248557Sray	if (len) {
426248557Sray		if (len == 1)
427248557Sray			i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
428248557Sray			    I2CCR_MSTA | I2CCR_TXAK);
429248557Sray		else
430248557Sray			i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
431248557Sray			    I2CCR_MSTA);
432289666Sian                /* Dummy read to prime the receiver. */
433289666Sian		i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
434248557Sray		i2c_read_reg(sc, I2C_DATA_REG);
435248557Sray	}
436248557Sray
437289666Sian	error = 0;
438289666Sian	*read = 0;
439248557Sray	while (*read < len) {
440289666Sian		if ((error = wait_for_xfer(sc, false)) != IIC_NOERR)
441289666Sian			break;
442248557Sray		i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
443289666Sian		if (last) {
444289666Sian			if (*read == len - 2) {
445289666Sian				/* NO ACK on last byte */
446289666Sian				i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
447289666Sian				    I2CCR_MSTA | I2CCR_TXAK);
448289666Sian			} else if (*read == len - 1) {
449289666Sian				/* Transfer done, signal stop. */
450289666Sian				i2c_write_reg(sc, I2C_CONTROL_REG, I2CCR_MEN |
451289666Sian				    I2CCR_TXAK);
452289666Sian				wait_for_busbusy(sc, false);
453289666Sian			}
454248557Sray		}
455248557Sray		reg = i2c_read_reg(sc, I2C_DATA_REG);
456248557Sray		*buf++ = reg;
457248557Sray		(*read)++;
458248557Sray	}
459248557Sray
460289666Sian	return (i2c_error_handler(sc, error));
461248557Sray}
462248557Sray
463248557Sraystatic int
464248557Srayi2c_write(device_t dev, const char *buf, int len, int *sent, int timeout)
465248557Sray{
466248557Sray	struct i2c_softc *sc;
467248557Sray	int error;
468248557Sray
469248557Sray	sc = device_get_softc(dev);
470289666Sian
471289666Sian	error = 0;
472248557Sray	*sent = 0;
473248557Sray	while (*sent < len) {
474248557Sray		i2c_write_reg(sc, I2C_STATUS_REG, 0x0);
475248557Sray		i2c_write_reg(sc, I2C_DATA_REG, *buf++);
476289666Sian		if ((error = wait_for_xfer(sc, true)) != IIC_NOERR)
477289666Sian			break;
478248557Sray		(*sent)++;
479248557Sray	}
480248557Sray
481289666Sian	return (i2c_error_handler(sc, error));
482248557Sray}
483