Skip to contentSkip to conversation

spi: add platform_data pointer to spi_board_info

RFC PATCH Applied Hubert Feurstein 3 participants 7 messages Jun 20, 2011 – Jun 24, 2011 1 message not in the archive SPI

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/3 spi: add platform_data pointer to spi_board_info

In the spi layer this pointer will be assigned to the platform_data pointer of the new spi device, thus it can be accessed in the drivers 'probe' handler.

---
 drivers/spi/spi.c |    1 +
 include/spi/spi.h |    4 ++--
 2 files changed, 3 insertions(+), 2 deletions(-)
drivers/spi/spi.c +1 -0
index 4560259..588c263 100644
@@ -75,6 +75,7 @@ struct spi_device *spi_new_device(struct spi_master *master,
 	proxy->chip_select = chip->chip_select;
 	proxy->max_speed_hz = chip->max_speed_hz;
 	proxy->mode = chip->mode;
+	proxy->dev.platform_data = chip->platform_data;
 	strcpy(proxy->dev.name, chip->name);
 	proxy->dev.type_data = proxy;
 	status = register_device(&proxy->dev);
include/spi/spi.h +2 -2
index 8dce8db..c76f3b7 100644
@@ -14,8 +14,8 @@ struct spi_board_info {
 	/* mode becomes spi_device.mode, and is essential for chips
 	 * where the default of SPI_CS_HIGH = 0 is wrong.
 	 */
-	u8		mode;
-
+	u8	mode;
+	void	*platform_data;
 };
 
 /**
Signature · 7 lines
1.7.4.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

RFC PATCH 2/3 spi: add more spi transfer functions

This commit adds the following spi transfer functions:
- spi_write
- spi_read
- spi_write_then_read
- spi_w8r8

The code has been ported from the linux kernel.

---
 drivers/spi/spi.c |   41 +++++++++++++++++++++++++++++
 include/spi/spi.h |   74 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 115 insertions(+), 0 deletions(-)
drivers/spi/spi.c +41 -0
index 588c263..6456897 100644
@@ -195,3 +195,44 @@ int spi_sync(struct spi_device *spi, struct spi_message *message)
 	return spi->master->transfer(spi, message);
 }
 
+/**
+ * spi_write_then_read - SPI synchronous write followed by read
+ * @spi: device with which data will be exchanged
+ * @txbuf: data to be written
+ * @n_tx: size of txbuf, in bytes
+ * @rxbuf: buffer into which data will be read
+ * @n_rx: size of rxbuf, in bytes
+ * Context: can sleep
+ *
+ * This performs a half duplex MicroWire style transaction with the
+ * device, sending txbuf and then reading rxbuf.  The return value
+ * is zero for success, else a negative errno status code.
+ * This call may only be used from a context that may sleep.
+ */
+int spi_write_then_read(struct spi_device *spi,
+		const void *txbuf, unsigned n_tx,
+		void *rxbuf, unsigned n_rx)
+{
+	int			status;
+	struct spi_message	message;
+	struct spi_transfer	x[2];
+
+	spi_message_init(&message);
+	memset(x, 0, sizeof x);
+	if (n_tx) {
+		x[0].len = n_tx;
+		spi_message_add_tail(&x[0], &message);
+	}
+	if (n_rx) {
+		x[1].len = n_rx;
+		spi_message_add_tail(&x[1], &message);
+	}
+
+	x[0].tx_buf = txbuf;
+	x[1].rx_buf = rxbuf;
+
+	/* do the i/o */
+	status = spi_sync(spi, &message);
+	return status;
+}
+EXPORT_SYMBOL(spi_write_then_read);
include/spi/spi.h +74 -0

80 lines not on this page · raw message

Signature · 7 lines
1.7.4.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

Re: [RFC PATCH 2/3] spi: add more spi transfer functions

Hello, I have a question regarding the SPI spi_write_then_read function.

2011/6/20 Hubert Feurstein <[email protected]>

Quoted 31 lines

This commit adds the following spi transfer functions:
- spi_write
- spi_read
- spi_write_then_read
- spi_w8r8

The code has been ported from the linux kernel.

---
 drivers/spi/spi.c |   41 +++++++++++++++++++++++++++++
 include/spi/spi.h |   74
+++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 115 insertions(+), 0 deletions(-)
drivers/spi/spi.c +6 -0
index 588c263..6456897 100644
@@ -195,3 +195,44 @@ int spi_sync(struct spi_device *spi, struct
 spi_message *message)
       return spi->master->transfer(spi, message);
 }
 
