diff options
author | Stephane Grosjean <s.grosjean@peak-system.com> | 2012-03-02 10:21:19 -0500 |
---|---|---|
committer | Marc Kleine-Budde <mkl@pengutronix.de> | 2012-03-03 11:40:39 -0500 |
commit | e6d9c80b7ca1504411ad6d7acdb8683e4ae1c9cd (patch) | |
tree | 0bb7a7b48fe5e79053b92708612ad1f10df0572c | |
parent | 224223aaf9a44cd484019d2b765b6ca2beee6dd4 (diff) |
can: peak_pci: add support of some new PEAK-System PCI cards
This patch adds the support of some new PEAK-System PCI cards in the CAN
network sub-system. These are:
PCAN-PCIeC (PCI-ExpressCard)
PCAN-mminiPCI (mini-PCI)
PCAN-PCI (PCI next-gen)
This patch also adds the control of the blinking leds of the PCAN-PCIeC.
Tested-by: Oliver Hartkopp <socketcan@hartkopp.net>
Acked-by: Wolfgang Grandegger <wg@grandegger.com>
Signed-off-by: Stephane Grosjean <s.grosjean@peak-system.com>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
-rw-r--r-- | drivers/net/can/sja1000/Kconfig | 18 | ||||
-rw-r--r-- | drivers/net/can/sja1000/peak_pci.c | 505 |
2 files changed, 506 insertions, 17 deletions
diff --git a/drivers/net/can/sja1000/Kconfig b/drivers/net/can/sja1000/Kconfig index b21523ddff3c..fdc39261d464 100644 --- a/drivers/net/can/sja1000/Kconfig +++ b/drivers/net/can/sja1000/Kconfig | |||
@@ -44,11 +44,23 @@ config CAN_EMS_PCI | |||
44 | (http://www.ems-wuensche.de). | 44 | (http://www.ems-wuensche.de). |
45 | 45 | ||
46 | config CAN_PEAK_PCI | 46 | config CAN_PEAK_PCI |
47 | tristate "PEAK PCAN PCI/PCIe Cards" | 47 | tristate "PEAK PCAN-PCI/PCIe/miniPCI Cards" |
48 | depends on PCI | 48 | depends on PCI |
49 | ---help--- | 49 | ---help--- |
50 | This driver is for the PCAN PCI/PCIe cards (1, 2, 3 or 4 channels) | 50 | This driver is for the PCAN-PCI/PCIe/miniPCI cards |
51 | from PEAK Systems (http://www.peak-system.com). | 51 | (1, 2, 3 or 4 channels) from PEAK-System Technik |
52 | (http://www.peak-system.com). | ||
53 | |||
54 | config CAN_PEAK_PCIEC | ||
55 | bool "PEAK PCAN-ExpressCard Cards" | ||
56 | depends on CAN_PEAK_PCI | ||
57 | select I2C | ||
58 | select I2C_ALGOBIT | ||
59 | default y | ||
60 | ---help--- | ||
61 | Say Y here if you want to use a PCAN-ExpressCard from PEAK-System | ||
62 | Technik. This will also automatically select I2C and I2C_ALGO | ||
63 | configuration options. | ||
52 | 64 | ||
53 | config CAN_KVASER_PCI | 65 | config CAN_KVASER_PCI |
54 | tristate "Kvaser PCIcanx and Kvaser PCIcan PCI Cards" | 66 | tristate "Kvaser PCIcanx and Kvaser PCIcan PCI Cards" |
diff --git a/drivers/net/can/sja1000/peak_pci.c b/drivers/net/can/sja1000/peak_pci.c index 214795945bc4..5f92b865f64b 100644 --- a/drivers/net/can/sja1000/peak_pci.c +++ b/drivers/net/can/sja1000/peak_pci.c | |||
@@ -1,5 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (C) 2007, 2011 Wolfgang Grandegger <wg@grandegger.com> | 2 | * Copyright (C) 2007, 2011 Wolfgang Grandegger <wg@grandegger.com> |
3 | * Copyright (C) 2012 Stephane Grosjean <s.grosjean@peak-system.com> | ||
3 | * | 4 | * |
4 | * Derived from the PCAN project file driver/src/pcan_pci.c: | 5 | * Derived from the PCAN project file driver/src/pcan_pci.c: |
5 | * | 6 | * |
@@ -13,10 +14,6 @@ | |||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * GNU General Public License for more details. | 16 | * GNU General Public License for more details. |
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software Foundation, | ||
19 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
20 | */ | 17 | */ |
21 | 18 | ||
22 | #include <linux/kernel.h> | 19 | #include <linux/kernel.h> |
@@ -26,22 +23,26 @@ | |||
26 | #include <linux/delay.h> | 23 | #include <linux/delay.h> |
27 | #include <linux/pci.h> | 24 | #include <linux/pci.h> |
28 | #include <linux/io.h> | 25 | #include <linux/io.h> |
26 | #include <linux/i2c.h> | ||
27 | #include <linux/i2c-algo-bit.h> | ||
29 | #include <linux/can.h> | 28 | #include <linux/can.h> |
30 | #include <linux/can/dev.h> | 29 | #include <linux/can/dev.h> |
31 | 30 | ||
32 | #include "sja1000.h" | 31 | #include "sja1000.h" |
33 | 32 | ||
34 | MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>"); | 33 | MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>"); |
35 | MODULE_DESCRIPTION("Socket-CAN driver for PEAK PCAN PCI/PCIe cards"); | 34 | MODULE_DESCRIPTION("Socket-CAN driver for PEAK PCAN PCI family cards"); |
36 | MODULE_SUPPORTED_DEVICE("PEAK PCAN PCI/PCIe CAN card"); | 35 | MODULE_SUPPORTED_DEVICE("PEAK PCAN PCI/PCIe/PCIeC miniPCI CAN cards"); |
37 | MODULE_LICENSE("GPL v2"); | 36 | MODULE_LICENSE("GPL v2"); |
38 | 37 | ||
39 | #define DRV_NAME "peak_pci" | 38 | #define DRV_NAME "peak_pci" |
40 | 39 | ||
40 | struct peak_pciec_card; | ||
41 | struct peak_pci_chan { | 41 | struct peak_pci_chan { |
42 | void __iomem *cfg_base; /* Common for all channels */ | 42 | void __iomem *cfg_base; /* Common for all channels */ |
43 | struct net_device *prev_dev; /* Chain of network devices */ | 43 | struct net_device *prev_dev; /* Chain of network devices */ |
44 | u16 icr_mask; /* Interrupt mask for fast ack */ | 44 | u16 icr_mask; /* Interrupt mask for fast ack */ |
45 | struct peak_pciec_card *pciec_card; /* only for PCIeC LEDs */ | ||
45 | }; | 46 | }; |
46 | 47 | ||
47 | #define PEAK_PCI_CAN_CLOCK (16000000 / 2) | 48 | #define PEAK_PCI_CAN_CLOCK (16000000 / 2) |
@@ -61,16 +62,464 @@ struct peak_pci_chan { | |||
61 | 62 | ||
62 | #define PEAK_PCI_VENDOR_ID 0x001C /* The PCI device and vendor IDs */ | 63 | #define PEAK_PCI_VENDOR_ID 0x001C /* The PCI device and vendor IDs */ |
63 | #define PEAK_PCI_DEVICE_ID 0x0001 /* for PCI/PCIe slot cards */ | 64 | #define PEAK_PCI_DEVICE_ID 0x0001 /* for PCI/PCIe slot cards */ |
65 | #define PEAK_PCIEC_DEVICE_ID 0x0002 /* for ExpressCard slot cards */ | ||
66 | #define PEAK_PCIE_DEVICE_ID 0x0003 /* for nextgen PCIe slot cards */ | ||
67 | #define PEAK_MPCI_DEVICE_ID 0x0008 /* The miniPCI slot cards */ | ||
68 | |||
69 | #define PEAK_PCI_CHAN_MAX 4 | ||
64 | 70 | ||
65 | static const u16 peak_pci_icr_masks[] = {0x02, 0x01, 0x40, 0x80}; | 71 | static const u16 peak_pci_icr_masks[PEAK_PCI_CHAN_MAX] = { |
72 | 0x02, 0x01, 0x40, 0x80 | ||
73 | }; | ||
66 | 74 | ||
67 | static DEFINE_PCI_DEVICE_TABLE(peak_pci_tbl) = { | 75 | static DEFINE_PCI_DEVICE_TABLE(peak_pci_tbl) = { |
68 | {PEAK_PCI_VENDOR_ID, PEAK_PCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,}, | 76 | {PEAK_PCI_VENDOR_ID, PEAK_PCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,}, |
77 | {PEAK_PCI_VENDOR_ID, PEAK_PCIE_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,}, | ||
78 | {PEAK_PCI_VENDOR_ID, PEAK_MPCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,}, | ||
79 | #ifdef CONFIG_CAN_PEAK_PCIEC | ||
80 | {PEAK_PCI_VENDOR_ID, PEAK_PCIEC_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,}, | ||
81 | #endif | ||
69 | {0,} | 82 | {0,} |
70 | }; | 83 | }; |
71 | 84 | ||
72 | MODULE_DEVICE_TABLE(pci, peak_pci_tbl); | 85 | MODULE_DEVICE_TABLE(pci, peak_pci_tbl); |
73 | 86 | ||
87 | #ifdef CONFIG_CAN_PEAK_PCIEC | ||
88 | /* | ||
89 | * PCAN-ExpressCard needs I2C bit-banging configuration option. | ||
90 | */ | ||
91 | |||
92 | /* GPIOICR byte access offsets */ | ||
93 | #define PITA_GPOUT 0x18 /* GPx output value */ | ||
94 | #define PITA_GPIN 0x19 /* GPx input value */ | ||
95 | #define PITA_GPOEN 0x1A /* configure GPx as ouput pin */ | ||
96 | |||
97 | /* I2C GP bits */ | ||
98 | #define PITA_GPIN_SCL 0x01 /* Serial Clock Line */ | ||
99 | #define PITA_GPIN_SDA 0x04 /* Serial DAta line */ | ||
100 | |||
101 | #define PCA9553_1_SLAVEADDR (0xC4 >> 1) | ||
102 | |||
103 | /* PCA9553 LS0 fields values */ | ||
104 | enum { | ||
105 | PCA9553_LOW, | ||
106 | PCA9553_HIGHZ, | ||
107 | PCA9553_PWM0, | ||
108 | PCA9553_PWM1 | ||
109 | }; | ||
110 | |||
111 | /* LEDs control */ | ||
112 | #define PCA9553_ON PCA9553_LOW | ||
113 | #define PCA9553_OFF PCA9553_HIGHZ | ||
114 | #define PCA9553_SLOW PCA9553_PWM0 | ||
115 | #define PCA9553_FAST PCA9553_PWM1 | ||
116 | |||
117 | #define PCA9553_LED(c) (1 << (c)) | ||
118 | #define PCA9553_LED_STATE(s, c) ((s) << ((c) << 1)) | ||
119 | |||
120 | #define PCA9553_LED_ON(c) PCA9553_LED_STATE(PCA9553_ON, c) | ||
121 | #define PCA9553_LED_OFF(c) PCA9553_LED_STATE(PCA9553_OFF, c) | ||
122 | #define PCA9553_LED_SLOW(c) PCA9553_LED_STATE(PCA9553_SLOW, c) | ||
123 | #define PCA9553_LED_FAST(c) PCA9553_LED_STATE(PCA9553_FAST, c) | ||
124 | #define PCA9553_LED_MASK(c) PCA9553_LED_STATE(0x03, c) | ||
125 | |||
126 | #define PCA9553_LED_OFF_ALL (PCA9553_LED_OFF(0) | PCA9553_LED_OFF(1)) | ||
127 | |||
128 | #define PCA9553_LS0_INIT 0x40 /* initial value (!= from 0x00) */ | ||
129 | |||
130 | struct peak_pciec_chan { | ||
131 | struct net_device *netdev; | ||
132 | unsigned long prev_rx_bytes; | ||
133 | unsigned long prev_tx_bytes; | ||
134 | }; | ||
135 | |||
136 | struct peak_pciec_card { | ||
137 | void __iomem *cfg_base; /* Common for all channels */ | ||
138 | void __iomem *reg_base; /* first channel base address */ | ||
139 | u8 led_cache; /* leds state cache */ | ||
140 | |||
141 | /* PCIExpressCard i2c data */ | ||
142 | struct i2c_algo_bit_data i2c_bit; | ||
143 | struct i2c_adapter led_chip; | ||
144 | struct delayed_work led_work; /* led delayed work */ | ||
145 | int chan_count; | ||
146 | struct peak_pciec_chan channel[PEAK_PCI_CHAN_MAX]; | ||
147 | }; | ||
148 | |||
149 | /* "normal" pci register write callback is overloaded for leds control */ | ||
150 | static void peak_pci_write_reg(const struct sja1000_priv *priv, | ||
151 | int port, u8 val); | ||
152 | |||
153 | static inline void pita_set_scl_highz(struct peak_pciec_card *card) | ||
154 | { | ||
155 | u8 gp_outen = readb(card->cfg_base + PITA_GPOEN) & ~PITA_GPIN_SCL; | ||
156 | writeb(gp_outen, card->cfg_base + PITA_GPOEN); | ||
157 | } | ||
158 | |||
159 | static inline void pita_set_sda_highz(struct peak_pciec_card *card) | ||
160 | { | ||
161 | u8 gp_outen = readb(card->cfg_base + PITA_GPOEN) & ~PITA_GPIN_SDA; | ||
162 | writeb(gp_outen, card->cfg_base + PITA_GPOEN); | ||
163 | } | ||
164 | |||
165 | static void peak_pciec_init_pita_gpio(struct peak_pciec_card *card) | ||
166 | { | ||
167 | /* raise SCL & SDA GPIOs to high-Z */ | ||
168 | pita_set_scl_highz(card); | ||
169 | pita_set_sda_highz(card); | ||
170 | } | ||
171 | |||
172 | static void pita_setsda(void *data, int state) | ||
173 | { | ||
174 | struct peak_pciec_card *card = (struct peak_pciec_card *)data; | ||
175 | u8 gp_out, gp_outen; | ||
176 | |||
177 | /* set output sda always to 0 */ | ||
178 | gp_out = readb(card->cfg_base + PITA_GPOUT) & ~PITA_GPIN_SDA; | ||
179 | writeb(gp_out, card->cfg_base + PITA_GPOUT); | ||
180 | |||
181 | /* control output sda with GPOEN */ | ||
182 | gp_outen = readb(card->cfg_base + PITA_GPOEN); | ||
183 | if (state) | ||
184 | gp_outen &= ~PITA_GPIN_SDA; | ||
185 | else | ||
186 | gp_outen |= PITA_GPIN_SDA; | ||
187 | |||
188 | writeb(gp_outen, card->cfg_base + PITA_GPOEN); | ||
189 | } | ||
190 | |||
191 | static void pita_setscl(void *data, int state) | ||
192 | { | ||
193 | struct peak_pciec_card *card = (struct peak_pciec_card *)data; | ||
194 | u8 gp_out, gp_outen; | ||
195 | |||
196 | /* set output scl always to 0 */ | ||
197 | gp_out = readb(card->cfg_base + PITA_GPOUT) & ~PITA_GPIN_SCL; | ||
198 | writeb(gp_out, card->cfg_base + PITA_GPOUT); | ||
199 | |||
200 | /* control output scl with GPOEN */ | ||
201 | gp_outen = readb(card->cfg_base + PITA_GPOEN); | ||
202 | if (state) | ||
203 | gp_outen &= ~PITA_GPIN_SCL; | ||
204 | else | ||
205 | gp_outen |= PITA_GPIN_SCL; | ||
206 | |||
207 | writeb(gp_outen, card->cfg_base + PITA_GPOEN); | ||
208 | } | ||
209 | |||
210 | static int pita_getsda(void *data) | ||
211 | { | ||
212 | struct peak_pciec_card *card = (struct peak_pciec_card *)data; | ||
213 | |||
214 | /* set tristate */ | ||
215 | pita_set_sda_highz(card); | ||
216 | |||
217 | return (readb(card->cfg_base + PITA_GPIN) & PITA_GPIN_SDA) ? 1 : 0; | ||
218 | } | ||
219 | |||
220 | static int pita_getscl(void *data) | ||
221 | { | ||
222 | struct peak_pciec_card *card = (struct peak_pciec_card *)data; | ||
223 | |||
224 | /* set tristate */ | ||
225 | pita_set_scl_highz(card); | ||
226 | |||
227 | return (readb(card->cfg_base + PITA_GPIN) & PITA_GPIN_SCL) ? 1 : 0; | ||
228 | } | ||
229 | |||
230 | /* | ||
231 | * write commands to the LED chip though the I2C-bus of the PCAN-PCIeC | ||
232 | */ | ||
233 | static int peak_pciec_write_pca9553(struct peak_pciec_card *card, | ||
234 | u8 offset, u8 data) | ||
235 | { | ||
236 | u8 buffer[2] = { | ||
237 | offset, | ||
238 | data | ||
239 | }; | ||
240 | struct i2c_msg msg = { | ||
241 | .addr = PCA9553_1_SLAVEADDR, | ||
242 | .len = 2, | ||
243 | .buf = buffer, | ||
244 | }; | ||
245 | int ret; | ||
246 | |||
247 | /* cache led mask */ | ||
248 | if ((offset == 5) && (data == card->led_cache)) | ||
249 | return 0; | ||
250 | |||
251 | ret = i2c_transfer(&card->led_chip, &msg, 1); | ||
252 | if (ret < 0) | ||
253 | return ret; | ||
254 | |||
255 | if (offset == 5) | ||
256 | card->led_cache = data; | ||
257 | |||
258 | return 0; | ||
259 | } | ||
260 | |||
261 | /* | ||
262 | * delayed work callback used to control the LEDs | ||
263 | */ | ||
264 | static void peak_pciec_led_work(struct work_struct *work) | ||
265 | { | ||
266 | struct peak_pciec_card *card = | ||
267 | container_of(work, struct peak_pciec_card, led_work.work); | ||
268 | struct net_device *netdev; | ||
269 | u8 new_led = card->led_cache; | ||
270 | int i, up_count = 0; | ||
271 | |||
272 | /* first check what is to do */ | ||
273 | for (i = 0; i < card->chan_count; i++) { | ||
274 | /* default is: not configured */ | ||
275 | new_led &= ~PCA9553_LED_MASK(i); | ||
276 | new_led |= PCA9553_LED_ON(i); | ||
277 | |||
278 | netdev = card->channel[i].netdev; | ||
279 | if (!netdev || !(netdev->flags & IFF_UP)) | ||
280 | continue; | ||
281 | |||
282 | up_count++; | ||
283 | |||
284 | /* no activity (but configured) */ | ||
285 | new_led &= ~PCA9553_LED_MASK(i); | ||
286 | new_led |= PCA9553_LED_SLOW(i); | ||
287 | |||
288 | /* if bytes counters changed, set fast blinking led */ | ||
289 | if (netdev->stats.rx_bytes != card->channel[i].prev_rx_bytes) { | ||
290 | card->channel[i].prev_rx_bytes = netdev->stats.rx_bytes; | ||
291 | new_led &= ~PCA9553_LED_MASK(i); | ||
292 | new_led |= PCA9553_LED_FAST(i); | ||
293 | } | ||
294 | if (netdev->stats.tx_bytes != card->channel[i].prev_tx_bytes) { | ||
295 | card->channel[i].prev_tx_bytes = netdev->stats.tx_bytes; | ||
296 | new_led &= ~PCA9553_LED_MASK(i); | ||
297 | new_led |= PCA9553_LED_FAST(i); | ||
298 | } | ||
299 | } | ||
300 | |||
301 | /* check if LS0 settings changed, only update i2c if so */ | ||
302 | peak_pciec_write_pca9553(card, 5, new_led); | ||
303 | |||
304 | /* restart timer (except if no more configured channels) */ | ||
305 | if (up_count) | ||
306 | schedule_delayed_work(&card->led_work, HZ); | ||
307 | } | ||
308 | |||
309 | /* | ||
310 | * set LEDs blinking state | ||
311 | */ | ||
312 | static void peak_pciec_set_leds(struct peak_pciec_card *card, u8 led_mask, u8 s) | ||
313 | { | ||
314 | u8 new_led = card->led_cache; | ||
315 | int i; | ||
316 | |||
317 | /* first check what is to do */ | ||
318 | for (i = 0; i < card->chan_count; i++) | ||
319 | if (led_mask & PCA9553_LED(i)) { | ||
320 | new_led &= ~PCA9553_LED_MASK(i); | ||
321 | new_led |= PCA9553_LED_STATE(s, i); | ||
322 | } | ||
323 | |||
324 | /* check if LS0 settings changed, only update i2c if so */ | ||
325 | peak_pciec_write_pca9553(card, 5, new_led); | ||
326 | } | ||
327 | |||
328 | /* | ||
329 | * start one second delayed work to control LEDs | ||
330 | */ | ||
331 | static void peak_pciec_start_led_work(struct peak_pciec_card *card) | ||
332 | { | ||
333 | if (!delayed_work_pending(&card->led_work)) | ||
334 | schedule_delayed_work(&card->led_work, HZ); | ||
335 | } | ||
336 | |||
337 | /* | ||
338 | * stop LEDs delayed work | ||
339 | */ | ||
340 | static void peak_pciec_stop_led_work(struct peak_pciec_card *card) | ||
341 | { | ||
342 | cancel_delayed_work_sync(&card->led_work); | ||
343 | } | ||
344 | |||
345 | /* | ||
346 | * initialize the PCA9553 4-bit I2C-bus LED chip | ||
347 | */ | ||
348 | static int peak_pciec_init_leds(struct peak_pciec_card *card) | ||
349 | { | ||
350 | int err; | ||
351 | |||
352 | /* prescaler for frequency 0: "SLOW" = 1 Hz = "44" */ | ||
353 | err = peak_pciec_write_pca9553(card, 1, 44 / 1); | ||
354 | if (err) | ||
355 | return err; | ||
356 | |||
357 | /* duty cycle 0: 50% */ | ||
358 | err = peak_pciec_write_pca9553(card, 2, 0x80); | ||
359 | if (err) | ||
360 | return err; | ||
361 | |||
362 | /* prescaler for frequency 1: "FAST" = 5 Hz */ | ||
363 | err = peak_pciec_write_pca9553(card, 3, 44 / 5); | ||
364 | if (err) | ||
365 | return err; | ||
366 | |||
367 | /* duty cycle 1: 50% */ | ||
368 | err = peak_pciec_write_pca9553(card, 4, 0x80); | ||
369 | if (err) | ||
370 | return err; | ||
371 | |||
372 | /* switch LEDs to initial state */ | ||
373 | return peak_pciec_write_pca9553(card, 5, PCA9553_LS0_INIT); | ||
374 | } | ||
375 | |||
376 | /* | ||
377 | * restore LEDs state to off peak_pciec_leds_exit | ||
378 | */ | ||
379 | static void peak_pciec_leds_exit(struct peak_pciec_card *card) | ||
380 | { | ||
381 | /* switch LEDs to off */ | ||
382 | peak_pciec_write_pca9553(card, 5, PCA9553_LED_OFF_ALL); | ||
383 | } | ||
384 | |||
385 | /* | ||
386 | * normal write sja1000 register method overloaded to catch when controller | ||
387 | * is started or stopped, to control leds | ||
388 | */ | ||
389 | static void peak_pciec_write_reg(const struct sja1000_priv *priv, | ||
390 | int port, u8 val) | ||
391 | { | ||
392 | struct peak_pci_chan *chan = priv->priv; | ||
393 | struct peak_pciec_card *card = chan->pciec_card; | ||
394 | int c = (priv->reg_base - card->reg_base) / PEAK_PCI_CHAN_SIZE; | ||
395 | |||
396 | /* sja1000 register changes control the leds state */ | ||
397 | if (port == REG_MOD) | ||
398 | switch (val) { | ||
399 | case MOD_RM: | ||
400 | /* Reset Mode: set led on */ | ||
401 | peak_pciec_set_leds(card, PCA9553_LED(c), PCA9553_ON); | ||
402 | break; | ||
403 | case 0x00: | ||
404 | /* Normal Mode: led slow blinking and start led timer */ | ||
405 | peak_pciec_set_leds(card, PCA9553_LED(c), PCA9553_SLOW); | ||
406 | peak_pciec_start_led_work(card); | ||
407 | break; | ||
408 | default: | ||
409 | break; | ||
410 | } | ||
411 | |||
412 | /* call base function */ | ||
413 | peak_pci_write_reg(priv, port, val); | ||
414 | } | ||
415 | |||
416 | static struct i2c_algo_bit_data peak_pciec_i2c_bit_ops = { | ||
417 | .setsda = pita_setsda, | ||
418 | .setscl = pita_setscl, | ||
419 | .getsda = pita_getsda, | ||
420 | .getscl = pita_getscl, | ||
421 | .udelay = 10, | ||
422 | .timeout = HZ, | ||
423 | }; | ||
424 | |||
425 | static int peak_pciec_probe(struct pci_dev *pdev, struct net_device *dev) | ||
426 | { | ||
427 | struct sja1000_priv *priv = netdev_priv(dev); | ||
428 | struct peak_pci_chan *chan = priv->priv; | ||
429 | struct peak_pciec_card *card; | ||
430 | int err; | ||
431 | |||
432 | /* copy i2c object address from 1st channel */ | ||
433 | if (chan->prev_dev) { | ||
434 | struct sja1000_priv *prev_priv = netdev_priv(chan->prev_dev); | ||
435 | struct peak_pci_chan *prev_chan = prev_priv->priv; | ||
436 | |||
437 | card = prev_chan->pciec_card; | ||
438 | if (!card) | ||
439 | return -ENODEV; | ||
440 | |||
441 | /* channel is the first one: do the init part */ | ||
442 | } else { | ||
443 | /* create the bit banging I2C adapter structure */ | ||
444 | card = kzalloc(sizeof(struct peak_pciec_card), GFP_KERNEL); | ||
445 | if (!card) { | ||
446 | dev_err(&pdev->dev, | ||
447 | "failed allocating memory for i2c chip\n"); | ||
448 | return -ENOMEM; | ||
449 | } | ||
450 | |||
451 | card->cfg_base = chan->cfg_base; | ||
452 | card->reg_base = priv->reg_base; | ||
453 | |||
454 | card->led_chip.owner = THIS_MODULE; | ||
455 | card->led_chip.dev.parent = &pdev->dev; | ||
456 | card->led_chip.algo_data = &card->i2c_bit; | ||
457 | strncpy(card->led_chip.name, "peak_i2c", | ||
458 | sizeof(card->led_chip.name)); | ||
459 | |||
460 | card->i2c_bit = peak_pciec_i2c_bit_ops; | ||
461 | card->i2c_bit.udelay = 10; | ||
462 | card->i2c_bit.timeout = HZ; | ||
463 | card->i2c_bit.data = card; | ||
464 | |||
465 | peak_pciec_init_pita_gpio(card); | ||
466 | |||
467 | err = i2c_bit_add_bus(&card->led_chip); | ||
468 | if (err) { | ||
469 | dev_err(&pdev->dev, "i2c init failed\n"); | ||
470 | goto pciec_init_err_1; | ||
471 | } | ||
472 | |||
473 | err = peak_pciec_init_leds(card); | ||
474 | if (err) { | ||
475 | dev_err(&pdev->dev, "leds hardware init failed\n"); | ||
476 | goto pciec_init_err_2; | ||
477 | } | ||
478 | |||
479 | INIT_DELAYED_WORK(&card->led_work, peak_pciec_led_work); | ||
480 | /* PCAN-ExpressCard needs its own callback for leds */ | ||
481 | priv->write_reg = peak_pciec_write_reg; | ||
482 | } | ||
483 | |||
484 | chan->pciec_card = card; | ||
485 | card->channel[card->chan_count++].netdev = dev; | ||
486 | |||
487 | return 0; | ||
488 | |||
489 | pciec_init_err_2: | ||
490 | i2c_del_adapter(&card->led_chip); | ||
491 | |||
492 | pciec_init_err_1: | ||
493 | peak_pciec_init_pita_gpio(card); | ||
494 | kfree(card); | ||
495 | |||
496 | return err; | ||
497 | } | ||
498 | |||
499 | static void peak_pciec_remove(struct peak_pciec_card *card) | ||
500 | { | ||
501 | peak_pciec_stop_led_work(card); | ||
502 | peak_pciec_leds_exit(card); | ||
503 | i2c_del_adapter(&card->led_chip); | ||
504 | peak_pciec_init_pita_gpio(card); | ||
505 | kfree(card); | ||
506 | } | ||
507 | |||
508 | #else /* CONFIG_CAN_PEAK_PCIEC */ | ||
509 | |||
510 | /* | ||
511 | * Placebo functions when PCAN-ExpressCard support is not selected | ||
512 | */ | ||
513 | static inline int peak_pciec_probe(struct pci_dev *pdev, struct net_device *dev) | ||
514 | { | ||
515 | return -ENODEV; | ||
516 | } | ||
517 | |||
518 | static inline void peak_pciec_remove(struct peak_pciec_card *card) | ||
519 | { | ||
520 | } | ||
521 | #endif /* CONFIG_CAN_PEAK_PCIEC */ | ||
522 | |||
74 | static u8 peak_pci_read_reg(const struct sja1000_priv *priv, int port) | 523 | static u8 peak_pci_read_reg(const struct sja1000_priv *priv, int port) |
75 | { | 524 | { |
76 | return readb(priv->reg_base + (port << 2)); | 525 | return readb(priv->reg_base + (port << 2)); |
@@ -188,17 +637,31 @@ static int __devinit peak_pci_probe(struct pci_dev *pdev, | |||
188 | 637 | ||
189 | SET_NETDEV_DEV(dev, &pdev->dev); | 638 | SET_NETDEV_DEV(dev, &pdev->dev); |
190 | 639 | ||
640 | /* Create chain of SJA1000 devices */ | ||
641 | chan->prev_dev = pci_get_drvdata(pdev); | ||
642 | pci_set_drvdata(pdev, dev); | ||
643 | |||
644 | /* | ||
645 | * PCAN-ExpressCard needs some additional i2c init. | ||
646 | * This must be done *before* register_sja1000dev() but | ||
647 | * *after* devices linkage | ||
648 | */ | ||
649 | if (pdev->device == PEAK_PCIEC_DEVICE_ID) { | ||
650 | err = peak_pciec_probe(pdev, dev); | ||
651 | if (err) { | ||
652 | dev_err(&pdev->dev, | ||
653 | "failed to probe device (err %d)\n", | ||
654 | err); | ||
655 | goto failure_free_dev; | ||
656 | } | ||
657 | } | ||
658 | |||
191 | err = register_sja1000dev(dev); | 659 | err = register_sja1000dev(dev); |
192 | if (err) { | 660 | if (err) { |
193 | dev_err(&pdev->dev, "failed to register device\n"); | 661 | dev_err(&pdev->dev, "failed to register device\n"); |
194 | free_sja1000dev(dev); | 662 | goto failure_free_dev; |
195 | goto failure_remove_channels; | ||
196 | } | 663 | } |
197 | 664 | ||
198 | /* Create chain of SJA1000 devices */ | ||
199 | chan->prev_dev = pci_get_drvdata(pdev); | ||
200 | pci_set_drvdata(pdev, dev); | ||
201 | |||
202 | dev_info(&pdev->dev, | 665 | dev_info(&pdev->dev, |
203 | "%s at reg_base=0x%p cfg_base=0x%p irq=%d\n", | 666 | "%s at reg_base=0x%p cfg_base=0x%p irq=%d\n", |
204 | dev->name, priv->reg_base, chan->cfg_base, dev->irq); | 667 | dev->name, priv->reg_base, chan->cfg_base, dev->irq); |
@@ -209,10 +672,15 @@ static int __devinit peak_pci_probe(struct pci_dev *pdev, | |||
209 | 672 | ||
210 | return 0; | 673 | return 0; |
211 | 674 | ||
675 | failure_free_dev: | ||
676 | pci_set_drvdata(pdev, chan->prev_dev); | ||
677 | free_sja1000dev(dev); | ||
678 | |||
212 | failure_remove_channels: | 679 | failure_remove_channels: |
213 | /* Disable interrupts */ | 680 | /* Disable interrupts */ |
214 | writew(0x0, cfg_base + PITA_ICR + 2); | 681 | writew(0x0, cfg_base + PITA_ICR + 2); |
215 | 682 | ||
683 | chan = NULL; | ||
216 | for (dev = pci_get_drvdata(pdev); dev; dev = chan->prev_dev) { | 684 | for (dev = pci_get_drvdata(pdev); dev; dev = chan->prev_dev) { |
217 | unregister_sja1000dev(dev); | 685 | unregister_sja1000dev(dev); |
218 | free_sja1000dev(dev); | 686 | free_sja1000dev(dev); |
@@ -220,6 +688,10 @@ failure_remove_channels: | |||
220 | chan = priv->priv; | 688 | chan = priv->priv; |
221 | } | 689 | } |
222 | 690 | ||
691 | /* free any PCIeC resources too */ | ||
692 | if (chan && chan->pciec_card) | ||
693 | peak_pciec_remove(chan->pciec_card); | ||
694 | |||
223 | pci_iounmap(pdev, reg_base); | 695 | pci_iounmap(pdev, reg_base); |
224 | 696 | ||
225 | failure_unmap_cfg_base: | 697 | failure_unmap_cfg_base: |
@@ -251,8 +723,13 @@ static void __devexit peak_pci_remove(struct pci_dev *pdev) | |||
251 | unregister_sja1000dev(dev); | 723 | unregister_sja1000dev(dev); |
252 | free_sja1000dev(dev); | 724 | free_sja1000dev(dev); |
253 | dev = chan->prev_dev; | 725 | dev = chan->prev_dev; |
254 | if (!dev) | 726 | |
727 | if (!dev) { | ||
728 | /* do that only for first channel */ | ||
729 | if (chan->pciec_card) | ||
730 | peak_pciec_remove(chan->pciec_card); | ||
255 | break; | 731 | break; |
732 | } | ||
256 | priv = netdev_priv(dev); | 733 | priv = netdev_priv(dev); |
257 | chan = priv->priv; | 734 | chan = priv->priv; |
258 | } | 735 | } |