Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Analog Devices AD-FMCOMMS1-EBZ board I2C-SPI bridge driver
0004  *
0005  * Copyright 2012 Analog Devices Inc.
0006  * Author: Lars-Peter Clausen <lars@metafoo.de>
0007  */
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/delay.h>
0012 #include <linux/i2c.h>
0013 #include <linux/spi/spi.h>
0014 #include <asm/unaligned.h>
0015 
0016 #define SPI_XCOMM_SETTINGS_LEN_OFFSET       10
0017 #define SPI_XCOMM_SETTINGS_3WIRE        BIT(6)
0018 #define SPI_XCOMM_SETTINGS_CS_HIGH      BIT(5)
0019 #define SPI_XCOMM_SETTINGS_SAMPLE_END       BIT(4)
0020 #define SPI_XCOMM_SETTINGS_CPHA         BIT(3)
0021 #define SPI_XCOMM_SETTINGS_CPOL         BIT(2)
0022 #define SPI_XCOMM_SETTINGS_CLOCK_DIV_MASK   0x3
0023 #define SPI_XCOMM_SETTINGS_CLOCK_DIV_64     0x2
0024 #define SPI_XCOMM_SETTINGS_CLOCK_DIV_16     0x1
0025 #define SPI_XCOMM_SETTINGS_CLOCK_DIV_4      0x0
0026 
0027 #define SPI_XCOMM_CMD_UPDATE_CONFIG 0x03
0028 #define SPI_XCOMM_CMD_WRITE     0x04
0029 
0030 #define SPI_XCOMM_CLOCK 48000000
0031 
0032 struct spi_xcomm {
0033     struct i2c_client *i2c;
0034 
0035     uint16_t settings;
0036     uint16_t chipselect;
0037 
0038     unsigned int current_speed;
0039 
0040     uint8_t buf[63];
0041 };
0042 
0043 static int spi_xcomm_sync_config(struct spi_xcomm *spi_xcomm, unsigned int len)
0044 {
0045     uint16_t settings;
0046     uint8_t *buf = spi_xcomm->buf;
0047 
0048     settings = spi_xcomm->settings;
0049     settings |= len << SPI_XCOMM_SETTINGS_LEN_OFFSET;
0050 
0051     buf[0] = SPI_XCOMM_CMD_UPDATE_CONFIG;
0052     put_unaligned_be16(settings, &buf[1]);
0053     put_unaligned_be16(spi_xcomm->chipselect, &buf[3]);
0054 
0055     return i2c_master_send(spi_xcomm->i2c, buf, 5);
0056 }
0057 
0058 static void spi_xcomm_chipselect(struct spi_xcomm *spi_xcomm,
0059     struct spi_device *spi, int is_active)
0060 {
0061     unsigned long cs = spi->chip_select;
0062     uint16_t chipselect = spi_xcomm->chipselect;
0063 
0064     if (is_active)
0065         chipselect |= BIT(cs);
0066     else
0067         chipselect &= ~BIT(cs);
0068 
0069     spi_xcomm->chipselect = chipselect;
0070 }
0071 
0072 static int spi_xcomm_setup_transfer(struct spi_xcomm *spi_xcomm,
0073     struct spi_device *spi, struct spi_transfer *t, unsigned int *settings)
0074 {
0075     if (t->len > 62)
0076         return -EINVAL;
0077 
0078     if (t->speed_hz != spi_xcomm->current_speed) {
0079         unsigned int divider;
0080 
0081         divider = DIV_ROUND_UP(SPI_XCOMM_CLOCK, t->speed_hz);
0082         if (divider >= 64)
0083             *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_64;
0084         else if (divider >= 16)
0085             *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_16;
0086         else
0087             *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_4;
0088 
0089         spi_xcomm->current_speed = t->speed_hz;
0090     }
0091 
0092     if (spi->mode & SPI_CPOL)
0093         *settings |= SPI_XCOMM_SETTINGS_CPOL;
0094     else
0095         *settings &= ~SPI_XCOMM_SETTINGS_CPOL;
0096 
0097     if (spi->mode & SPI_CPHA)
0098         *settings &= ~SPI_XCOMM_SETTINGS_CPHA;
0099     else
0100         *settings |= SPI_XCOMM_SETTINGS_CPHA;
0101 
0102     if (spi->mode & SPI_3WIRE)
0103         *settings |= SPI_XCOMM_SETTINGS_3WIRE;
0104     else
0105         *settings &= ~SPI_XCOMM_SETTINGS_3WIRE;
0106 
0107     return 0;
0108 }
0109 
0110 static int spi_xcomm_txrx_bufs(struct spi_xcomm *spi_xcomm,
0111     struct spi_device *spi, struct spi_transfer *t)
0112 {
0113     int ret;
0114 
0115     if (t->tx_buf) {
0116         spi_xcomm->buf[0] = SPI_XCOMM_CMD_WRITE;
0117         memcpy(spi_xcomm->buf + 1, t->tx_buf, t->len);
0118 
0119         ret = i2c_master_send(spi_xcomm->i2c, spi_xcomm->buf, t->len + 1);
0120         if (ret < 0)
0121             return ret;
0122         else if (ret != t->len + 1)
0123             return -EIO;
0124     } else if (t->rx_buf) {
0125         ret = i2c_master_recv(spi_xcomm->i2c, t->rx_buf, t->len);
0126         if (ret < 0)
0127             return ret;
0128         else if (ret != t->len)
0129             return -EIO;
0130     }
0131 
0132     return t->len;
0133 }
0134 
0135 static int spi_xcomm_transfer_one(struct spi_master *master,
0136     struct spi_message *msg)
0137 {
0138     struct spi_xcomm *spi_xcomm = spi_master_get_devdata(master);
0139     unsigned int settings = spi_xcomm->settings;
0140     struct spi_device *spi = msg->spi;
0141     unsigned cs_change = 0;
0142     struct spi_transfer *t;
0143     bool is_first = true;
0144     int status = 0;
0145     bool is_last;
0146 
0147     spi_xcomm_chipselect(spi_xcomm, spi, true);
0148 
0149     list_for_each_entry(t, &msg->transfers, transfer_list) {
0150 
0151         if (!t->tx_buf && !t->rx_buf && t->len) {
0152             status = -EINVAL;
0153             break;
0154         }
0155 
0156         status = spi_xcomm_setup_transfer(spi_xcomm, spi, t, &settings);
0157         if (status < 0)
0158             break;
0159 
0160         is_last = list_is_last(&t->transfer_list, &msg->transfers);
0161         cs_change = t->cs_change;
0162 
0163         if (cs_change ^ is_last)
0164             settings |= BIT(5);
0165         else
0166             settings &= ~BIT(5);
0167 
0168         if (t->rx_buf) {
0169             spi_xcomm->settings = settings;
0170             status = spi_xcomm_sync_config(spi_xcomm, t->len);
0171             if (status < 0)
0172                 break;
0173         } else if (settings != spi_xcomm->settings || is_first) {
0174             spi_xcomm->settings = settings;
0175             status = spi_xcomm_sync_config(spi_xcomm, 0);
0176             if (status < 0)
0177                 break;
0178         }
0179 
0180         if (t->len) {
0181             status = spi_xcomm_txrx_bufs(spi_xcomm, spi, t);
0182 
0183             if (status < 0)
0184                 break;
0185 
0186             if (status > 0)
0187                 msg->actual_length += status;
0188         }
0189         status = 0;
0190 
0191         spi_transfer_delay_exec(t);
0192 
0193         is_first = false;
0194     }
0195 
0196     if (status != 0 || !cs_change)
0197         spi_xcomm_chipselect(spi_xcomm, spi, false);
0198 
0199     msg->status = status;
0200     spi_finalize_current_message(master);
0201 
0202     return status;
0203 }
0204 
0205 static int spi_xcomm_probe(struct i2c_client *i2c,
0206     const struct i2c_device_id *id)
0207 {
0208     struct spi_xcomm *spi_xcomm;
0209     struct spi_master *master;
0210     int ret;
0211 
0212     master = spi_alloc_master(&i2c->dev, sizeof(*spi_xcomm));
0213     if (!master)
0214         return -ENOMEM;
0215 
0216     spi_xcomm = spi_master_get_devdata(master);
0217     spi_xcomm->i2c = i2c;
0218 
0219     master->num_chipselect = 16;
0220     master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_3WIRE;
0221     master->bits_per_word_mask = SPI_BPW_MASK(8);
0222     master->flags = SPI_MASTER_HALF_DUPLEX;
0223     master->transfer_one_message = spi_xcomm_transfer_one;
0224     master->dev.of_node = i2c->dev.of_node;
0225     i2c_set_clientdata(i2c, master);
0226 
0227     ret = devm_spi_register_master(&i2c->dev, master);
0228     if (ret < 0)
0229         spi_master_put(master);
0230 
0231     return ret;
0232 }
0233 
0234 static const struct i2c_device_id spi_xcomm_ids[] = {
0235     { "spi-xcomm" },
0236     { },
0237 };
0238 MODULE_DEVICE_TABLE(i2c, spi_xcomm_ids);
0239 
0240 static struct i2c_driver spi_xcomm_driver = {
0241     .driver = {
0242         .name   = "spi-xcomm",
0243     },
0244     .id_table   = spi_xcomm_ids,
0245     .probe      = spi_xcomm_probe,
0246 };
0247 module_i2c_driver(spi_xcomm_driver);
0248 
0249 MODULE_LICENSE("GPL");
0250 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
0251 MODULE_DESCRIPTION("Analog Devices AD-FMCOMMS1-EBZ board I2C-SPI bridge driver");