blob: fcbb2e9dfd3706af3d6dd0cccfaa6f45693d7328 [file] [log] [blame]
Ira W. Snyderbd358132010-04-07 09:43:00 +02001/*
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/init.h>
17#include <linux/pci.h>
18#include <linux/interrupt.h>
19#include <linux/delay.h>
20#include <linux/platform_device.h>
David Miller8102bad2010-08-04 22:57:14 -070021#include <linux/slab.h>
Ira W. Snyderbd358132010-04-07 09:43:00 +020022#include <linux/mfd/core.h>
23
24#include <linux/mfd/janz.h>
25
26#define DRV_NAME "janz-cmodio"
27
28/* Size of each MODULbus module in PCI BAR4 */
29#define CMODIO_MODULBUS_SIZE 0x200
30
31/* Maximum number of MODULbus modules on a CMOD-IO carrier board */
32#define CMODIO_MAX_MODULES 4
33
34/* Module Parameters */
35static unsigned int num_modules = CMODIO_MAX_MODULES;
Rusty Russellbafeafe2012-01-13 09:32:16 +103036static char *modules[CMODIO_MAX_MODULES] = {
Ira W. Snyderbd358132010-04-07 09:43:00 +020037 "empty", "empty", "empty", "empty",
38};
39
40module_param_array(modules, charp, &num_modules, S_IRUGO);
41MODULE_PARM_DESC(modules, "MODULbus modules attached to the carrier board");
42
43/* Unique Device Id */
44static unsigned int cmodio_id;
45
46struct cmodio_device {
47 /* Parent PCI device */
48 struct pci_dev *pdev;
49
50 /* PLX control registers */
51 struct janz_cmodio_onboard_regs __iomem *ctrl;
52
53 /* hex switch position */
54 u8 hex;
55
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];
60};
61
62/*
63 * Subdevices using the mfd-core API
64 */
65
Bill Pembertonf791be42012-11-19 13:23:04 -050066static int cmodio_setup_subdevice(struct cmodio_device *priv,
Ira W. Snyderbd358132010-04-07 09:43:00 +020067 char *name, unsigned int devno,
68 unsigned int modno)
69{
70 struct janz_platform_data *pdata;
71 struct mfd_cell *cell;
72 struct resource *res;
73 struct pci_dev *pci;
74
75 pci = priv->pdev;
76 cell = &priv->cells[devno];
77 res = &priv->resources[devno * 3];
78 pdata = &priv->pdata[devno];
79
80 cell->name = name;
81 cell->resources = res;
82 cell->num_resources = 3;
83
84 /* Setup the subdevice ID -- must be unique */
85 cell->id = cmodio_id++;
86
87 /* Add platform data */
88 pdata->modno = modno;
Samuel Ortiz3d2bdf72011-04-06 16:02:25 +020089 cell->platform_data = pdata;
90 cell->pdata_size = sizeof(*pdata);
Ira W. Snyderbd358132010-04-07 09:43:00 +020091
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++;
98
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++;
105
106 /*
107 * IRQ
108 *
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.
112 */
113 res->flags = IORESOURCE_IRQ;
114 res->parent = NULL;
115 res->start = 0;
116 res->end = 0;
117 res++;
118
119 return 0;
120}
121
122/* Probe each submodule using kernel parameters */
Bill Pembertonf791be42012-11-19 13:23:04 -0500123static int cmodio_probe_submodules(struct cmodio_device *priv)
Ira W. Snyderbd358132010-04-07 09:43:00 +0200124{
125 struct pci_dev *pdev = priv->pdev;
126 unsigned int num_probed = 0;
127 char *name;
128 int i;
129
130 for (i = 0; i < num_modules; i++) {
131 name = modules[i];
132 if (!strcmp(name, "") || !strcmp(name, "empty"))
133 continue;
134
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++;
138 }
139
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;
147 }
148
149 return mfd_add_devices(&pdev->dev, 0, priv->cells,
Mark Brown0848c942012-09-11 15:16:36 +0800150 num_probed, NULL, pdev->irq, NULL);
Ira W. Snyderbd358132010-04-07 09:43:00 +0200151}
152
153/*
154 * SYSFS Attributes
155 */
156
157static ssize_t mbus_show(struct device *dev, struct device_attribute *attr,
158 char *buf)
159{
160 struct cmodio_device *priv = dev_get_drvdata(dev);
161
162 return snprintf(buf, PAGE_SIZE, "%x\n", priv->hex);
163}
164
165static DEVICE_ATTR(modulbus_number, S_IRUGO, mbus_show, NULL);
166
167static struct attribute *cmodio_sysfs_attrs[] = {
168 &dev_attr_modulbus_number.attr,
169 NULL,
170};
171
172static const struct attribute_group cmodio_sysfs_attr_group = {
173 .attrs = cmodio_sysfs_attrs,
174};
175
176/*
177 * PCI Driver
178 */
179
Bill Pembertonf791be42012-11-19 13:23:04 -0500180static int cmodio_pci_probe(struct pci_dev *dev,
Ira W. Snyderbd358132010-04-07 09:43:00 +0200181 const struct pci_device_id *id)
182{
183 struct cmodio_device *priv;
184 int ret;
185
Lee Jonesfa75d4a2013-05-23 16:25:15 +0100186 priv = devm_kzalloc(&dev->dev, sizeof(*priv), GFP_KERNEL);
Ira W. Snyderbd358132010-04-07 09:43:00 +0200187 if (!priv) {
188 dev_err(&dev->dev, "unable to allocate private data\n");
Lee Jonesfa75d4a2013-05-23 16:25:15 +0100189 return -ENOMEM;
Ira W. Snyderbd358132010-04-07 09:43:00 +0200190 }
191
192 pci_set_drvdata(dev, priv);
193 priv->pdev = dev;
194
195 /* Hardware Initialization */
196 ret = pci_enable_device(dev);
197 if (ret) {
198 dev_err(&dev->dev, "unable to enable device\n");
Lee Jonesfa75d4a2013-05-23 16:25:15 +0100199 return ret;
Ira W. Snyderbd358132010-04-07 09:43:00 +0200200 }
201
202 pci_set_master(dev);
203 ret = pci_request_regions(dev, DRV_NAME);
204 if (ret) {
205 dev_err(&dev->dev, "unable to request regions\n");
206 goto out_pci_disable_device;
207 }
208
209 /* Onboard configuration registers */
210 priv->ctrl = pci_ioremap_bar(dev, 4);
211 if (!priv->ctrl) {
212 dev_err(&dev->dev, "unable to remap onboard regs\n");
213 ret = -ENOMEM;
214 goto out_pci_release_regions;
215 }
216
217 /* Read the hex switch on the carrier board */
218 priv->hex = ioread8(&priv->ctrl->int_enable);
219
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);
222 if (ret) {
223 dev_err(&dev->dev, "unable to create sysfs attributes\n");
224 goto out_unmap_ctrl;
225 }
226
227 /*
228 * Disable all interrupt lines, each submodule will enable its
229 * own interrupt line if needed
230 */
231 iowrite8(0xf, &priv->ctrl->int_disable);
232
233 /* Register drivers for all submodules */
234 ret = cmodio_probe_submodules(priv);
235 if (ret) {
236 dev_err(&dev->dev, "unable to probe submodules\n");
237 goto out_sysfs_remove_group;
238 }
239
240 return 0;
241
242out_sysfs_remove_group:
243 sysfs_remove_group(&dev->dev.kobj, &cmodio_sysfs_attr_group);
244out_unmap_ctrl:
245 iounmap(priv->ctrl);
246out_pci_release_regions:
247 pci_release_regions(dev);
248out_pci_disable_device:
249 pci_disable_device(dev);
Lee Jonesfa75d4a2013-05-23 16:25:15 +0100250
Ira W. Snyderbd358132010-04-07 09:43:00 +0200251 return ret;
252}
253
Bill Pemberton4740f732012-11-19 13:26:01 -0500254static void cmodio_pci_remove(struct pci_dev *dev)
Ira W. Snyderbd358132010-04-07 09:43:00 +0200255{
256 struct cmodio_device *priv = pci_get_drvdata(dev);
257
258 mfd_remove_devices(&dev->dev);
259 sysfs_remove_group(&dev->dev.kobj, &cmodio_sysfs_attr_group);
260 iounmap(priv->ctrl);
261 pci_release_regions(dev);
262 pci_disable_device(dev);
Ira W. Snyderbd358132010-04-07 09:43:00 +0200263}
264
265#define PCI_VENDOR_ID_JANZ 0x13c3
266
267/* The list of devices that this module will support */
268static DEFINE_PCI_DEVICE_TABLE(cmodio_pci_ids) = {
269 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030, PCI_VENDOR_ID_JANZ, 0x0101 },
270 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050, PCI_VENDOR_ID_JANZ, 0x0100 },
271 { 0, }
272};
273MODULE_DEVICE_TABLE(pci, cmodio_pci_ids);
274
275static struct pci_driver cmodio_pci_driver = {
276 .name = DRV_NAME,
277 .id_table = cmodio_pci_ids,
278 .probe = cmodio_pci_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500279 .remove = cmodio_pci_remove,
Ira W. Snyderbd358132010-04-07 09:43:00 +0200280};
281
Axel Lin38a36f52012-04-03 09:09:19 +0800282module_pci_driver(cmodio_pci_driver);
Ira W. Snyderbd358132010-04-07 09:43:00 +0200283
284MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
285MODULE_DESCRIPTION("Janz CMOD-IO PCI MODULbus Carrier Board Driver");
286MODULE_LICENSE("GPL");