fbdev: fbmem: Fix the implicit type casting
[linux-2.6-block.git] / drivers / spi / spi-mem.c
index 1513553e40805a91d02f9c68952bd0b68c869aa0..37f4443ce9a0971d8e5fce49cff72c15faa8e65f 100644 (file)
@@ -6,6 +6,7 @@
  * Author: Boris Brezillon <boris.brezillon@bootlin.com>
  */
 #include <linux/dmaengine.h>
+#include <linux/iopoll.h>
 #include <linux/pm_runtime.h>
 #include <linux/spi/spi.h>
 #include <linux/spi/spi-mem.h>
@@ -743,6 +744,91 @@ static inline struct spi_mem_driver *to_spi_mem_drv(struct device_driver *drv)
        return container_of(drv, struct spi_mem_driver, spidrv.driver);
 }
 
+static int spi_mem_read_status(struct spi_mem *mem,
+                              const struct spi_mem_op *op,
+                              u16 *status)
+{
+       const u8 *bytes = (u8 *)op->data.buf.in;
+       int ret;
+
+       ret = spi_mem_exec_op(mem, op);
+       if (ret)
+               return ret;
+
+       if (op->data.nbytes > 1)
+               *status = ((u16)bytes[0] << 8) | bytes[1];
+       else
+               *status = bytes[0];
+
+       return 0;
+}
+
+/**
+ * spi_mem_poll_status() - Poll memory device status
+ * @mem: SPI memory device
+ * @op: the memory operation to execute
+ * @mask: status bitmask to ckeck
+ * @match: (status & mask) expected value
+ * @initial_delay_us: delay in us before starting to poll
+ * @polling_delay_us: time to sleep between reads in us
+ * @timeout_ms: timeout in milliseconds
+ *
+ * This function polls a status register and returns when
+ * (status & mask) == match or when the timeout has expired.
+ *
+ * Return: 0 in case of success, -ETIMEDOUT in case of error,
+ *         -EOPNOTSUPP if not supported.
+ */
+int spi_mem_poll_status(struct spi_mem *mem,
+                       const struct spi_mem_op *op,
+                       u16 mask, u16 match,
+                       unsigned long initial_delay_us,
+                       unsigned long polling_delay_us,
+                       u16 timeout_ms)
+{
+       struct spi_controller *ctlr = mem->spi->controller;
+       int ret = -EOPNOTSUPP;
+       int read_status_ret;
+       u16 status;
+
+       if (op->data.nbytes < 1 || op->data.nbytes > 2 ||
+           op->data.dir != SPI_MEM_DATA_IN)
+               return -EINVAL;
+
+       if (ctlr->mem_ops && ctlr->mem_ops->poll_status) {
+               ret = spi_mem_access_start(mem);
+               if (ret)
+                       return ret;
+
+               ret = ctlr->mem_ops->poll_status(mem, op, mask, match,
+                                                initial_delay_us, polling_delay_us,
+                                                timeout_ms);
+
+               spi_mem_access_end(mem);
+       }
+
+       if (ret == -EOPNOTSUPP) {
+               if (!spi_mem_supports_op(mem, op))
+                       return ret;
+
+               if (initial_delay_us < 10)
+                       udelay(initial_delay_us);
+               else
+                       usleep_range((initial_delay_us >> 2) + 1,
+                                    initial_delay_us);
+
+               ret = read_poll_timeout(spi_mem_read_status, read_status_ret,
+                                       (read_status_ret || ((status) & mask) == match),
+                                       polling_delay_us, timeout_ms * 1000, false, mem,
+                                       op, &status);
+               if (read_status_ret)
+                       return read_status_ret;
+       }
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(spi_mem_poll_status);
+
 static int spi_mem_probe(struct spi_device *spi)
 {
        struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
@@ -810,7 +896,7 @@ int spi_mem_driver_register_with_owner(struct spi_mem_driver *memdrv,
 EXPORT_SYMBOL_GPL(spi_mem_driver_register_with_owner);
 
 /**
- * spi_mem_driver_unregister_with_owner() - Unregister a SPI memory driver
+ * spi_mem_driver_unregister() - Unregister a SPI memory driver
  * @memdrv: the SPI memory driver to unregister
  *
  * Unregisters a SPI memory driver.