mirror of
https://github.com/AuxXxilium/linux_dsm_epyc7002.git
synced 2024-12-22 11:38:14 +07:00
a86854d0c5
The devm_kzalloc() function has a 2-factor argument form, devm_kcalloc(). This patch replaces cases of: devm_kzalloc(handle, a * b, gfp) with: devm_kcalloc(handle, a * b, gfp) as well as handling cases of: devm_kzalloc(handle, a * b * c, gfp) with: devm_kzalloc(handle, array3_size(a, b, c), gfp) as it's slightly less ugly than: devm_kcalloc(handle, array_size(a, b), c, gfp) This does, however, attempt to ignore constant size factors like: devm_kzalloc(handle, 4 * 1024, gfp) though any constants defined via macros get caught up in the conversion. Any factors with a sizeof() of "unsigned char", "char", and "u8" were dropped, since they're redundant. Some manual whitespace fixes were needed in this patch, as Coccinelle really liked to write "=devm_kcalloc..." instead of "= devm_kcalloc...". The Coccinelle script used for this was: // Fix redundant parens around sizeof(). @@ expression HANDLE; type TYPE; expression THING, E; @@ ( devm_kzalloc(HANDLE, - (sizeof(TYPE)) * E + sizeof(TYPE) * E , ...) | devm_kzalloc(HANDLE, - (sizeof(THING)) * E + sizeof(THING) * E , ...) ) // Drop single-byte sizes and redundant parens. @@ expression HANDLE; expression COUNT; typedef u8; typedef __u8; @@ ( devm_kzalloc(HANDLE, - sizeof(u8) * (COUNT) + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(__u8) * (COUNT) + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(char) * (COUNT) + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(unsigned char) * (COUNT) + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(u8) * COUNT + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(__u8) * COUNT + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(char) * COUNT + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(unsigned char) * COUNT + COUNT , ...) ) // 2-factor product with sizeof(type/expression) and identifier or constant. @@ expression HANDLE; type TYPE; expression THING; identifier COUNT_ID; constant COUNT_CONST; @@ ( - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(TYPE) * (COUNT_ID) + COUNT_ID, sizeof(TYPE) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(TYPE) * COUNT_ID + COUNT_ID, sizeof(TYPE) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(TYPE) * (COUNT_CONST) + COUNT_CONST, sizeof(TYPE) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(TYPE) * COUNT_CONST + COUNT_CONST, sizeof(TYPE) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(THING) * (COUNT_ID) + COUNT_ID, sizeof(THING) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(THING) * COUNT_ID + COUNT_ID, sizeof(THING) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(THING) * (COUNT_CONST) + COUNT_CONST, sizeof(THING) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(THING) * COUNT_CONST + COUNT_CONST, sizeof(THING) , ...) ) // 2-factor product, only identifiers. @@ expression HANDLE; identifier SIZE, COUNT; @@ - devm_kzalloc + devm_kcalloc (HANDLE, - SIZE * COUNT + COUNT, SIZE , ...) // 3-factor product with 1 sizeof(type) or sizeof(expression), with // redundant parens removed. @@ expression HANDLE; expression THING; identifier STRIDE, COUNT; type TYPE; @@ ( devm_kzalloc(HANDLE, - sizeof(TYPE) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | devm_kzalloc(HANDLE, - sizeof(TYPE) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | devm_kzalloc(HANDLE, - sizeof(TYPE) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | devm_kzalloc(HANDLE, - sizeof(TYPE) * COUNT * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | devm_kzalloc(HANDLE, - sizeof(THING) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | devm_kzalloc(HANDLE, - sizeof(THING) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | devm_kzalloc(HANDLE, - sizeof(THING) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | devm_kzalloc(HANDLE, - sizeof(THING) * COUNT * STRIDE + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) ) // 3-factor product with 2 sizeof(variable), with redundant parens removed. @@ expression HANDLE; expression THING1, THING2; identifier COUNT; type TYPE1, TYPE2; @@ ( devm_kzalloc(HANDLE, - sizeof(TYPE1) * sizeof(TYPE2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | devm_kzalloc(HANDLE, - sizeof(TYPE1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | devm_kzalloc(HANDLE, - sizeof(THING1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | devm_kzalloc(HANDLE, - sizeof(THING1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | devm_kzalloc(HANDLE, - sizeof(TYPE1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2)) , ...) | devm_kzalloc(HANDLE, - sizeof(TYPE1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2)) , ...) ) // 3-factor product, only identifiers, with redundant parens removed. @@ expression HANDLE; identifier STRIDE, SIZE, COUNT; @@ ( devm_kzalloc(HANDLE, - (COUNT) * STRIDE * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - COUNT * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - COUNT * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - (COUNT) * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - COUNT * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - (COUNT) * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - (COUNT) * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - COUNT * STRIDE * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) ) // Any remaining multi-factor products, first at least 3-factor products, // when they're not all constants... @@ expression HANDLE; expression E1, E2, E3; constant C1, C2, C3; @@ ( devm_kzalloc(HANDLE, C1 * C2 * C3, ...) | devm_kzalloc(HANDLE, - (E1) * E2 * E3 + array3_size(E1, E2, E3) , ...) | devm_kzalloc(HANDLE, - (E1) * (E2) * E3 + array3_size(E1, E2, E3) , ...) | devm_kzalloc(HANDLE, - (E1) * (E2) * (E3) + array3_size(E1, E2, E3) , ...) | devm_kzalloc(HANDLE, - E1 * E2 * E3 + array3_size(E1, E2, E3) , ...) ) // And then all remaining 2 factors products when they're not all constants, // keeping sizeof() as the second factor argument. @@ expression HANDLE; expression THING, E1, E2; type TYPE; constant C1, C2, C3; @@ ( devm_kzalloc(HANDLE, sizeof(THING) * C2, ...) | devm_kzalloc(HANDLE, sizeof(TYPE) * C2, ...) | devm_kzalloc(HANDLE, C1 * C2 * C3, ...) | devm_kzalloc(HANDLE, C1 * C2, ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(TYPE) * (E2) + E2, sizeof(TYPE) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(TYPE) * E2 + E2, sizeof(TYPE) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(THING) * (E2) + E2, sizeof(THING) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(THING) * E2 + E2, sizeof(THING) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - (E1) * E2 + E1, E2 , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - (E1) * (E2) + E1, E2 , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - E1 * E2 + E1, E2 , ...) ) Signed-off-by: Kees Cook <keescook@chromium.org>
463 lines
13 KiB
C
463 lines
13 KiB
C
/*
|
|
* SPI master driver using generic bitbanged GPIO
|
|
*
|
|
* Copyright (C) 2006,2008 David Brownell
|
|
* Copyright (C) 2017 Linus Walleij
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*/
|
|
#include <linux/kernel.h>
|
|
#include <linux/module.h>
|
|
#include <linux/platform_device.h>
|
|
#include <linux/gpio/consumer.h>
|
|
#include <linux/of.h>
|
|
#include <linux/of_device.h>
|
|
|
|
#include <linux/spi/spi.h>
|
|
#include <linux/spi/spi_bitbang.h>
|
|
#include <linux/spi/spi_gpio.h>
|
|
|
|
|
|
/*
|
|
* This bitbanging SPI master driver should help make systems usable
|
|
* when a native hardware SPI engine is not available, perhaps because
|
|
* its driver isn't yet working or because the I/O pins it requires
|
|
* are used for other purposes.
|
|
*
|
|
* platform_device->driver_data ... points to spi_gpio
|
|
*
|
|
* spi->controller_state ... reserved for bitbang framework code
|
|
* spi->controller_data ... holds chipselect GPIO
|
|
*
|
|
* spi->master->dev.driver_data ... points to spi_gpio->bitbang
|
|
*/
|
|
|
|
struct spi_gpio {
|
|
struct spi_bitbang bitbang;
|
|
struct spi_gpio_platform_data pdata;
|
|
struct platform_device *pdev;
|
|
struct gpio_desc *sck;
|
|
struct gpio_desc *miso;
|
|
struct gpio_desc *mosi;
|
|
struct gpio_desc **cs_gpios;
|
|
bool has_cs;
|
|
};
|
|
|
|
/*----------------------------------------------------------------------*/
|
|
|
|
/*
|
|
* Because the overhead of going through four GPIO procedure calls
|
|
* per transferred bit can make performance a problem, this code
|
|
* is set up so that you can use it in either of two ways:
|
|
*
|
|
* - The slow generic way: set up platform_data to hold the GPIO
|
|
* numbers used for MISO/MOSI/SCK, and issue procedure calls for
|
|
* each of them. This driver can handle several such busses.
|
|
*
|
|
* - The quicker inlined way: only helps with platform GPIO code
|
|
* that inlines operations for constant GPIOs. This can give
|
|
* you tight (fast!) inner loops, but each such bus needs a
|
|
* new driver. You'll define a new C file, with Makefile and
|
|
* Kconfig support; the C code can be a total of six lines:
|
|
*
|
|
* #define DRIVER_NAME "myboard_spi2"
|
|
* #define SPI_MISO_GPIO 119
|
|
* #define SPI_MOSI_GPIO 120
|
|
* #define SPI_SCK_GPIO 121
|
|
* #define SPI_N_CHIPSEL 4
|
|
* #include "spi-gpio.c"
|
|
*/
|
|
|
|
#ifndef DRIVER_NAME
|
|
#define DRIVER_NAME "spi_gpio"
|
|
|
|
#define GENERIC_BITBANG /* vs tight inlines */
|
|
|
|
#endif
|
|
|
|
/*----------------------------------------------------------------------*/
|
|
|
|
static inline struct spi_gpio *__pure
|
|
spi_to_spi_gpio(const struct spi_device *spi)
|
|
{
|
|
const struct spi_bitbang *bang;
|
|
struct spi_gpio *spi_gpio;
|
|
|
|
bang = spi_master_get_devdata(spi->master);
|
|
spi_gpio = container_of(bang, struct spi_gpio, bitbang);
|
|
return spi_gpio;
|
|
}
|
|
|
|
static inline struct spi_gpio_platform_data *__pure
|
|
spi_to_pdata(const struct spi_device *spi)
|
|
{
|
|
return &spi_to_spi_gpio(spi)->pdata;
|
|
}
|
|
|
|
/* These helpers are in turn called by the bitbang inlines */
|
|
static inline void setsck(const struct spi_device *spi, int is_on)
|
|
{
|
|
struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
|
|
|
|
gpiod_set_value_cansleep(spi_gpio->sck, is_on);
|
|
}
|
|
|
|
static inline void setmosi(const struct spi_device *spi, int is_on)
|
|
{
|
|
struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
|
|
|
|
gpiod_set_value_cansleep(spi_gpio->mosi, is_on);
|
|
}
|
|
|
|
static inline int getmiso(const struct spi_device *spi)
|
|
{
|
|
struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
|
|
|
|
return !!gpiod_get_value_cansleep(spi_gpio->miso);
|
|
}
|
|
|
|
/*
|
|
* NOTE: this clocks "as fast as we can". It "should" be a function of the
|
|
* requested device clock. Software overhead means we usually have trouble
|
|
* reaching even one Mbit/sec (except when we can inline bitops), so for now
|
|
* we'll just assume we never need additional per-bit slowdowns.
|
|
*/
|
|
#define spidelay(nsecs) do {} while (0)
|
|
|
|
#include "spi-bitbang-txrx.h"
|
|
|
|
/*
|
|
* These functions can leverage inline expansion of GPIO calls to shrink
|
|
* costs for a txrx bit, often by factors of around ten (by instruction
|
|
* count). That is particularly visible for larger word sizes, but helps
|
|
* even with default 8-bit words.
|
|
*
|
|
* REVISIT overheads calling these functions for each word also have
|
|
* significant performance costs. Having txrx_bufs() calls that inline
|
|
* the txrx_word() logic would help performance, e.g. on larger blocks
|
|
* used with flash storage or MMC/SD. There should also be ways to make
|
|
* GCC be less stupid about reloading registers inside the I/O loops,
|
|
* even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3?
|
|
*/
|
|
|
|
static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi,
|
|
unsigned nsecs, u32 word, u8 bits)
|
|
{
|
|
return bitbang_txrx_be_cpha0(spi, nsecs, 0, 0, word, bits);
|
|
}
|
|
|
|
static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
|
|
unsigned nsecs, u32 word, u8 bits)
|
|
{
|
|
return bitbang_txrx_be_cpha1(spi, nsecs, 0, 0, word, bits);
|
|
}
|
|
|
|
static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
|
|
unsigned nsecs, u32 word, u8 bits)
|
|
{
|
|
return bitbang_txrx_be_cpha0(spi, nsecs, 1, 0, word, bits);
|
|
}
|
|
|
|
static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
|
|
unsigned nsecs, u32 word, u8 bits)
|
|
{
|
|
return bitbang_txrx_be_cpha1(spi, nsecs, 1, 0, word, bits);
|
|
}
|
|
|
|
/*
|
|
* These functions do not call setmosi or getmiso if respective flag
|
|
* (SPI_MASTER_NO_RX or SPI_MASTER_NO_TX) is set, so they are safe to
|
|
* call when such pin is not present or defined in the controller.
|
|
* A separate set of callbacks is defined to get highest possible
|
|
* speed in the generic case (when both MISO and MOSI lines are
|
|
* available), as optimiser will remove the checks when argument is
|
|
* constant.
|
|
*/
|
|
|
|
static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi,
|
|
unsigned nsecs, u32 word, u8 bits)
|
|
{
|
|
unsigned flags = spi->master->flags;
|
|
return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
|
|
}
|
|
|
|
static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi,
|
|
unsigned nsecs, u32 word, u8 bits)
|
|
{
|
|
unsigned flags = spi->master->flags;
|
|
return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
|
|
}
|
|
|
|
static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi,
|
|
unsigned nsecs, u32 word, u8 bits)
|
|
{
|
|
unsigned flags = spi->master->flags;
|
|
return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
|
|
}
|
|
|
|
static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi,
|
|
unsigned nsecs, u32 word, u8 bits)
|
|
{
|
|
unsigned flags = spi->master->flags;
|
|
return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
|
|
}
|
|
|
|
/*----------------------------------------------------------------------*/
|
|
|
|
static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
|
|
{
|
|
struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
|
|
|
|
/* set initial clock line level */
|
|
if (is_active)
|
|
gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL);
|
|
|
|
/* Drive chip select line, if we have one */
|
|
if (spi_gpio->has_cs) {
|
|
struct gpio_desc *cs = spi_gpio->cs_gpios[spi->chip_select];
|
|
|
|
/* SPI chip selects are normally active-low */
|
|
gpiod_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
|
|
}
|
|
}
|
|
|
|
static int spi_gpio_setup(struct spi_device *spi)
|
|
{
|
|
struct gpio_desc *cs;
|
|
int status = 0;
|
|
struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
|
|
|
|
/*
|
|
* The CS GPIOs have already been
|
|
* initialized from the descriptor lookup.
|
|
*/
|
|
cs = spi_gpio->cs_gpios[spi->chip_select];
|
|
if (!spi->controller_state && cs)
|
|
status = gpiod_direction_output(cs,
|
|
!(spi->mode & SPI_CS_HIGH));
|
|
|
|
if (!status)
|
|
status = spi_bitbang_setup(spi);
|
|
|
|
return status;
|
|
}
|
|
|
|
static void spi_gpio_cleanup(struct spi_device *spi)
|
|
{
|
|
spi_bitbang_cleanup(spi);
|
|
}
|
|
|
|
/*
|
|
* It can be convenient to use this driver with pins that have alternate
|
|
* functions associated with a "native" SPI controller if a driver for that
|
|
* controller is not available, or is missing important functionality.
|
|
*
|
|
* On platforms which can do so, configure MISO with a weak pullup unless
|
|
* there's an external pullup on that signal. That saves power by avoiding
|
|
* floating signals. (A weak pulldown would save power too, but many
|
|
* drivers expect to see all-ones data as the no slave "response".)
|
|
*/
|
|
static int spi_gpio_request(struct device *dev,
|
|
struct spi_gpio *spi_gpio,
|
|
unsigned int num_chipselects,
|
|
u16 *mflags)
|
|
{
|
|
int i;
|
|
|
|
spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW);
|
|
if (IS_ERR(spi_gpio->mosi))
|
|
return PTR_ERR(spi_gpio->mosi);
|
|
if (!spi_gpio->mosi)
|
|
/* HW configuration without MOSI pin */
|
|
*mflags |= SPI_MASTER_NO_TX;
|
|
|
|
spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN);
|
|
if (IS_ERR(spi_gpio->miso))
|
|
return PTR_ERR(spi_gpio->miso);
|
|
if (!spi_gpio->miso)
|
|
/* HW configuration without MISO pin */
|
|
*mflags |= SPI_MASTER_NO_RX;
|
|
|
|
spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
|
|
if (IS_ERR(spi_gpio->mosi))
|
|
return PTR_ERR(spi_gpio->mosi);
|
|
|
|
for (i = 0; i < num_chipselects; i++) {
|
|
spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs",
|
|
i, GPIOD_OUT_HIGH);
|
|
if (IS_ERR(spi_gpio->cs_gpios[i]))
|
|
return PTR_ERR(spi_gpio->cs_gpios[i]);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
#ifdef CONFIG_OF
|
|
static const struct of_device_id spi_gpio_dt_ids[] = {
|
|
{ .compatible = "spi-gpio" },
|
|
{}
|
|
};
|
|
MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids);
|
|
|
|
static int spi_gpio_probe_dt(struct platform_device *pdev)
|
|
{
|
|
int ret;
|
|
u32 tmp;
|
|
struct spi_gpio_platform_data *pdata;
|
|
struct device_node *np = pdev->dev.of_node;
|
|
const struct of_device_id *of_id =
|
|
of_match_device(spi_gpio_dt_ids, &pdev->dev);
|
|
|
|
if (!of_id)
|
|
return 0;
|
|
|
|
pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
|
|
if (!pdata)
|
|
return -ENOMEM;
|
|
|
|
|
|
ret = of_property_read_u32(np, "num-chipselects", &tmp);
|
|
if (ret < 0) {
|
|
dev_err(&pdev->dev, "num-chipselects property not found\n");
|
|
goto error_free;
|
|
}
|
|
|
|
pdata->num_chipselect = tmp;
|
|
pdev->dev.platform_data = pdata;
|
|
|
|
return 1;
|
|
|
|
error_free:
|
|
devm_kfree(&pdev->dev, pdata);
|
|
return ret;
|
|
}
|
|
#else
|
|
static inline int spi_gpio_probe_dt(struct platform_device *pdev)
|
|
{
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
static int spi_gpio_probe(struct platform_device *pdev)
|
|
{
|
|
int status;
|
|
struct spi_master *master;
|
|
struct spi_gpio *spi_gpio;
|
|
struct spi_gpio_platform_data *pdata;
|
|
u16 master_flags = 0;
|
|
bool use_of = 0;
|
|
|
|
status = spi_gpio_probe_dt(pdev);
|
|
if (status < 0)
|
|
return status;
|
|
if (status > 0)
|
|
use_of = 1;
|
|
|
|
pdata = dev_get_platdata(&pdev->dev);
|
|
#ifdef GENERIC_BITBANG
|
|
if (!pdata || (!use_of && !pdata->num_chipselect))
|
|
return -ENODEV;
|
|
#endif
|
|
|
|
master = spi_alloc_master(&pdev->dev, sizeof(*spi_gpio));
|
|
if (!master)
|
|
return -ENOMEM;
|
|
|
|
spi_gpio = spi_master_get_devdata(master);
|
|
|
|
spi_gpio->cs_gpios = devm_kcalloc(&pdev->dev,
|
|
pdata->num_chipselect,
|
|
sizeof(*spi_gpio->cs_gpios),
|
|
GFP_KERNEL);
|
|
if (!spi_gpio->cs_gpios)
|
|
return -ENOMEM;
|
|
|
|
platform_set_drvdata(pdev, spi_gpio);
|
|
|
|
/* Determine if we have chip selects connected */
|
|
spi_gpio->has_cs = !!pdata->num_chipselect;
|
|
|
|
spi_gpio->pdev = pdev;
|
|
if (pdata)
|
|
spi_gpio->pdata = *pdata;
|
|
|
|
status = spi_gpio_request(&pdev->dev, spi_gpio,
|
|
pdata->num_chipselect, &master_flags);
|
|
if (status)
|
|
return status;
|
|
|
|
master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
|
|
master->flags = master_flags;
|
|
master->bus_num = pdev->id;
|
|
/* The master needs to think there is a chipselect even if not connected */
|
|
master->num_chipselect = spi_gpio->has_cs ? pdata->num_chipselect : 1;
|
|
master->setup = spi_gpio_setup;
|
|
master->cleanup = spi_gpio_cleanup;
|
|
#ifdef CONFIG_OF
|
|
master->dev.of_node = pdev->dev.of_node;
|
|
#endif
|
|
|
|
spi_gpio->bitbang.master = master;
|
|
spi_gpio->bitbang.chipselect = spi_gpio_chipselect;
|
|
|
|
if ((master_flags & (SPI_MASTER_NO_TX | SPI_MASTER_NO_RX)) == 0) {
|
|
spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
|
|
spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
|
|
spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
|
|
spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
|
|
} else {
|
|
spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
|
|
spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
|
|
spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
|
|
spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
|
|
}
|
|
spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
|
|
spi_gpio->bitbang.flags = SPI_CS_HIGH;
|
|
|
|
status = spi_bitbang_start(&spi_gpio->bitbang);
|
|
if (status)
|
|
spi_master_put(master);
|
|
|
|
return status;
|
|
}
|
|
|
|
static int spi_gpio_remove(struct platform_device *pdev)
|
|
{
|
|
struct spi_gpio *spi_gpio;
|
|
struct spi_gpio_platform_data *pdata;
|
|
|
|
spi_gpio = platform_get_drvdata(pdev);
|
|
pdata = dev_get_platdata(&pdev->dev);
|
|
|
|
/* stop() unregisters child devices too */
|
|
spi_bitbang_stop(&spi_gpio->bitbang);
|
|
|
|
spi_master_put(spi_gpio->bitbang.master);
|
|
|
|
return 0;
|
|
}
|
|
|
|
MODULE_ALIAS("platform:" DRIVER_NAME);
|
|
|
|
static struct platform_driver spi_gpio_driver = {
|
|
.driver = {
|
|
.name = DRIVER_NAME,
|
|
.of_match_table = of_match_ptr(spi_gpio_dt_ids),
|
|
},
|
|
.probe = spi_gpio_probe,
|
|
.remove = spi_gpio_remove,
|
|
};
|
|
module_platform_driver(spi_gpio_driver);
|
|
|
|
MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO ");
|
|
MODULE_AUTHOR("David Brownell");
|
|
MODULE_LICENSE("GPL");
|