Debian Codegraph @

drivers/spi/spi-altera-platform.c

raw
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Altera SPI driver
4 *
5 * Copyright (C) 2008 Thomas Chou <[email protected]>
6 *
7 * Based on spi_s3c24xx.c, which is:
8 * Copyright (c) 2006 Ben Dooks
9 * Copyright (c) 2006 Simtec Electronics
10 * Ben Dooks <[email protected]>
11 */
12
13 #include <linux/interrupt.h>
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/spi/altera.h>
18 #include <linux/spi/spi.h>
19 #include <linux/io.h>
20 #include <linux/of.h>
21
22 #define DRV_NAME "spi_altera"
23
24 enum altera_spi_type {
25 ALTERA_SPI_TYPE_UNKNOWN,
26 ALTERA_SPI_TYPE_SUBDEV,
27 };
28
29 static const struct regmap_config spi_altera_config = {
30 .reg_bits = 32,
31 .reg_stride = 4,
32 .val_bits = 32,
33 };
34
35 static int altera_spi_probe(struct platform_device *pdev)
36 {
37 const struct platform_device_id *platid = platform_get_device_id(pdev);
38 struct altera_spi_platform_data *pdata = dev_get_platdata(&pdev->dev);
39 enum altera_spi_type type = ALTERA_SPI_TYPE_UNKNOWN;
40 struct altera_spi *hw;
41 struct spi_controller *host;
42 int err = -ENODEV;
43 u16 i;
44
45 host = spi_alloc_host(&pdev->dev, sizeof(struct altera_spi));
46 if (!host)
47 return err;
48
49 /* setup the host state. */
50 host->bus_num = -1;
51
52 if (pdata) {
53 if (pdata->num_chipselect > ALTERA_SPI_MAX_CS) {
54 dev_err(&pdev->dev,
55 "Invalid number of chipselect: %u\n",
56 pdata->num_chipselect);
57 err = -EINVAL;
58 goto exit;
59 }
60
61 host->num_chipselect = pdata->num_chipselect;
62 host->mode_bits = pdata->mode_bits;
63 host->bits_per_word_mask = pdata->bits_per_word_mask;
64 } else {
65 host->num_chipselect = 16;
66 host->mode_bits = SPI_CS_HIGH;
67 host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 16);
68 }
69
70 hw = spi_controller_get_devdata(host);
71 hw->dev = &pdev->dev;
72
73 if (platid)
74 type = platid->driver_data;
75
76 /* find and map our resources */
77 if (type == ALTERA_SPI_TYPE_SUBDEV) {
78 struct resource *regoff;
79
80 hw->regmap = dev_get_regmap(pdev->dev.parent, NULL);
81 if (!hw->regmap) {
82 dev_err(&pdev->dev, "get regmap failed\n");
83 goto exit;
84 }
85
86 regoff = platform_get_resource(pdev, IORESOURCE_REG, 0);
87 if (regoff)
88 hw->regoff = regoff->start;
89 } else {
90 void __iomem *res;
91
92 res = devm_platform_ioremap_resource(pdev, 0);
93 if (IS_ERR(res)) {
94 err = PTR_ERR(res);
95 goto exit;
96 }
97
98 hw->regmap = devm_regmap_init_mmio(&pdev->dev, res,
99 &spi_altera_config);
100 if (IS_ERR(hw->regmap)) {
101 dev_err(&pdev->dev, "regmap mmio init failed\n");
102 err = PTR_ERR(hw->regmap);
103 goto exit;
104 }
105 }
106
107 altera_spi_init_host(host);
108
109 /* irq is optional */
110 hw->irq = platform_get_irq(pdev, 0);
111 if (hw->irq >= 0) {
112 err = devm_request_irq(&pdev->dev, hw->irq, altera_spi_irq, 0,
113 pdev->name, host);
114 if (err)
115 goto exit;
116 }
117
118 err = devm_spi_register_controller(&pdev->dev, host);
119 if (err)
120 goto exit;
121
122 if (pdata) {
123 for (i = 0; i < pdata->num_devices; i++) {
124 if (!spi_new_device(host, pdata->devices + i))
125 dev_warn(&pdev->dev,
126 "unable to create SPI device: %s\n",
127 pdata->devices[i].modalias);
128 }
129 }
130
131 dev_info(&pdev->dev, "regoff %u, irq %d\n", hw->regoff, hw->irq);
132
133 return 0;
134 exit:
135 spi_controller_put(host);
136 return err;
137 }
138
139 #ifdef CONFIG_OF
140 static const struct of_device_id altera_spi_match[] = {
141 { .compatible = "ALTR,spi-1.0", },
142 { .compatible = "altr,spi-1.0", },
143 {},
144 };
145 MODULE_DEVICE_TABLE(of, altera_spi_match);
146 #endif /* CONFIG_OF */
147
148 static const struct platform_device_id altera_spi_ids[] = {
149 { DRV_NAME, ALTERA_SPI_TYPE_UNKNOWN },
150 { "subdev_spi_altera", ALTERA_SPI_TYPE_SUBDEV },
151 { }
152 };
153 MODULE_DEVICE_TABLE(platform, altera_spi_ids);
154
155 static struct platform_driver altera_spi_driver = {
156 .probe = altera_spi_probe,
157 .driver = {
158 .name = DRV_NAME,
159 .pm = NULL,
160 .of_match_table = of_match_ptr(altera_spi_match),
161 },
162 .id_table = altera_spi_ids,
163 };
164 module_platform_driver(altera_spi_driver);
165
166 MODULE_DESCRIPTION("Altera SPI driver");
167 MODULE_AUTHOR("Thomas Chou <[email protected]>");
168 MODULE_LICENSE("GPL");