Merge branch 'akpm'
[linux-2.6/next.git] / drivers / mfd / janz-cmodio.c
blob5c2a06acb77fce5d226c183e2197602eff8f434d
1 /*
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/slab.h>
22 #include <linux/mfd/core.h>
24 #include <linux/mfd/janz.h>
26 #define DRV_NAME "janz-cmodio"
28 /* Size of each MODULbus module in PCI BAR4 */
29 #define CMODIO_MODULBUS_SIZE 0x200
31 /* Maximum number of MODULbus modules on a CMOD-IO carrier board */
32 #define CMODIO_MAX_MODULES 4
34 /* Module Parameters */
35 static unsigned int num_modules = CMODIO_MAX_MODULES;
36 static unsigned char *modules[CMODIO_MAX_MODULES] = {
37 "empty", "empty", "empty", "empty",
40 module_param_array(modules, charp, &num_modules, S_IRUGO);
41 MODULE_PARM_DESC(modules, "MODULbus modules attached to the carrier board");
43 /* Unique Device Id */
44 static unsigned int cmodio_id;
46 struct cmodio_device {
47 /* Parent PCI device */
48 struct pci_dev *pdev;
50 /* PLX control registers */
51 struct janz_cmodio_onboard_regs __iomem *ctrl;
53 /* hex switch position */
54 u8 hex;
56 /* mfd-core API */
57 struct mfd_cell cells[CMODIO_MAX_MODULES];
58 struct resource resources[3 * CMODIO_MAX_MODULES];
59 struct janz_platform_data pdata[CMODIO_MAX_MODULES];
63 * Subdevices using the mfd-core API
66 static int __devinit cmodio_setup_subdevice(struct cmodio_device *priv,
67 char *name, unsigned int devno,
68 unsigned int modno)
70 struct janz_platform_data *pdata;
71 struct mfd_cell *cell;
72 struct resource *res;
73 struct pci_dev *pci;
75 pci = priv->pdev;
76 cell = &priv->cells[devno];
77 res = &priv->resources[devno * 3];
78 pdata = &priv->pdata[devno];
80 cell->name = name;
81 cell->resources = res;
82 cell->num_resources = 3;
84 /* Setup the subdevice ID -- must be unique */
85 cell->id = cmodio_id++;
87 /* Add platform data */
88 pdata->modno = modno;
89 cell->platform_data = pdata;
90 cell->pdata_size = sizeof(*pdata);
92 /* MODULbus registers -- PCI BAR3 is big-endian MODULbus access */
93 res->flags = IORESOURCE_MEM;
94 res->parent = &pci->resource[3];
95 res->start = pci->resource[3].start + (CMODIO_MODULBUS_SIZE * modno);
96 res->end = res->start + CMODIO_MODULBUS_SIZE - 1;
97 res++;
99 /* PLX Control Registers -- PCI BAR4 is interrupt and other registers */
100 res->flags = IORESOURCE_MEM;
101 res->parent = &pci->resource[4];
102 res->start = pci->resource[4].start;
103 res->end = pci->resource[4].end;
104 res++;
107 * IRQ
109 * The start and end fields are used as an offset to the irq_base
110 * parameter passed into the mfd_add_devices() function call. All
111 * devices share the same IRQ.
113 res->flags = IORESOURCE_IRQ;
114 res->parent = NULL;
115 res->start = 0;
116 res->end = 0;
117 res++;
119 return 0;
122 /* Probe each submodule using kernel parameters */
123 static int __devinit cmodio_probe_submodules(struct cmodio_device *priv)
125 struct pci_dev *pdev = priv->pdev;
126 unsigned int num_probed = 0;
127 char *name;
128 int i;
130 for (i = 0; i < num_modules; i++) {
131 name = modules[i];
132 if (!strcmp(name, "") || !strcmp(name, "empty"))
133 continue;
135 dev_dbg(&priv->pdev->dev, "MODULbus %d: name %s\n", i, name);
136 cmodio_setup_subdevice(priv, name, num_probed, i);
137 num_probed++;
140 /* print an error message if no modules were probed */
141 if (num_probed == 0) {
142 dev_err(&priv->pdev->dev, "no MODULbus modules specified, "
143 "please set the ``modules'' kernel "
144 "parameter according to your "
145 "hardware configuration\n");
146 return -ENODEV;
149 return mfd_add_devices(&pdev->dev, 0, priv->cells,
150 num_probed, NULL, pdev->irq);
154 * SYSFS Attributes
157 static ssize_t mbus_show(struct device *dev, struct device_attribute *attr,
158 char *buf)
160 struct cmodio_device *priv = dev_get_drvdata(dev);
162 return snprintf(buf, PAGE_SIZE, "%x\n", priv->hex);
165 static DEVICE_ATTR(modulbus_number, S_IRUGO, mbus_show, NULL);
167 static struct attribute *cmodio_sysfs_attrs[] = {
168 &dev_attr_modulbus_number.attr,
169 NULL,
172 static const struct attribute_group cmodio_sysfs_attr_group = {
173 .attrs = cmodio_sysfs_attrs,
177 * PCI Driver
180 static int __devinit cmodio_pci_probe(struct pci_dev *dev,
181 const struct pci_device_id *id)
183 struct cmodio_device *priv;
184 int ret;
186 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
187 if (!priv) {
188 dev_err(&dev->dev, "unable to allocate private data\n");
189 ret = -ENOMEM;
190 goto out_return;
193 pci_set_drvdata(dev, priv);
194 priv->pdev = dev;
196 /* Hardware Initialization */
197 ret = pci_enable_device(dev);
198 if (ret) {
199 dev_err(&dev->dev, "unable to enable device\n");
200 goto out_free_priv;
203 pci_set_master(dev);
204 ret = pci_request_regions(dev, DRV_NAME);
205 if (ret) {
206 dev_err(&dev->dev, "unable to request regions\n");
207 goto out_pci_disable_device;
210 /* Onboard configuration registers */
211 priv->ctrl = pci_ioremap_bar(dev, 4);
212 if (!priv->ctrl) {
213 dev_err(&dev->dev, "unable to remap onboard regs\n");
214 ret = -ENOMEM;
215 goto out_pci_release_regions;
218 /* Read the hex switch on the carrier board */
219 priv->hex = ioread8(&priv->ctrl->int_enable);
221 /* Add the MODULbus number (hex switch value) to the device's sysfs */
222 ret = sysfs_create_group(&dev->dev.kobj, &cmodio_sysfs_attr_group);
223 if (ret) {
224 dev_err(&dev->dev, "unable to create sysfs attributes\n");
225 goto out_unmap_ctrl;
229 * Disable all interrupt lines, each submodule will enable its
230 * own interrupt line if needed
232 iowrite8(0xf, &priv->ctrl->int_disable);
234 /* Register drivers for all submodules */
235 ret = cmodio_probe_submodules(priv);
236 if (ret) {
237 dev_err(&dev->dev, "unable to probe submodules\n");
238 goto out_sysfs_remove_group;
241 return 0;
243 out_sysfs_remove_group:
244 sysfs_remove_group(&dev->dev.kobj, &cmodio_sysfs_attr_group);
245 out_unmap_ctrl:
246 iounmap(priv->ctrl);
247 out_pci_release_regions:
248 pci_release_regions(dev);
249 out_pci_disable_device:
250 pci_disable_device(dev);
251 out_free_priv:
252 kfree(priv);
253 out_return:
254 return ret;
257 static void __devexit cmodio_pci_remove(struct pci_dev *dev)
259 struct cmodio_device *priv = pci_get_drvdata(dev);
261 mfd_remove_devices(&dev->dev);
262 sysfs_remove_group(&dev->dev.kobj, &cmodio_sysfs_attr_group);
263 iounmap(priv->ctrl);
264 pci_release_regions(dev);
265 pci_disable_device(dev);
266 kfree(priv);
269 #define PCI_VENDOR_ID_JANZ 0x13c3
271 /* The list of devices that this module will support */
272 static DEFINE_PCI_DEVICE_TABLE(cmodio_pci_ids) = {
273 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030, PCI_VENDOR_ID_JANZ, 0x0101 },
274 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050, PCI_VENDOR_ID_JANZ, 0x0100 },
275 { 0, }
277 MODULE_DEVICE_TABLE(pci, cmodio_pci_ids);
279 static struct pci_driver cmodio_pci_driver = {
280 .name = DRV_NAME,
281 .id_table = cmodio_pci_ids,
282 .probe = cmodio_pci_probe,
283 .remove = __devexit_p(cmodio_pci_remove),
287 * Module Init / Exit
290 static int __init cmodio_init(void)
292 return pci_register_driver(&cmodio_pci_driver);
295 static void __exit cmodio_exit(void)
297 pci_unregister_driver(&cmodio_pci_driver);
300 MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
301 MODULE_DESCRIPTION("Janz CMOD-IO PCI MODULbus Carrier Board Driver");
302 MODULE_LICENSE("GPL");
304 module_init(cmodio_init);
305 module_exit(cmodio_exit);