xref: /linux/drivers/tty/serial/sc16is7xx_spi.c (revision 90d32e92011eaae8e70a9169b4e7acf4ca8f9d3a)
1 // SPDX-License-Identifier: GPL-2.0+
2 /* SC16IS7xx SPI interface driver */
3 
4 #include <linux/dev_printk.h>
5 #include <linux/mod_devicetable.h>
6 #include <linux/module.h>
7 #include <linux/regmap.h>
8 #include <linux/spi/spi.h>
9 #include <linux/string.h>
10 #include <linux/units.h>
11 
12 #include "sc16is7xx.h"
13 
14 /* SPI definitions */
15 #define SC16IS7XX_SPI_READ_BIT	BIT(7)
16 
17 static int sc16is7xx_spi_probe(struct spi_device *spi)
18 {
19 	const struct sc16is7xx_devtype *devtype;
20 	struct regmap *regmaps[SC16IS7XX_MAX_PORTS];
21 	struct regmap_config regcfg;
22 	unsigned int i;
23 	int ret;
24 
25 	/* Setup SPI bus */
26 	spi->bits_per_word	= 8;
27 	/* For all variants, only mode 0 is supported */
28 	if ((spi->mode & SPI_MODE_X_MASK) != SPI_MODE_0)
29 		return dev_err_probe(&spi->dev, -EINVAL, "Unsupported SPI mode\n");
30 
31 	spi->mode		= spi->mode ? : SPI_MODE_0;
32 	spi->max_speed_hz	= spi->max_speed_hz ? : 4 * HZ_PER_MHZ;
33 	ret = spi_setup(spi);
34 	if (ret)
35 		return ret;
36 
37 	devtype = spi_get_device_match_data(spi);
38 	if (!devtype)
39 		return dev_err_probe(&spi->dev, -ENODEV, "Failed to match device\n");
40 
41 	memcpy(&regcfg, &sc16is7xx_regcfg, sizeof(struct regmap_config));
42 
43 	for (i = 0; i < devtype->nr_uart; i++) {
44 		regcfg.name = sc16is7xx_regmap_name(i);
45 		/*
46 		 * If read_flag_mask is 0, the regmap code sets it to a default
47 		 * of 0x80. Since we specify our own mask, we must add the READ
48 		 * bit ourselves:
49 		 */
50 		regcfg.read_flag_mask = sc16is7xx_regmap_port_mask(i) |
51 			SC16IS7XX_SPI_READ_BIT;
52 		regcfg.write_flag_mask = sc16is7xx_regmap_port_mask(i);
53 		regmaps[i] = devm_regmap_init_spi(spi, &regcfg);
54 	}
55 
56 	return sc16is7xx_probe(&spi->dev, devtype, regmaps, spi->irq);
57 }
58 
59 static void sc16is7xx_spi_remove(struct spi_device *spi)
60 {
61 	sc16is7xx_remove(&spi->dev);
62 }
63 
64 static const struct spi_device_id sc16is7xx_spi_id_table[] = {
65 	{ "sc16is74x",	(kernel_ulong_t)&sc16is74x_devtype, },
66 	{ "sc16is740",	(kernel_ulong_t)&sc16is74x_devtype, },
67 	{ "sc16is741",	(kernel_ulong_t)&sc16is74x_devtype, },
68 	{ "sc16is750",	(kernel_ulong_t)&sc16is750_devtype, },
69 	{ "sc16is752",	(kernel_ulong_t)&sc16is752_devtype, },
70 	{ "sc16is760",	(kernel_ulong_t)&sc16is760_devtype, },
71 	{ "sc16is762",	(kernel_ulong_t)&sc16is762_devtype, },
72 	{ }
73 };
74 MODULE_DEVICE_TABLE(spi, sc16is7xx_spi_id_table);
75 
76 static struct spi_driver sc16is7xx_spi_driver = {
77 	.driver = {
78 		.name		= SC16IS7XX_NAME,
79 		.of_match_table	= sc16is7xx_dt_ids,
80 	},
81 	.probe		= sc16is7xx_spi_probe,
82 	.remove		= sc16is7xx_spi_remove,
83 	.id_table	= sc16is7xx_spi_id_table,
84 };
85 
86 module_spi_driver(sc16is7xx_spi_driver);
87 
88 MODULE_LICENSE("GPL");
89 MODULE_DESCRIPTION("SC16IS7xx SPI interface driver");
90 MODULE_IMPORT_NS(SERIAL_NXP_SC16IS7XX);
91