2012-11-08 01:30:29 +08:00
|
|
|
/*
|
|
|
|
* CLPS711X SPI bus driver
|
|
|
|
*
|
2014-01-10 21:02:05 +08:00
|
|
|
* Copyright (C) 2012-2014 Alexander Shiyan <shc_work@mail.ru>
|
2012-11-08 01:30:29 +08:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/gpio.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/spi/spi.h>
|
|
|
|
#include <linux/platform_data/spi-clps711x.h>
|
|
|
|
|
|
|
|
#include <mach/hardware.h>
|
|
|
|
|
|
|
|
#define DRIVER_NAME "spi-clps711x"
|
|
|
|
|
|
|
|
struct spi_clps711x_data {
|
|
|
|
struct completion done;
|
|
|
|
|
|
|
|
struct clk *spi_clk;
|
|
|
|
u32 max_speed_hz;
|
|
|
|
|
|
|
|
u8 *tx_buf;
|
|
|
|
u8 *rx_buf;
|
2014-02-02 14:59:49 +08:00
|
|
|
unsigned int bpw;
|
2012-11-08 01:30:29 +08:00
|
|
|
int len;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int spi_clps711x_setup(struct spi_device *spi)
|
|
|
|
{
|
|
|
|
/* We are expect that SPI-device is not selected */
|
2014-02-02 14:59:50 +08:00
|
|
|
gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
|
2012-11-08 01:30:29 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void spi_clps711x_setup_mode(struct spi_device *spi)
|
|
|
|
{
|
|
|
|
/* Setup edge for transfer */
|
|
|
|
if (spi->mode & SPI_CPHA)
|
|
|
|
clps_writew(clps_readw(SYSCON3) | SYSCON3_ADCCKNSEN, SYSCON3);
|
|
|
|
else
|
|
|
|
clps_writew(clps_readw(SYSCON3) & ~SYSCON3_ADCCKNSEN, SYSCON3);
|
|
|
|
}
|
|
|
|
|
2014-02-02 14:59:49 +08:00
|
|
|
static void spi_clps711x_setup_xfer(struct spi_device *spi,
|
|
|
|
struct spi_transfer *xfer)
|
2012-11-08 01:30:29 +08:00
|
|
|
{
|
|
|
|
u32 speed = xfer->speed_hz ? : spi->max_speed_hz;
|
|
|
|
struct spi_clps711x_data *hw = spi_master_get_devdata(spi->master);
|
|
|
|
|
|
|
|
/* Setup SPI frequency divider */
|
|
|
|
if (!speed || (speed >= hw->max_speed_hz))
|
|
|
|
clps_writel((clps_readl(SYSCON1) & ~SYSCON1_ADCKSEL_MASK) |
|
|
|
|
SYSCON1_ADCKSEL(3), SYSCON1);
|
|
|
|
else if (speed >= (hw->max_speed_hz / 2))
|
|
|
|
clps_writel((clps_readl(SYSCON1) & ~SYSCON1_ADCKSEL_MASK) |
|
|
|
|
SYSCON1_ADCKSEL(2), SYSCON1);
|
|
|
|
else if (speed >= (hw->max_speed_hz / 8))
|
|
|
|
clps_writel((clps_readl(SYSCON1) & ~SYSCON1_ADCKSEL_MASK) |
|
|
|
|
SYSCON1_ADCKSEL(1), SYSCON1);
|
|
|
|
else
|
|
|
|
clps_writel((clps_readl(SYSCON1) & ~SYSCON1_ADCKSEL_MASK) |
|
|
|
|
SYSCON1_ADCKSEL(0), SYSCON1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int spi_clps711x_transfer_one_message(struct spi_master *master,
|
|
|
|
struct spi_message *msg)
|
|
|
|
{
|
|
|
|
struct spi_clps711x_data *hw = spi_master_get_devdata(master);
|
2014-02-02 14:59:49 +08:00
|
|
|
struct spi_device *spi = msg->spi;
|
2012-11-08 01:30:29 +08:00
|
|
|
struct spi_transfer *xfer;
|
|
|
|
|
2014-02-02 14:59:49 +08:00
|
|
|
spi_clps711x_setup_mode(spi);
|
2012-11-08 01:30:29 +08:00
|
|
|
|
|
|
|
list_for_each_entry(xfer, &msg->transfers, transfer_list) {
|
2014-02-02 14:59:48 +08:00
|
|
|
u8 data;
|
|
|
|
|
2014-02-02 14:59:49 +08:00
|
|
|
spi_clps711x_setup_xfer(spi, xfer);
|
2012-11-08 01:30:29 +08:00
|
|
|
|
2014-02-02 14:59:50 +08:00
|
|
|
gpio_set_value(spi->cs_gpio, !!(spi->mode & SPI_CS_HIGH));
|
2012-11-08 01:30:29 +08:00
|
|
|
|
2013-11-15 06:32:02 +08:00
|
|
|
reinit_completion(&hw->done);
|
2012-11-08 01:30:29 +08:00
|
|
|
|
|
|
|
hw->len = xfer->len;
|
2014-02-02 14:59:49 +08:00
|
|
|
hw->bpw = xfer->bits_per_word ? : spi->bits_per_word;
|
2012-11-08 01:30:29 +08:00
|
|
|
hw->tx_buf = (u8 *)xfer->tx_buf;
|
|
|
|
hw->rx_buf = (u8 *)xfer->rx_buf;
|
|
|
|
|
|
|
|
/* Initiate transfer */
|
2014-02-02 14:59:48 +08:00
|
|
|
data = hw->tx_buf ? *hw->tx_buf++ : 0;
|
2014-02-02 14:59:49 +08:00
|
|
|
clps_writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN,
|
|
|
|
SYNCIO);
|
2012-11-08 01:30:29 +08:00
|
|
|
|
|
|
|
wait_for_completion(&hw->done);
|
|
|
|
|
|
|
|
if (xfer->delay_usecs)
|
|
|
|
udelay(xfer->delay_usecs);
|
|
|
|
|
|
|
|
if (xfer->cs_change ||
|
|
|
|
list_is_last(&xfer->transfer_list, &msg->transfers))
|
2014-02-02 14:59:50 +08:00
|
|
|
gpio_set_value(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
|
2012-11-08 01:30:29 +08:00
|
|
|
|
|
|
|
msg->actual_length += xfer->len;
|
|
|
|
}
|
|
|
|
|
2014-02-02 14:59:49 +08:00
|
|
|
msg->status = 0;
|
2012-11-08 01:30:29 +08:00
|
|
|
spi_finalize_current_message(master);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t spi_clps711x_isr(int irq, void *dev_id)
|
|
|
|
{
|
|
|
|
struct spi_clps711x_data *hw = (struct spi_clps711x_data *)dev_id;
|
2014-02-02 14:59:48 +08:00
|
|
|
u8 data;
|
2012-11-08 01:30:29 +08:00
|
|
|
|
|
|
|
/* Handle RX */
|
|
|
|
data = clps_readb(SYNCIO);
|
|
|
|
if (hw->rx_buf)
|
2014-02-02 14:59:48 +08:00
|
|
|
*hw->rx_buf++ = data;
|
2012-11-08 01:30:29 +08:00
|
|
|
|
|
|
|
/* Handle TX */
|
2014-02-02 14:59:48 +08:00
|
|
|
if (--hw->len > 0) {
|
|
|
|
data = hw->tx_buf ? *hw->tx_buf++ : 0;
|
2014-02-02 14:59:49 +08:00
|
|
|
clps_writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN,
|
|
|
|
SYNCIO);
|
2012-11-08 01:30:29 +08:00
|
|
|
} else
|
|
|
|
complete(&hw->done);
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2012-12-08 00:57:14 +08:00
|
|
|
static int spi_clps711x_probe(struct platform_device *pdev)
|
2012-11-08 01:30:29 +08:00
|
|
|
{
|
|
|
|
int i, ret;
|
|
|
|
struct spi_master *master;
|
|
|
|
struct spi_clps711x_data *hw;
|
|
|
|
struct spi_clps711x_pdata *pdata = dev_get_platdata(&pdev->dev);
|
|
|
|
|
|
|
|
if (!pdata) {
|
|
|
|
dev_err(&pdev->dev, "No platform data supplied\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pdata->num_chipselect < 1) {
|
|
|
|
dev_err(&pdev->dev, "At least one CS must be defined\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-02-02 14:59:50 +08:00
|
|
|
master = spi_alloc_master(&pdev->dev, sizeof(*hw));
|
|
|
|
if (!master)
|
2012-11-08 01:30:29 +08:00
|
|
|
return -ENOMEM;
|
2014-02-02 14:59:50 +08:00
|
|
|
|
|
|
|
master->cs_gpios = devm_kzalloc(&pdev->dev, sizeof(int) *
|
|
|
|
pdata->num_chipselect, GFP_KERNEL);
|
|
|
|
if (!master->cs_gpios) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_out;
|
2012-11-08 01:30:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
master->bus_num = pdev->id;
|
|
|
|
master->mode_bits = SPI_CPHA | SPI_CS_HIGH;
|
2014-02-02 14:59:49 +08:00
|
|
|
master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 8);
|
2012-11-08 01:30:29 +08:00
|
|
|
master->num_chipselect = pdata->num_chipselect;
|
|
|
|
master->setup = spi_clps711x_setup;
|
|
|
|
master->transfer_one_message = spi_clps711x_transfer_one_message;
|
|
|
|
|
|
|
|
hw = spi_master_get_devdata(master);
|
|
|
|
|
|
|
|
for (i = 0; i < master->num_chipselect; i++) {
|
2014-02-02 14:59:50 +08:00
|
|
|
master->cs_gpios[i] = pdata->chipselect[i];
|
|
|
|
if (!gpio_is_valid(master->cs_gpios[i])) {
|
2012-11-08 01:30:29 +08:00
|
|
|
dev_err(&pdev->dev, "Invalid CS GPIO %i\n", i);
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto err_out;
|
|
|
|
}
|
2014-02-02 14:59:50 +08:00
|
|
|
if (devm_gpio_request(&pdev->dev, master->cs_gpios[i], NULL)) {
|
2012-11-08 01:30:29 +08:00
|
|
|
dev_err(&pdev->dev, "Can't get CS GPIO %i\n", i);
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hw->spi_clk = devm_clk_get(&pdev->dev, "spi");
|
|
|
|
if (IS_ERR(hw->spi_clk)) {
|
|
|
|
dev_err(&pdev->dev, "Can't get clocks\n");
|
|
|
|
ret = PTR_ERR(hw->spi_clk);
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
hw->max_speed_hz = clk_get_rate(hw->spi_clk);
|
|
|
|
|
|
|
|
init_completion(&hw->done);
|
|
|
|
platform_set_drvdata(pdev, master);
|
|
|
|
|
|
|
|
/* Disable extended mode due hardware problems */
|
|
|
|
clps_writew(clps_readw(SYSCON3) & ~SYSCON3_ADCCON, SYSCON3);
|
|
|
|
|
|
|
|
/* Clear possible pending interrupt */
|
|
|
|
clps_readl(SYNCIO);
|
|
|
|
|
|
|
|
ret = devm_request_irq(&pdev->dev, IRQ_SSEOTI, spi_clps711x_isr, 0,
|
|
|
|
dev_name(&pdev->dev), hw);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&pdev->dev, "Can't request IRQ\n");
|
2013-09-27 18:02:53 +08:00
|
|
|
goto err_out;
|
2012-11-08 01:30:29 +08:00
|
|
|
}
|
|
|
|
|
2013-09-24 12:27:48 +08:00
|
|
|
ret = devm_spi_register_master(&pdev->dev, master);
|
2012-11-08 01:30:29 +08:00
|
|
|
if (!ret) {
|
|
|
|
dev_info(&pdev->dev,
|
|
|
|
"SPI bus driver initialized. Master clock %u Hz\n",
|
|
|
|
hw->max_speed_hz);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_err(&pdev->dev, "Failed to register master\n");
|
|
|
|
|
|
|
|
err_out:
|
|
|
|
spi_master_put(master);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver clps711x_spi_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = DRIVER_NAME,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
},
|
|
|
|
.probe = spi_clps711x_probe,
|
|
|
|
};
|
|
|
|
module_platform_driver(clps711x_spi_driver);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
|
|
|
|
MODULE_DESCRIPTION("CLPS711X SPI bus driver");
|
2014-01-14 17:01:54 +08:00
|
|
|
MODULE_ALIAS("platform:" DRIVER_NAME);
|