2 * Janz CMOD-IO MODULbus Carrier Board PCI Driver
4 * Copyright (c) 2010 Ira W. Snyder <iws@ovro.caltech.edu>
6 * Lots of inspiration and code was copied from drivers/mfd/sm501.c
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.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/pci.h>
18 #include <linux/interrupt.h>
19 #include <linux/delay.h>
20 #include <linux/platform_device.h>
21 #include <linux/mfd/core.h>
23 #include <linux/mfd/janz.h>
25 #define DRV_NAME "janz-cmodio"
27 /* Size of each MODULbus module in PCI BAR4 */
28 #define CMODIO_MODULBUS_SIZE 0x200
30 /* Maximum number of MODULbus modules on a CMOD-IO carrier board */
31 #define CMODIO_MAX_MODULES 4
33 /* Module Parameters */
34 static unsigned int num_modules
= CMODIO_MAX_MODULES
;
35 static unsigned char *modules
[CMODIO_MAX_MODULES
] = {
36 "empty", "empty", "empty", "empty",
39 module_param_array(modules
, charp
, &num_modules
, S_IRUGO
);
40 MODULE_PARM_DESC(modules
, "MODULbus modules attached to the carrier board");
42 /* Unique Device Id */
43 static unsigned int cmodio_id
;
45 struct cmodio_device
{
46 /* Parent PCI device */
49 /* PLX control registers */
50 struct janz_cmodio_onboard_regs __iomem
*ctrl
;
52 /* hex switch position */
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
];
62 * Subdevices using the mfd-core API
65 static int __devinit
cmodio_setup_subdevice(struct cmodio_device
*priv
,
66 char *name
, unsigned int devno
,
69 struct janz_platform_data
*pdata
;
70 struct mfd_cell
*cell
;
75 cell
= &priv
->cells
[devno
];
76 res
= &priv
->resources
[devno
* 3];
77 pdata
= &priv
->pdata
[devno
];
80 cell
->resources
= res
;
81 cell
->num_resources
= 3;
83 /* Setup the subdevice ID -- must be unique */
84 cell
->id
= cmodio_id
++;
86 /* Add platform data */
88 cell
->platform_data
= pdata
;
89 cell
->data_size
= sizeof(*pdata
);
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;
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
;
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.
112 res
->flags
= IORESOURCE_IRQ
;
121 /* Probe each submodule using kernel parameters */
122 static int __devinit
cmodio_probe_submodules(struct cmodio_device
*priv
)
124 struct pci_dev
*pdev
= priv
->pdev
;
125 unsigned int num_probed
= 0;
129 for (i
= 0; i
< num_modules
; i
++) {
131 if (!strcmp(name
, "") || !strcmp(name
, "empty"))
134 dev_dbg(&priv
->pdev
->dev
, "MODULbus %d: name %s\n", i
, name
);
135 cmodio_setup_subdevice(priv
, name
, num_probed
, i
);
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");
148 return mfd_add_devices(&pdev
->dev
, 0, priv
->cells
,
149 num_probed
, NULL
, pdev
->irq
);
156 static ssize_t
mbus_show(struct device
*dev
, struct device_attribute
*attr
,
159 struct cmodio_device
*priv
= dev_get_drvdata(dev
);
161 return snprintf(buf
, PAGE_SIZE
, "%x\n", priv
->hex
);
164 static DEVICE_ATTR(modulbus_number
, S_IRUGO
, mbus_show
, NULL
);
166 static struct attribute
*cmodio_sysfs_attrs
[] = {
167 &dev_attr_modulbus_number
.attr
,
171 static const struct attribute_group cmodio_sysfs_attr_group
= {
172 .attrs
= cmodio_sysfs_attrs
,
179 static int __devinit
cmodio_pci_probe(struct pci_dev
*dev
,
180 const struct pci_device_id
*id
)
182 struct cmodio_device
*priv
;
185 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
187 dev_err(&dev
->dev
, "unable to allocate private data\n");
192 pci_set_drvdata(dev
, priv
);
195 /* Hardware Initialization */
196 ret
= pci_enable_device(dev
);
198 dev_err(&dev
->dev
, "unable to enable device\n");
203 ret
= pci_request_regions(dev
, DRV_NAME
);
205 dev_err(&dev
->dev
, "unable to request regions\n");
206 goto out_pci_disable_device
;
209 /* Onboard configuration registers */
210 priv
->ctrl
= pci_ioremap_bar(dev
, 4);
212 dev_err(&dev
->dev
, "unable to remap onboard regs\n");
214 goto out_pci_release_regions
;
217 /* Read the hex switch on the carrier board */
218 priv
->hex
= ioread8(&priv
->ctrl
->int_enable
);
220 /* Add the MODULbus number (hex switch value) to the device's sysfs */
221 ret
= sysfs_create_group(&dev
->dev
.kobj
, &cmodio_sysfs_attr_group
);
223 dev_err(&dev
->dev
, "unable to create sysfs attributes\n");
228 * Disable all interrupt lines, each submodule will enable its
229 * own interrupt line if needed
231 iowrite8(0xf, &priv
->ctrl
->int_disable
);
233 /* Register drivers for all submodules */
234 ret
= cmodio_probe_submodules(priv
);
236 dev_err(&dev
->dev
, "unable to probe submodules\n");
237 goto out_sysfs_remove_group
;
242 out_sysfs_remove_group
:
243 sysfs_remove_group(&dev
->dev
.kobj
, &cmodio_sysfs_attr_group
);
246 out_pci_release_regions
:
247 pci_release_regions(dev
);
248 out_pci_disable_device
:
249 pci_disable_device(dev
);
256 static void __devexit
cmodio_pci_remove(struct pci_dev
*dev
)
258 struct cmodio_device
*priv
= pci_get_drvdata(dev
);
260 mfd_remove_devices(&dev
->dev
);
261 sysfs_remove_group(&dev
->dev
.kobj
, &cmodio_sysfs_attr_group
);
263 pci_release_regions(dev
);
264 pci_disable_device(dev
);
268 #define PCI_VENDOR_ID_JANZ 0x13c3
270 /* The list of devices that this module will support */
271 static DEFINE_PCI_DEVICE_TABLE(cmodio_pci_ids
) = {
272 { PCI_VENDOR_ID_PLX
, PCI_DEVICE_ID_PLX_9030
, PCI_VENDOR_ID_JANZ
, 0x0101 },
273 { PCI_VENDOR_ID_PLX
, PCI_DEVICE_ID_PLX_9050
, PCI_VENDOR_ID_JANZ
, 0x0100 },
276 MODULE_DEVICE_TABLE(pci
, cmodio_pci_ids
);
278 static struct pci_driver cmodio_pci_driver
= {
280 .id_table
= cmodio_pci_ids
,
281 .probe
= cmodio_pci_probe
,
282 .remove
= __devexit_p(cmodio_pci_remove
),
289 static int __init
cmodio_init(void)
291 return pci_register_driver(&cmodio_pci_driver
);
294 static void __exit
cmodio_exit(void)
296 pci_unregister_driver(&cmodio_pci_driver
);
299 MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
300 MODULE_DESCRIPTION("Janz CMOD-IO PCI MODULbus Carrier Board Driver");
301 MODULE_LICENSE("GPL");
303 module_init(cmodio_init
);
304 module_exit(cmodio_exit
);