+/**
+ * spi_write_then_read - SPI synchronous write followed by read
+ * @spi: device with which data will be exchanged
+ * @txbuf: data to be written
+ * @n_tx: size of txbuf, in bytes
+ * @rxbuf: buffer into which data will be read

drivers/spi/spi.c int spi_sync(struct spi_device *spi, struct · line 213

Quoted 10 lines
drivers/spi/spi.c +10 -0
+ * @n_rx: size of rxbuf, in bytes
+ * Context: can sleep
+ *
+ * This performs a half duplex MicroWire style transaction with the
+ * device, sending txbuf and then reading rxbuf.  The return value
+ * is zero for success, else a negative errno status code.
+ * This call may only be used from a context that may sleep.
+ */
+int spi_write_then_read(struct spi_device *spi,
+               const void *txbuf, unsigned n_tx,

Could you explain me why const is used here ?

drivers/spi/spi.c int spi_sync(struct spi_device *spi, struct spi_message *message) · line 221

Quoted 9 lines
drivers/spi/spi.c +8 -0
+               void *rxbuf, unsigned n_rx)
+{
+       int                     status;
+       struct spi_message      message;
+       struct spi_transfer     x[2];
+
+       spi_message_init(&message);
+       memset(x, 0, sizeof x);

Here we set everything to zero, including rx/tx buffer pointers.

drivers/spi/spi.c int spi_sync(struct spi_device *spi, struct spi_message *message) · line 232

Quoted 12 lines
drivers/spi/spi.c +11 -0
+       if (n_tx) {
+               x[0].len = n_tx;
+               spi_message_add_tail(&x[0], &message);
+       }
+       if (n_rx) {
+               x[1].len = n_rx;
+               spi_message_add_tail(&x[1], &message);
+       }
+
+       x[0].tx_buf = txbuf;
+       x[1].rx_buf = rxbuf;

We still have x[0].rx_buf = 0 and x[1].tx_buf = 0

drivers/spi/spi.c int spi_sync(struct spi_device *spi, struct spi_message *message) · line 235

Quoted 4 lines
drivers/spi/spi.c +3 -0
+
+       /* do the i/o */
+       status = spi_sync(spi, &message);

Then we call spi_sync then master->transfer. In this last function we have:

list_for_each_entry (t, &mesg->transfers, transfer_list) {

const u32 *txbuf = t->tx_buf; <--- point to zero when x[1] right ?????
u32 *rxbuf = t->rx_buf; <--- point to zero when x[0] right ?????

int i = 0;

while(i < t->len >> 2) {
rxbuf[i] = imx->xchg_single(imx, txbuf[i]);
i++;
}
}

What am I missing ???

Quoted 93 lines
drivers/spi/spi.c +3 -0
+       return status;
+}
+EXPORT_SYMBOL(spi_write_then_read);
include/spi/spi.h +74 -0
index c76f3b7..ac2013a 100644
@@ -349,6 +349,80 @@ static inline int spi_register_board_info(struct
 spi_board_info const *info,
 }
 #endif
 
+/**
+ * spi_write - SPI synchronous write
+ * @spi: device to which data will be written
+ * @buf: data buffer
+ * @len: data buffer size
+ * Context: can sleep
+ *
+ * This writes the buffer and returns zero or a negative error code.
+ * Callable only from contexts that can sleep.
+ */
+static inline int
+spi_write(struct spi_device *spi, const void *buf, size_t len)
+{
+       struct spi_transfer     t = {
+                       .tx_buf         = buf,
+                       .len            = len,
+               };
+       struct spi_message      m;
+
+       spi_message_init(&m);
+       spi_message_add_tail(&t, &m);
+       return spi_sync(spi, &m);
+}
+
+/**
+ * spi_read - SPI synchronous read
+ * @spi: device from which data will be read
+ * @buf: data buffer
+ * @len: data buffer size
+ * Context: can sleep
+ *
+ * This reads the buffer and returns zero or a negative error code.
+ * Callable only from contexts that can sleep.
+ */
+static inline int
+spi_read(struct spi_device *spi, void *buf, size_t len)
+{
+       struct spi_transfer     t = {
+                       .rx_buf         = buf,
+                       .len            = len,
+               };
+       struct spi_message      m;
+
+       spi_message_init(&m);
+       spi_message_add_tail(&t, &m);
+       return spi_sync(spi, &m);
+}
+
+/* this copies txbuf and rxbuf data; for small transfers only! */
+extern int spi_write_then_read(struct spi_device *spi,
+               const void *txbuf, unsigned n_tx,
+               void *rxbuf, unsigned n_rx);
+
+/**
+ * spi_w8r8 - SPI synchronous 8 bit write followed by 8 bit read
+ * @spi: device with which data will be exchanged
+ * @cmd: command to be written before data is read back
+ * Context: can sleep
+ *
+ * This returns the (unsigned) eight bit number returned by the
+ * device, or else a negative error code.  Callable only from
+ * contexts that can sleep.
+ */
+static inline ssize_t spi_w8r8(struct spi_device *spi, u8 cmd)
+{
+       ssize_t                 status;
+       u8                      result;
+
+       status = spi_write_then_read(spi, &cmd, 1, &result, 1);
+
+       /* return negative errno or unsigned value */
+       return (status < 0) ? status : result;
+}
+
 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
 

