xref: /openbmc/linux/drivers/mfd/janz-cmodio.c (revision f79e4d5f)
1 /*
2  * Janz CMOD-IO MODULbus Carrier Board PCI Driver
3  *
4  * Copyright (c) 2010 Ira W. Snyder <iws@ovro.caltech.edu>
5  *
6  * Lots of inspiration and code was copied from drivers/mfd/sm501.c
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2 of the License, or (at your
11  * option) any later version.
12  */
13 
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/pci.h>
17 #include <linux/interrupt.h>
18 #include <linux/delay.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21 #include <linux/mfd/core.h>
22 
23 #include <linux/mfd/janz.h>
24 
25 #define DRV_NAME "janz-cmodio"
26 
27 /* Size of each MODULbus module in PCI BAR4 */
28 #define CMODIO_MODULBUS_SIZE	0x200
29 
30 /* Maximum number of MODULbus modules on a CMOD-IO carrier board */
31 #define CMODIO_MAX_MODULES	4
32 
33 /* Module Parameters */
34 static unsigned int num_modules = CMODIO_MAX_MODULES;
35 static char *modules[CMODIO_MAX_MODULES] = {
36 	"empty", "empty", "empty", "empty",
37 };
38 
39 module_param_array(modules, charp, &num_modules, S_IRUGO);
40 MODULE_PARM_DESC(modules, "MODULbus modules attached to the carrier board");
41 
42 /* Unique Device Id */
43 static unsigned int cmodio_id;
44 
45 struct cmodio_device {
46 	/* Parent PCI device */
47 	struct pci_dev *pdev;
48 
49 	/* PLX control registers */
50 	struct janz_cmodio_onboard_regs __iomem *ctrl;
51 
52 	/* hex switch position */
53 	u8 hex;
54 
55 	/* mfd-core API */
56 	struct mfd_cell cells[CMODIO_MAX_MODULES];
57 	struct resource resources[3 * CMODIO_MAX_MODULES];
58 	struct janz_platform_data pdata[CMODIO_MAX_MODULES];
59 };
60 
61 /*
62  * Subdevices using the mfd-core API
63  */
64 
65 static int cmodio_setup_subdevice(struct cmodio_device *priv,
66 					    char *name, unsigned int devno,
67 					    unsigned int modno)
68 {
69 	struct janz_platform_data *pdata;
70 	struct mfd_cell *cell;
71 	struct resource *res;
72 	struct pci_dev *pci;
73 
74 	pci = priv->pdev;
75 	cell = &priv->cells[devno];
76 	res = &priv->resources[devno * 3];
77 	pdata = &priv->pdata[devno];
78 
79 	cell->name = name;
80 	cell->resources = res;
81 	cell->num_resources = 3;
82 
83 	/* Setup the subdevice ID -- must be unique */
84 	cell->id = cmodio_id++;
85 
86 	/* Add platform data */
87 	pdata->modno = modno;
88 	cell->platform_data = pdata;
89 	cell->pdata_size = sizeof(*pdata);
90 
91 	/* MODULbus registers -- PCI BAR3 is big-endian MODULbus access */
92 	res->flags = IORESOURCE_MEM;
93 	res->parent = &pci->resource[3];
94 	res->start = pci->resource[3].start + (CMODIO_MODULBUS_SIZE * modno);
95 	res->end = res->start + CMODIO_MODULBUS_SIZE - 1;
96 	res++;
97 
98 	/* PLX Control Registers -- PCI BAR4 is interrupt and other registers */
99 	res->flags = IORESOURCE_MEM;
100 	res->parent = &pci->resource[4];
101 	res->start = pci->resource[4].start;
102 	res->end = pci->resource[4].end;
103 	res++;
104 
105 	/*
106 	 * IRQ
107 	 *
108 	 * The start and end fields are used as an offset to the irq_base
109 	 * parameter passed into the mfd_add_devices() function call. All
110 	 * devices share the same IRQ.
111 	 */
112 	res->flags = IORESOURCE_IRQ;
113 	res->parent = NULL;
114 	res->start = 0;
115 	res->end = 0;
116 	res++;
117 
118 	return 0;
119 }
120 
121 /* Probe each submodule using kernel parameters */
122 static int cmodio_probe_submodules(struct cmodio_device *priv)
123 {
124 	struct pci_dev *pdev = priv->pdev;
125 	unsigned int num_probed = 0;
126 	char *name;
127 	int i;
128 
129 	for (i = 0; i < num_modules; i++) {
130 		name = modules[i];
131 		if (!strcmp(name, "") || !strcmp(name, "empty"))
132 			continue;
133 
134 		dev_dbg(&priv->pdev->dev, "MODULbus %d: name %s\n", i, name);
135 		cmodio_setup_subdevice(priv, name, num_probed, i);
136 		num_probed++;
137 	}
138 
139 	/* print an error message if no modules were probed */
140 	if (num_probed == 0) {
141 		dev_err(&priv->pdev->dev, "no MODULbus modules specified, "
142 					  "please set the ``modules'' kernel "
143 					  "parameter according to your "
144 					  "hardware configuration\n");
145 		return -ENODEV;
146 	}
147 
148 	return mfd_add_devices(&pdev->dev, 0, priv->cells,
149 			       num_probed, NULL, pdev->irq, NULL);
150 }
151 
152 /*
153  * SYSFS Attributes
154  */
155 
156 static ssize_t mbus_show(struct device *dev, struct device_attribute *attr,
157 			 char *buf)
158 {
159 	struct cmodio_device *priv = dev_get_drvdata(dev);
160 
161 	return snprintf(buf, PAGE_SIZE, "%x\n", priv->hex);
162 }
163 
164 static DEVICE_ATTR(modulbus_number, S_IRUGO, mbus_show, NULL);
165 
166 static struct attribute *cmodio_sysfs_attrs[] = {
167 	&dev_attr_modulbus_number.attr,
168 	NULL,
169 };
170 
171 static const struct attribute_group cmodio_sysfs_attr_group = {
172 	.attrs = cmodio_sysfs_attrs,
173 };
174 
175 /*
176  * PCI Driver
177  */
178 
179 static int cmodio_pci_probe(struct pci_dev *dev,
180 				      const struct pci_device_id *id)
181 {
182 	struct cmodio_device *priv;
183 	int ret;
184 
185 	priv = devm_kzalloc(&dev->dev, sizeof(*priv), GFP_KERNEL);
186 	if (!priv)
187 		return -ENOMEM;
188 
189 	pci_set_drvdata(dev, priv);
190 	priv->pdev = dev;
191 
192 	/* Hardware Initialization */
193 	ret = pci_enable_device(dev);
194 	if (ret) {
195 		dev_err(&dev->dev, "unable to enable device\n");
196 		return ret;
197 	}
198 
199 	pci_set_master(dev);
200 	ret = pci_request_regions(dev, DRV_NAME);
201 	if (ret) {
202 		dev_err(&dev->dev, "unable to request regions\n");
203 		goto out_pci_disable_device;
204 	}
205 
206 	/* Onboard configuration registers */
207 	priv->ctrl = pci_ioremap_bar(dev, 4);
208 	if (!priv->ctrl) {
209 		dev_err(&dev->dev, "unable to remap onboard regs\n");
210 		ret = -ENOMEM;
211 		goto out_pci_release_regions;
212 	}
213 
214 	/* Read the hex switch on the carrier board */
215 	priv->hex = ioread8(&priv->ctrl->int_enable);
216 
217 	/* Add the MODULbus number (hex switch value) to the device's sysfs */
218 	ret = sysfs_create_group(&dev->dev.kobj, &cmodio_sysfs_attr_group);
219 	if (ret) {
220 		dev_err(&dev->dev, "unable to create sysfs attributes\n");
221 		goto out_unmap_ctrl;
222 	}
223 
224 	/*
225 	 * Disable all interrupt lines, each submodule will enable its
226 	 * own interrupt line if needed
227 	 */
228 	iowrite8(0xf, &priv->ctrl->int_disable);
229 
230 	/* Register drivers for all submodules */
231 	ret = cmodio_probe_submodules(priv);
232 	if (ret) {
233 		dev_err(&dev->dev, "unable to probe submodules\n");
234 		goto out_sysfs_remove_group;
235 	}
236 
237 	return 0;
238 
239 out_sysfs_remove_group:
240 	sysfs_remove_group(&dev->dev.kobj, &cmodio_sysfs_attr_group);
241 out_unmap_ctrl:
242 	iounmap(priv->ctrl);
243 out_pci_release_regions:
244 	pci_release_regions(dev);
245 out_pci_disable_device:
246 	pci_disable_device(dev);
247 
248 	return ret;
249 }
250 
251 static void cmodio_pci_remove(struct pci_dev *dev)
252 {
253 	struct cmodio_device *priv = pci_get_drvdata(dev);
254 
255 	mfd_remove_devices(&dev->dev);
256 	sysfs_remove_group(&dev->dev.kobj, &cmodio_sysfs_attr_group);
257 	iounmap(priv->ctrl);
258 	pci_release_regions(dev);
259 	pci_disable_device(dev);
260 }
261 
262 #define PCI_VENDOR_ID_JANZ		0x13c3
263 
264 /* The list of devices that this module will support */
265 static const struct pci_device_id cmodio_pci_ids[] = {
266 	{ PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030, PCI_VENDOR_ID_JANZ, 0x0101 },
267 	{ PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050, PCI_VENDOR_ID_JANZ, 0x0100 },
268 	{ PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030, PCI_VENDOR_ID_JANZ, 0x0201 },
269 	{ PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030, PCI_VENDOR_ID_JANZ, 0x0202 },
270 	{ PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050, PCI_VENDOR_ID_JANZ, 0x0201 },
271 	{ PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050, PCI_VENDOR_ID_JANZ, 0x0202 },
272 	{ 0, }
273 };
274 MODULE_DEVICE_TABLE(pci, cmodio_pci_ids);
275 
276 static struct pci_driver cmodio_pci_driver = {
277 	.name     = DRV_NAME,
278 	.id_table = cmodio_pci_ids,
279 	.probe    = cmodio_pci_probe,
280 	.remove   = cmodio_pci_remove,
281 };
282 
283 module_pci_driver(cmodio_pci_driver);
284 
285 MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
286 MODULE_DESCRIPTION("Janz CMOD-IO PCI MODULbus Carrier Board Driver");
287 MODULE_LICENSE("GPL");
288