0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/kernel.h>
0009 #include <linux/err.h>
0010 #include <linux/module.h>
0011 #include <linux/spi/spi.h>
0012 #include <linux/i2c.h>
0013 #include <linux/delay.h>
0014 #include <linux/pm_runtime.h>
0015 #include <linux/of_device.h>
0016 #include <linux/of.h>
0017 #include <linux/platform_data/sc18is602.h>
0018 #include <linux/gpio/consumer.h>
0019
0020 enum chips { sc18is602, sc18is602b, sc18is603 };
0021
0022 #define SC18IS602_BUFSIZ 200
0023 #define SC18IS602_CLOCK 7372000
0024
0025 #define SC18IS602_MODE_CPHA BIT(2)
0026 #define SC18IS602_MODE_CPOL BIT(3)
0027 #define SC18IS602_MODE_LSB_FIRST BIT(5)
0028 #define SC18IS602_MODE_CLOCK_DIV_4 0x0
0029 #define SC18IS602_MODE_CLOCK_DIV_16 0x1
0030 #define SC18IS602_MODE_CLOCK_DIV_64 0x2
0031 #define SC18IS602_MODE_CLOCK_DIV_128 0x3
0032
0033 struct sc18is602 {
0034 struct spi_master *master;
0035 struct device *dev;
0036 u8 ctrl;
0037 u32 freq;
0038 u32 speed;
0039
0040
0041 struct i2c_client *client;
0042 enum chips id;
0043 u8 buffer[SC18IS602_BUFSIZ + 1];
0044 int tlen;
0045 int rindex;
0046
0047 struct gpio_desc *reset;
0048 };
0049
0050 static int sc18is602_wait_ready(struct sc18is602 *hw, int len)
0051 {
0052 int i, err;
0053 int usecs = 1000000 * len / hw->speed + 1;
0054 u8 dummy[1];
0055
0056 for (i = 0; i < 10; i++) {
0057 err = i2c_master_recv(hw->client, dummy, 1);
0058 if (err >= 0)
0059 return 0;
0060 usleep_range(usecs, usecs * 2);
0061 }
0062 return -ETIMEDOUT;
0063 }
0064
0065 static int sc18is602_txrx(struct sc18is602 *hw, struct spi_message *msg,
0066 struct spi_transfer *t, bool do_transfer)
0067 {
0068 unsigned int len = t->len;
0069 int ret;
0070
0071 if (hw->tlen == 0) {
0072
0073 hw->buffer[0] = 1 << msg->spi->chip_select;
0074 hw->tlen = 1;
0075 hw->rindex = 0;
0076 }
0077
0078
0079
0080
0081
0082 if (t->tx_buf) {
0083 memcpy(&hw->buffer[hw->tlen], t->tx_buf, len);
0084 hw->tlen += len;
0085 if (t->rx_buf)
0086 do_transfer = true;
0087 else
0088 hw->rindex = hw->tlen - 1;
0089 } else if (t->rx_buf) {
0090
0091
0092
0093
0094
0095
0096 hw->rindex = hw->tlen - 1;
0097 memset(&hw->buffer[hw->tlen], 0, len);
0098 hw->tlen += len;
0099 do_transfer = true;
0100 }
0101
0102 if (do_transfer && hw->tlen > 1) {
0103 ret = sc18is602_wait_ready(hw, SC18IS602_BUFSIZ);
0104 if (ret < 0)
0105 return ret;
0106 ret = i2c_master_send(hw->client, hw->buffer, hw->tlen);
0107 if (ret < 0)
0108 return ret;
0109 if (ret != hw->tlen)
0110 return -EIO;
0111
0112 if (t->rx_buf) {
0113 int rlen = hw->rindex + len;
0114
0115 ret = sc18is602_wait_ready(hw, hw->tlen);
0116 if (ret < 0)
0117 return ret;
0118 ret = i2c_master_recv(hw->client, hw->buffer, rlen);
0119 if (ret < 0)
0120 return ret;
0121 if (ret != rlen)
0122 return -EIO;
0123 memcpy(t->rx_buf, &hw->buffer[hw->rindex], len);
0124 }
0125 hw->tlen = 0;
0126 }
0127 return len;
0128 }
0129
0130 static int sc18is602_setup_transfer(struct sc18is602 *hw, u32 hz, u8 mode)
0131 {
0132 u8 ctrl = 0;
0133 int ret;
0134
0135 if (mode & SPI_CPHA)
0136 ctrl |= SC18IS602_MODE_CPHA;
0137 if (mode & SPI_CPOL)
0138 ctrl |= SC18IS602_MODE_CPOL;
0139 if (mode & SPI_LSB_FIRST)
0140 ctrl |= SC18IS602_MODE_LSB_FIRST;
0141
0142
0143 if (hz >= hw->freq / 4) {
0144 ctrl |= SC18IS602_MODE_CLOCK_DIV_4;
0145 hw->speed = hw->freq / 4;
0146 } else if (hz >= hw->freq / 16) {
0147 ctrl |= SC18IS602_MODE_CLOCK_DIV_16;
0148 hw->speed = hw->freq / 16;
0149 } else if (hz >= hw->freq / 64) {
0150 ctrl |= SC18IS602_MODE_CLOCK_DIV_64;
0151 hw->speed = hw->freq / 64;
0152 } else {
0153 ctrl |= SC18IS602_MODE_CLOCK_DIV_128;
0154 hw->speed = hw->freq / 128;
0155 }
0156
0157
0158
0159
0160
0161
0162 if (ctrl == hw->ctrl)
0163 return 0;
0164
0165 ret = i2c_smbus_write_byte_data(hw->client, 0xf0, ctrl);
0166 if (ret < 0)
0167 return ret;
0168
0169 hw->ctrl = ctrl;
0170
0171 return 0;
0172 }
0173
0174 static int sc18is602_check_transfer(struct spi_device *spi,
0175 struct spi_transfer *t, int tlen)
0176 {
0177 if (t && t->len + tlen > SC18IS602_BUFSIZ + 1)
0178 return -EINVAL;
0179
0180 return 0;
0181 }
0182
0183 static int sc18is602_transfer_one(struct spi_master *master,
0184 struct spi_message *m)
0185 {
0186 struct sc18is602 *hw = spi_master_get_devdata(master);
0187 struct spi_device *spi = m->spi;
0188 struct spi_transfer *t;
0189 int status = 0;
0190
0191 hw->tlen = 0;
0192 list_for_each_entry(t, &m->transfers, transfer_list) {
0193 bool do_transfer;
0194
0195 status = sc18is602_check_transfer(spi, t, hw->tlen);
0196 if (status < 0)
0197 break;
0198
0199 status = sc18is602_setup_transfer(hw, t->speed_hz, spi->mode);
0200 if (status < 0)
0201 break;
0202
0203 do_transfer = t->cs_change || list_is_last(&t->transfer_list,
0204 &m->transfers);
0205
0206 if (t->len) {
0207 status = sc18is602_txrx(hw, m, t, do_transfer);
0208 if (status < 0)
0209 break;
0210 m->actual_length += status;
0211 }
0212 status = 0;
0213
0214 spi_transfer_delay_exec(t);
0215 }
0216 m->status = status;
0217 spi_finalize_current_message(master);
0218
0219 return status;
0220 }
0221
0222 static size_t sc18is602_max_transfer_size(struct spi_device *spi)
0223 {
0224 return SC18IS602_BUFSIZ;
0225 }
0226
0227 static int sc18is602_setup(struct spi_device *spi)
0228 {
0229 struct sc18is602 *hw = spi_master_get_devdata(spi->master);
0230
0231
0232 if (hw->id == sc18is602 && spi->chip_select == 2)
0233 return -ENXIO;
0234
0235 return 0;
0236 }
0237
0238 static int sc18is602_probe(struct i2c_client *client,
0239 const struct i2c_device_id *id)
0240 {
0241 struct device *dev = &client->dev;
0242 struct device_node *np = dev->of_node;
0243 struct sc18is602_platform_data *pdata = dev_get_platdata(dev);
0244 struct sc18is602 *hw;
0245 struct spi_master *master;
0246
0247 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
0248 I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
0249 return -EINVAL;
0250
0251 master = devm_spi_alloc_master(dev, sizeof(struct sc18is602));
0252 if (!master)
0253 return -ENOMEM;
0254
0255 hw = spi_master_get_devdata(master);
0256 i2c_set_clientdata(client, hw);
0257
0258
0259 hw->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
0260 if (IS_ERR(hw->reset))
0261 return PTR_ERR(hw->reset);
0262 gpiod_set_value_cansleep(hw->reset, 0);
0263
0264 hw->master = master;
0265 hw->client = client;
0266 hw->dev = dev;
0267 hw->ctrl = 0xff;
0268
0269 if (client->dev.of_node)
0270 hw->id = (enum chips)of_device_get_match_data(&client->dev);
0271 else
0272 hw->id = id->driver_data;
0273
0274 switch (hw->id) {
0275 case sc18is602:
0276 case sc18is602b:
0277 master->num_chipselect = 4;
0278 hw->freq = SC18IS602_CLOCK;
0279 break;
0280 case sc18is603:
0281 master->num_chipselect = 2;
0282 if (pdata) {
0283 hw->freq = pdata->clock_frequency;
0284 } else {
0285 const __be32 *val;
0286 int len;
0287
0288 val = of_get_property(np, "clock-frequency", &len);
0289 if (val && len >= sizeof(__be32))
0290 hw->freq = be32_to_cpup(val);
0291 }
0292 if (!hw->freq)
0293 hw->freq = SC18IS602_CLOCK;
0294 break;
0295 }
0296 master->bus_num = np ? -1 : client->adapter->nr;
0297 master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_LSB_FIRST;
0298 master->bits_per_word_mask = SPI_BPW_MASK(8);
0299 master->setup = sc18is602_setup;
0300 master->transfer_one_message = sc18is602_transfer_one;
0301 master->max_transfer_size = sc18is602_max_transfer_size;
0302 master->max_message_size = sc18is602_max_transfer_size;
0303 master->dev.of_node = np;
0304 master->min_speed_hz = hw->freq / 128;
0305 master->max_speed_hz = hw->freq / 4;
0306
0307 return devm_spi_register_master(dev, master);
0308 }
0309
0310 static const struct i2c_device_id sc18is602_id[] = {
0311 { "sc18is602", sc18is602 },
0312 { "sc18is602b", sc18is602b },
0313 { "sc18is603", sc18is603 },
0314 { }
0315 };
0316 MODULE_DEVICE_TABLE(i2c, sc18is602_id);
0317
0318 static const struct of_device_id sc18is602_of_match[] = {
0319 {
0320 .compatible = "nxp,sc18is602",
0321 .data = (void *)sc18is602
0322 },
0323 {
0324 .compatible = "nxp,sc18is602b",
0325 .data = (void *)sc18is602b
0326 },
0327 {
0328 .compatible = "nxp,sc18is603",
0329 .data = (void *)sc18is603
0330 },
0331 { },
0332 };
0333 MODULE_DEVICE_TABLE(of, sc18is602_of_match);
0334
0335 static struct i2c_driver sc18is602_driver = {
0336 .driver = {
0337 .name = "sc18is602",
0338 .of_match_table = of_match_ptr(sc18is602_of_match),
0339 },
0340 .probe = sc18is602_probe,
0341 .id_table = sc18is602_id,
0342 };
0343
0344 module_i2c_driver(sc18is602_driver);
0345
0346 MODULE_DESCRIPTION("SC18IS602/603 SPI Master Driver");
0347 MODULE_AUTHOR("Guenter Roeck");
0348 MODULE_LICENSE("GPL");