#endif /* __INCLUDE_SPI_H */
--
1.7.4.1

Quoted 4 lines

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

Franck.

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#3 lore raw

Re: [RFC PATCH 2/3] spi: add more spi transfer functions

Hi Franck,

2011/6/23 Franck JULLIEN <[email protected]>:

Quoted 2 lines

Hello, I have a question regarding the SPI spi_write_then_read function.

[snip]

Quoted 4 lines
Quoted 2 lines

+int spi_write_then_read(struct spi_device *spi,
+               const void *txbuf, unsigned n_tx,

Could you explain me why const is used here ?

Because this function will *not* write to the data of txbuf. With the const here, this is
ensured at least at syntax level.

[snip]

Quoted 12 lines

Then we call spi_sync then master->transfer. In this last function we have:
list_for_each_entry (t, &mesg->transfers, transfer_list) { const u32 *txbuf = t->tx_buf;  <--- point to zero when x[1] right ????? u32 *rxbuf = t->rx_buf;        <--- point to zero when x[0] right ????? int i = 0;
while(i < t->len >> 2) {
rxbuf[i] = imx->xchg_single(imx, txbuf[i]);
i++;
}
}
What am I missing ???

Your transfer function has to handle this correctly:
while(i < t->len >> 2) {
u32 txval = txbuf ? txbuf[i] : 0
u32 rxval = imx->xchg_single(imx, txval); if (rxbuf)

    rxbuf[i] = rxval;

i++;
}
If no txbuf is available then simply transfer 0, if no rxbuf is available then throw away the result.
BTW, I'm not sure if the above transfer-handler is able to handle 8bit transfers correctly (because of len >> 2).

Hope this helps.

Best Regards
Hubert

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw

RFC PATCH 2/3 spi: add more spi transfer functions

Hi Hubert,

2011/6/24 Hubert Feurstein <[email protected]>

Quoted 5 lines

Hi Franck,

2011/6/23 Franck JULLIEN <[email protected]>:

Quoted 2 lines

Hello, I have a question regarding the SPI spi_write_then_read function.

Quoted 9 lines

[snip]

Quoted 4 lines

drivers/spi/spi.c int spi_sync(struct spi_device *spi, struct spi_message *message) · line 213

Quoted 2 lines
drivers/spi/spi.c +2 -0
+int spi_write_then_read(struct spi_device *spi,
+               const void *txbuf, unsigned n_tx,

Could you explain me why const is used here ?

Because this function will *not* write to the data of txbuf. With the const here, this is
ensured at least at syntax level.

OK.

Quoted 15 lines

[snip]

Quoted 2 lines

Then we call spi_sync then master->transfer. In this last function we

have:

Quoted 10 lines

list_for_each_entry (t, &mesg->transfers, transfer_list) { const u32 *txbuf = t->tx_buf; <--- point to zero when x[1] right ?????
u32 *rxbuf = t->rx_buf; <--- point to zero when x[0] right ?????
int i = 0;
while(i < t->len >> 2) {
rxbuf[i] = imx->xchg_single(imx, txbuf[i]);
i++;
}
}
What am I missing ???

Quoted 9 lines

Your transfer function has to handle this correctly:
while(i < t->len >> 2) {
u32 txval = txbuf ? txbuf[i] : 0
u32 rxval = imx->xchg_single(imx, txval);
if (rxbuf)
rxbuf[i] = rxval;
i++;
}

That's what I was thinking about. The code above is from imx_spi.c. I am currently
writing an Altera spi driver that's why I was looking at this...

Quoted 6 lines

If no txbuf is available then simply transfer 0, if no rxbuf is available then throw away the result.
BTW, I'm not sure if the above transfer-handler is able to handle 8bit transfers correctly (because of len >> 2).

I agree.

Quoted 5 lines

Hope this helps.

Best Regards
Hubert

Thanks,
Franck.

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#6 lore raw

RFC PATCH 3/3 eeprom: add at25 eeprom driver

In reply to Hubert Feurstein

This commit adds support for most spi eeproms, such as the Atmel at25 models.

---
Changes since previous version:
- moved driver to drivers/eeprom (eliminated the misc folder)
- removed mdelay in at25_write and retries in 'wait for ready' loop in at25_write. The loop is only guarded by the timeout now.

 drivers/Kconfig         |    1 +
 drivers/Makefile        |    1 +
 drivers/eeprom/Kconfig  |   11 ++
 drivers/eeprom/Makefile |    1 +
 drivers/eeprom/at25.c   |  319 +++++++++++++++++++++++++++++++++++++++++++++++
 include/spi/eeprom.h    |   22 ++++
 6 files changed, 355 insertions(+), 0 deletions(-)
 create mode 100644 drivers/eeprom/Kconfig
 create mode 100644 drivers/eeprom/Makefile
 create mode 100644 drivers/eeprom/at25.c
 create mode 100644 include/spi/eeprom.h
drivers/Kconfig +1 -0
index 86d8fb5..c4e1517 100644
@@ -13,5 +13,6 @@ source "drivers/mci/Kconfig"
 source "drivers/clk/Kconfig"
 source "drivers/mfd/Kconfig"
 source "drivers/led/Kconfig"
+source "drivers/eeprom/Kconfig"
 
 endmenu
drivers/Makefile +1 -0
index b1b402f..92b22bd 100644
@@ -11,3 +11,4 @@ obj-$(CONFIG_VIDEO) += video/
 obj-y	+= clk/
 obj-y	+= mfd/
 obj-$(CONFIG_LED) += led/
+obj-y	+= eeprom/
drivers/eeprom/Kconfig new file +11 -0
new file mode 100644
index 0000000..a0b5489
@@ -0,0 +1,11 @@
+menu "EEPROM support"
+
+config EEPROM_AT25
+	tristate "SPI EEPROMs from most vendors"
+	depends on SPI
+	help
+	  Enable this driver to get read/write support to most SPI EEPROMs,
+	  after you configure the board init code to know about each eeprom
+	  on your target board.
+
+endmenu
drivers/eeprom/Makefile new file +1 -0
new file mode 100644
index 0000000..e323bd0
@@ -0,0 +1 @@
+obj-$(CONFIG_EEPROM_AT25)	+= at25.o
drivers/eeprom/at25.c new file +319 -0

319 lines not on this page · raw message

include/spi/eeprom.h new file +22 -0
new file mode 100644
index 0000000..15495e5
@@ -0,0 +1,22 @@
+#ifndef _SPI_EEPROM_H
+#define _SPI_EEPROM_H
+
+/*
+ * Put one of these structures in platform_data for SPI EEPROMS handled
+ * by the "at25" driver.  On SPI, most EEPROMS understand the same core
+ * command set.  If you need to support EEPROMs that don't yet fit, add
+ * flags to support those protocol options.  These values all come from
+ * the chip datasheets.
+ */
+struct spi_eeprom {
+	char		name[10];
+	u16		page_size;		/* for writes */
+	u16		flags;
+#define	EE_ADDR1	0x0001			/*  8 bit addrs */
+#define	EE_ADDR2	0x0002			/* 16 bit addrs */
+#define	EE_ADDR3	0x0004			/* 24 bit addrs */
+#define	EE_READONLY	0x0008			/* disallow writes */
+	u32		size;
+};
+
+#endif /* _SPI_EEPROM_H */
Signature · 7 lines
1.7.4.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Applied
#7 lore raw
In reply to Hubert Feurstein

Applied this series to -next.

Sascha

Quoted 46 lines · Hubert Feurstein

In the spi layer this pointer will be assigned to the platform_data pointer of the new spi device, thus it can be accessed in the drivers 'probe' handler.

---
 drivers/spi/spi.c |    1 +
 include/spi/spi.h |    4 ++--
 2 files changed, 3 insertions(+), 2 deletions(-)
drivers/spi/spi.c +1 -0
index 4560259..588c263 100644
@@ -75,6 +75,7 @@ struct spi_device *spi_new_device(struct spi_master *master,
 	proxy->chip_select = chip->chip_select;
 	proxy->max_speed_hz = chip->max_speed_hz;
 	proxy->mode = chip->mode;
+	proxy->dev.platform_data = chip->platform_data;
 	strcpy(proxy->dev.name, chip->name);
 	proxy->dev.type_data = proxy;
 	status = register_device(&proxy->dev);
include/spi/spi.h +2 -2
index 8dce8db..c76f3b7 100644
@@ -14,8 +14,8 @@ struct spi_board_info {
 	/* mode becomes spi_device.mode, and is essential for chips
 	 * where the default of SPI_CS_HIGH = 0 is wrong.
 	 */
-	u8		mode;
-
+	u8	mode;
+	void	*platform_data;
 };
 
 /**
Signature · 7 lines
1.7.4.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox