File Index
Files and directories
Sign in
FILE

i2c-algo-pcf.c

File preview with syntax highlighting.

linux-6.19.14/drivers/i2c/algos/i2c-algo-pcf.c C
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * i2c-algo-pcf.c i2c driver algorithms for PCF8584 adapters
  4 *
  5 *   Copyright (C) 1995-1997 Simon G. Vogl
  6 *		   1998-2000 Hans Berglund
  7 *
  8 * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and
  9 * Frodo Looijaard <frodol@dds.nl>, and also from Martin Bailey
 10 * <mbailey@littlefeet-inc.com>
 11 *
 12 * Partially rewriten by Oleg I. Vdovikin <vdovikin@jscc.ru> to handle multiple
 13 * messages, proper stop/repstart signaling during receive, added detect code
 14 */
 15
 16#include <linux/kernel.h>
 17#include <linux/module.h>
 18#include <linux/delay.h>
 19#include <linux/errno.h>
 20#include <linux/i2c.h>
 21#include <linux/i2c-algo-pcf.h>
 22#include <linux/string_choices.h>
 23#include "i2c-algo-pcf.h"
 24
 25
 26#define DEF_TIMEOUT 16
 27
 28/* setting states on the bus with the right timing: */
 29
 30#define set_pcf(adap, ctl, val) adap->setpcf(adap->data, ctl, val)
 31#define get_pcf(adap, ctl) adap->getpcf(adap->data, ctl)
 32#define get_own(adap) adap->getown(adap->data)
 33#define get_clock(adap) adap->getclock(adap->data)
 34#define i2c_outb(adap, val) adap->setpcf(adap->data, 0, val)
 35#define i2c_inb(adap) adap->getpcf(adap->data, 0)
 36
 37/* other auxiliary functions */
 38
 39static void i2c_start(struct i2c_algo_pcf_data *adap)
 40{
 41	set_pcf(adap, 1, I2C_PCF_START);
 42}
 43
 44static void i2c_repstart(struct i2c_algo_pcf_data *adap)
 45{
 46	set_pcf(adap, 1, I2C_PCF_REPSTART);
 47}
 48
 49static void i2c_stop(struct i2c_algo_pcf_data *adap)
 50{
 51	set_pcf(adap, 1, I2C_PCF_STOP);
 52}
 53
 54static void handle_lab(struct i2c_algo_pcf_data *adap, const int *status)
 55{
 56	/*
 57	 * Cleanup from LAB -- reset and enable ESO.
 58	 * This resets the PCF8584; since we've lost the bus, no
 59	 * further attempts should be made by callers to clean up
 60	 * (no i2c_stop() etc.)
 61	 */
 62	set_pcf(adap, 1, I2C_PCF_PIN);
 63	set_pcf(adap, 1, I2C_PCF_ESO);
 64	/*
 65	 * We pause for a time period sufficient for any running
 66	 * I2C transaction to complete -- the arbitration logic won't
 67	 * work properly until the next START is seen.
 68	 * It is assumed the bus driver or client has set a proper value.
 69	 *
 70	 * REVISIT: should probably use msleep instead of mdelay if we
 71	 * know we can sleep.
 72	 */
 73	if (adap->lab_mdelay)
 74		mdelay(adap->lab_mdelay);
 75
 76}
 77
 78static int wait_for_bb(struct i2c_algo_pcf_data *adap)
 79{
 80
 81	int timeout = DEF_TIMEOUT;
 82	int status;
 83
 84	status = get_pcf(adap, 1);
 85
 86	while (!(status & I2C_PCF_BB) && --timeout) {
 87		udelay(100); /* wait for 100 us */
 88		status = get_pcf(adap, 1);
 89	}
 90
 91	if (timeout == 0) {
 92		printk(KERN_ERR "Timeout waiting for Bus Busy\n");
 93		return -ETIMEDOUT;
 94	}
 95
 96	return 0;
 97}
 98
 99static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status)
100{
101
102	int timeout = DEF_TIMEOUT;
103
104	*status = get_pcf(adap, 1);
105
106	while ((*status & I2C_PCF_PIN) && --timeout) {
107		adap->waitforpin(adap->data);
108		*status = get_pcf(adap, 1);
109	}
110	if (*status & I2C_PCF_LAB) {
111		handle_lab(adap, status);
112		return -EINTR;
113	}
114
115	if (timeout == 0)
116		return -ETIMEDOUT;
117
118	return 0;
119}
120
121/*
122 * This should perform the 'PCF8584 initialization sequence' as described
123 * in the Philips IC12 data book (1995, Aug 29).
124 * There should be a 30 clock cycle wait after reset, I assume this
125 * has been fulfilled.
126 * There should be a delay at the end equal to the longest I2C message
127 * to synchronize the BB-bit (in multimaster systems). How long is
128 * this? I assume 1 second is always long enough.
129 *
130 * vdovikin: added detect code for PCF8584
131 */
132static int pcf_init_8584(struct i2c_algo_pcf_data *adap)
133{
134	unsigned char temp;
135
136	/* S1=0x80: S0 selected, serial interface off */
137	set_pcf(adap, 1, I2C_PCF_PIN);
138	/*
139	 * check to see S1 now used as R/W ctrl -
140	 * PCF8584 does that when ESO is zero
141	 */
142	temp = get_pcf(adap, 1);
143	if ((temp & 0x7f) != 0)
144		return -ENXIO; /* definitely not PCF8584 */
145
146	/* load own address in S0, effective address is (own << 1) */
147	i2c_outb(adap, get_own(adap));
148	/* check it's really written */
149	temp = i2c_inb(adap);
150	if (temp != get_own(adap))
151		return -ENXIO;
152
153	/* S1=0xA0, next byte in S2 */
154	set_pcf(adap, 1, I2C_PCF_PIN | I2C_PCF_ES1);
155	/* check to see S2 now selected */
156	temp = get_pcf(adap, 1);
157	if ((temp & 0x7f) != I2C_PCF_ES1)
158		return -ENXIO;
159
160	/* load clock register S2 */
161	i2c_outb(adap, get_clock(adap));
162	/* check it's really written, the only 5 lowest bits does matter */
163	temp = i2c_inb(adap);
164	if ((temp & 0x1f) != get_clock(adap))
165		return -ENXIO;
166
167	/* Enable serial interface, idle, S0 selected */
168	set_pcf(adap, 1, I2C_PCF_IDLE);
169
170	/* check to see PCF is really idled and we can access status register */
171	temp = get_pcf(adap, 1);
172	if (temp != (I2C_PCF_PIN | I2C_PCF_BB))
173		return -ENXIO;
174
175	printk(KERN_DEBUG "i2c-algo-pcf.o: detected and initialized PCF8584.\n");
176
177	return 0;
178}
179
180static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
181			 int count, int last)
182{
183	struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
184	int wrcount, status, timeout;
185
186	for (wrcount = 0; wrcount < count; ++wrcount) {
187		i2c_outb(adap, buf[wrcount]);
188		timeout = wait_for_pin(adap, &status);
189		if (timeout) {
190			if (timeout == -EINTR)
191				return -EINTR; /* arbitration lost */
192
193			i2c_stop(adap);
194			dev_err(&i2c_adap->dev, "i2c_write: error - timeout.\n");
195			return -EREMOTEIO; /* got a better one ?? */
196		}
197		if (status & I2C_PCF_LRB) {
198			i2c_stop(adap);
199			dev_err(&i2c_adap->dev, "i2c_write: error - no ack.\n");
200			return -EREMOTEIO; /* got a better one ?? */
201		}
202	}
203	if (last)
204		i2c_stop(adap);
205	else
206		i2c_repstart(adap);
207
208	return wrcount;
209}
210
211static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
212			 int count, int last)
213{
214	int i, status;
215	struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
216	int wfp;
217
218	/* increment number of bytes to read by one -- read dummy byte */
219	for (i = 0; i <= count; i++) {
220
221		wfp = wait_for_pin(adap, &status);
222		if (wfp) {
223			if (wfp == -EINTR)
224				return -EINTR; /* arbitration lost */
225
226			i2c_stop(adap);
227			dev_err(&i2c_adap->dev, "pcf_readbytes timed out.\n");
228			return -1;
229		}
230
231		if ((status & I2C_PCF_LRB) && (i != count)) {
232			i2c_stop(adap);
233			dev_err(&i2c_adap->dev, "i2c_read: i2c_inb, No ack.\n");
234			return -1;
235		}
236
237		if (i == count - 1) {
238			set_pcf(adap, 1, I2C_PCF_ESO);
239		} else if (i == count) {
240			if (last)
241				i2c_stop(adap);
242			else
243				i2c_repstart(adap);
244		}
245
246		if (i)
247			buf[i - 1] = i2c_inb(adap);
248		else
249			i2c_inb(adap); /* dummy read */
250	}
251
252	return i - 1;
253}
254
255
256static void pcf_send_address(struct i2c_algo_pcf_data *adap,
257			 struct i2c_msg *msg)
258{
259	unsigned char addr = i2c_8bit_addr_from_msg(msg);
260
261	if (msg->flags & I2C_M_REV_DIR_ADDR)
262		addr ^= 1;
263	i2c_outb(adap, addr);
264}
265
266static int pcf_xfer(struct i2c_adapter *i2c_adap,
267		    struct i2c_msg *msgs,
268		    int num)
269{
270	struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
271	struct i2c_msg *pmsg;
272	int i;
273	int timeout, status;
274
275	if (adap->xfer_begin)
276		adap->xfer_begin(adap->data);
277
278	/* Check for bus busy */
279	timeout = wait_for_bb(adap);
280	if (timeout) {
281		i = -EIO;
282		goto out;
283	}
284
285	for (i = 0; i < num; i++) {
286		int ret;
287
288		pmsg = &msgs[i];
289		pcf_send_address(adap, pmsg);
290
291		/* Send START */
292		if (i == 0)
293			i2c_start(adap);
294
295		/* Wait for PIN (pending interrupt NOT) */
296		timeout = wait_for_pin(adap, &status);
297		if (timeout) {
298			if (timeout == -EINTR) {
299				/* arbitration lost */
300				i = -EINTR;
301				goto out;
302			}
303			i2c_stop(adap);
304			i = -EREMOTEIO;
305			goto out;
306		}
307
308		/* Check LRB (last rcvd bit - slave ack) */
309		if (status & I2C_PCF_LRB) {
310			i2c_stop(adap);
311			i = -EREMOTEIO;
312			goto out;
313		}
314
315
316		if (pmsg->flags & I2C_M_RD) {
317			ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
318					    (i + 1 == num));
319		} else {
320			ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
321					    (i + 1 == num));
322		}
323
324		if (ret < 0)
325			goto out;
326	}
327
328out:
329	if (adap->xfer_end)
330		adap->xfer_end(adap->data);
331	return i;
332}
333
334static u32 pcf_func(struct i2c_adapter *adap)
335{
336	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
337	       I2C_FUNC_PROTOCOL_MANGLING;
338}
339
340/* exported algorithm data: */
341static const struct i2c_algorithm pcf_algo = {
342	.xfer = pcf_xfer,
343	.functionality = pcf_func,
344};
345
346/*
347 * registering functions to load algorithms at runtime
348 */
349int i2c_pcf_add_bus(struct i2c_adapter *adap)
350{
351	struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
352	int rval;
353
354	/* register new adapter to i2c module... */
355	adap->algo = &pcf_algo;
356
357	rval = pcf_init_8584(pcf_adap);
358	if (rval)
359		return rval;
360
361	rval = i2c_add_adapter(adap);
362
363	return rval;
364}
365EXPORT_SYMBOL(i2c_pcf_add_bus);
366
367MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
368MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
369MODULE_LICENSE("GPL");
~/ Read-only file preview.