2 comedi/drivers/jr3_pci.c
3 hardware driver for JR3/PCI force sensor board
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 2007 Anders Blomdell <anders.blomdell@control.lth.se>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * Description: JR3/PCI force sensor board
26 * Author: Anders Blomdell <anders.blomdell@control.lth.se>
27 * Updated: Thu, 01 Nov 2012 17:34:55 +0000
29 * Devices: [JR3] PCI force sensor board (jr3_pci)
31 * Configuration options:
34 * Manual configuration of comedi devices is not supported by this
35 * driver; supported PCI devices are configured as comedi devices
38 * The DSP on the board requires initialization code, which can be
39 * loaded by placing it in /lib/firmware/comedi. The initialization
40 * code should be somewhere on the media you got with your card. One
41 * version is available from http://www.comedi.org in the
42 * comedi_nonfree_firmware tarball. The file is called "jr3pci.idm".
45 #include <linux/kernel.h>
46 #include <linux/pci.h>
47 #include <linux/delay.h>
48 #include <linux/ctype.h>
49 #include <linux/firmware.h>
50 #include <linux/jiffies.h>
51 #include <linux/slab.h>
52 #include <linux/timer.h>
54 #include "../comedidev.h"
58 #define PCI_VENDOR_ID_JR3 0x1762
59 #define PCI_DEVICE_ID_JR3_1_CHANNEL 0x3111
60 #define PCI_DEVICE_ID_JR3_1_CHANNEL_NEW 0x1111
61 #define PCI_DEVICE_ID_JR3_2_CHANNEL 0x3112
62 #define PCI_DEVICE_ID_JR3_3_CHANNEL 0x3113
63 #define PCI_DEVICE_ID_JR3_4_CHANNEL 0x3114
65 struct jr3_pci_dev_private {
66 struct jr3_t __iomem *iobase;
68 struct timer_list timer;
76 struct jr3_pci_subdev_private {
77 struct jr3_channel __iomem *channel;
78 unsigned long next_time_min;
79 unsigned long next_time_max;
80 enum { state_jr3_poll,
81 state_jr3_init_wait_for_offset,
82 state_jr3_init_transform_complete,
83 state_jr3_init_set_full_scale_complete,
84 state_jr3_init_use_offset_complete,
92 struct comedi_krange range;
94 const struct comedi_lrange *range_table_list[8 * 7 + 2];
95 unsigned int maxdata_list[8 * 7 + 2];
100 /* Hotplug firmware loading stuff */
101 static int comedi_load_firmware(struct comedi_device *dev, const char *name,
102 int (*cb)(struct comedi_device *dev,
103 const u8 *data, size_t size))
105 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
107 const struct firmware *fw;
109 static const char *prefix = "comedi/";
111 firmware_path = kmalloc(strlen(prefix) + strlen(name) + 1, GFP_KERNEL);
112 if (!firmware_path) {
115 firmware_path[0] = '\0';
116 strcat(firmware_path, prefix);
117 strcat(firmware_path, name);
118 result = request_firmware(&fw, firmware_path, &pcidev->dev);
123 result = cb(dev, fw->data, fw->size);
124 release_firmware(fw);
126 kfree(firmware_path);
131 static struct poll_delay_t poll_delay_min_max(int min, int max)
133 struct poll_delay_t result;
140 static int is_complete(struct jr3_channel __iomem *channel)
142 return get_s16(&channel->command_word0) == 0;
152 static void set_transforms(struct jr3_channel __iomem *channel,
153 struct transform_t transf, short num)
157 num &= 0x000f; /* Make sure that 0 <= num <= 15 */
158 for (i = 0; i < 8; i++) {
159 set_u16(&channel->transforms[num].link[i].link_type,
160 transf.link[i].link_type);
162 set_s16(&channel->transforms[num].link[i].link_amount,
163 transf.link[i].link_amount);
165 if (transf.link[i].link_type == end_x_form)
170 static void use_transform(struct jr3_channel __iomem *channel,
173 set_s16(&channel->command_word0, 0x0500 + (transf_num & 0x000f));
176 static void use_offset(struct jr3_channel __iomem *channel, short offset_num)
178 set_s16(&channel->command_word0, 0x0600 + (offset_num & 0x000f));
181 static void set_offset(struct jr3_channel __iomem *channel)
183 set_s16(&channel->command_word0, 0x0700);
195 static void set_full_scales(struct jr3_channel __iomem *channel,
196 struct six_axis_t full_scale)
198 set_s16(&channel->full_scale.fx, full_scale.fx);
199 set_s16(&channel->full_scale.fy, full_scale.fy);
200 set_s16(&channel->full_scale.fz, full_scale.fz);
201 set_s16(&channel->full_scale.mx, full_scale.mx);
202 set_s16(&channel->full_scale.my, full_scale.my);
203 set_s16(&channel->full_scale.mz, full_scale.mz);
204 set_s16(&channel->command_word0, 0x0a00);
207 static struct six_axis_t get_min_full_scales(struct jr3_channel __iomem
210 struct six_axis_t result;
211 result.fx = get_s16(&channel->min_full_scale.fx);
212 result.fy = get_s16(&channel->min_full_scale.fy);
213 result.fz = get_s16(&channel->min_full_scale.fz);
214 result.mx = get_s16(&channel->min_full_scale.mx);
215 result.my = get_s16(&channel->min_full_scale.my);
216 result.mz = get_s16(&channel->min_full_scale.mz);
220 static struct six_axis_t get_max_full_scales(struct jr3_channel __iomem
223 struct six_axis_t result;
224 result.fx = get_s16(&channel->max_full_scale.fx);
225 result.fy = get_s16(&channel->max_full_scale.fy);
226 result.fz = get_s16(&channel->max_full_scale.fz);
227 result.mx = get_s16(&channel->max_full_scale.mx);
228 result.my = get_s16(&channel->max_full_scale.my);
229 result.mz = get_s16(&channel->max_full_scale.mz);
233 static int jr3_pci_ai_insn_read(struct comedi_device *dev,
234 struct comedi_subdevice *s,
235 struct comedi_insn *insn, unsigned int *data)
238 struct jr3_pci_subdev_private *p;
242 channel = CR_CHAN(insn->chanspec);
243 if (p == NULL || channel > 57) {
249 if (p->state != state_jr3_done ||
250 (get_u16(&p->channel->errors) & (watch_dog | watch_dog2 |
252 /* No sensor or sensor changed */
253 if (p->state == state_jr3_done) {
254 /* Restart polling */
255 p->state = state_jr3_poll;
259 for (i = 0; i < insn->n; i++) {
264 filter = channel / 8;
265 if (p->state != state_jr3_done) {
271 F = get_s16(&p->channel->
275 F = get_s16(&p->channel->
279 F = get_s16(&p->channel->
283 F = get_s16(&p->channel->
287 F = get_s16(&p->channel->
291 F = get_s16(&p->channel->
295 F = get_s16(&p->channel->
299 F = get_s16(&p->channel->
303 data[i] = F + 0x4000;
305 } else if (channel == 56) {
306 if (p->state != state_jr3_done)
310 get_u16(&p->channel->model_no);
311 } else if (channel == 57) {
312 if (p->state != state_jr3_done)
316 get_u16(&p->channel->serial_no);
323 static int jr3_pci_open(struct comedi_device *dev)
326 struct jr3_pci_dev_private *devpriv = dev->private;
328 dev_dbg(dev->class_dev, "jr3_pci_open\n");
329 for (i = 0; i < devpriv->n_channels; i++) {
330 struct jr3_pci_subdev_private *p;
332 p = dev->subdevices[i].private;
334 dev_dbg(dev->class_dev, "serial: %p %d (%d)\n", p,
335 p->serial_no, p->channel_no);
341 static int read_idm_word(const u8 *data, size_t size, int *pos,
346 /* Skip over non hex */
347 for (; *pos < size && !isxdigit(data[*pos]); (*pos)++)
351 for (; *pos < size; (*pos)++) {
353 value = hex_to_bin(data[*pos]);
356 *val = (*val << 4) + value;
365 static int jr3_download_firmware(struct comedi_device *dev, const u8 *data,
369 * IDM file format is:
370 * { count, address, data <count> } *
373 int result, more, pos, OK;
380 unsigned int count, addr;
382 more = more && read_idm_word(data, size, &pos, &count);
383 if (more && count == 0xffff) {
387 more = more && read_idm_word(data, size, &pos, &addr);
388 while (more && count > 0) {
390 more = more && read_idm_word(data, size, &pos, &dummy);
399 struct jr3_pci_dev_private *p = dev->private;
401 for (i = 0; i < p->n_channels; i++) {
402 struct jr3_pci_subdev_private *sp;
404 sp = dev->subdevices[i].private;
408 unsigned int count, addr;
410 read_idm_word(data, size, &pos, &count);
411 if (more && count == 0xffff)
414 read_idm_word(data, size, &pos, &addr);
415 dev_dbg(dev->class_dev,
416 "Loading#%d %4.4x bytes at %4.4x\n",
418 while (more && count > 0) {
420 /* 16 bit data, never seen
429 /* jr3[addr + 0x20000 * pnum] =
432 /* Download 24 bit program */
433 unsigned int data1, data2;
440 read_idm_word(data, size,
465 static struct poll_delay_t jr3_pci_poll_subdevice(struct comedi_subdevice *s)
467 struct poll_delay_t result = poll_delay_min_max(1000, 2000);
468 struct jr3_pci_subdev_private *p = s->private;
472 struct jr3_channel __iomem *channel = p->channel;
473 int errors = get_u16(&channel->errors);
475 if (errors != p->errors)
478 if (errors & (watch_dog | watch_dog2 | sensor_change))
479 /* Sensor communication lost, force poll mode */
480 p->state = state_jr3_poll;
483 case state_jr3_poll: {
484 u16 model_no = get_u16(&channel->model_no);
485 u16 serial_no = get_u16(&channel->serial_no);
486 if ((errors & (watch_dog | watch_dog2)) ||
487 model_no == 0 || serial_no == 0) {
489 * Still no sensor, keep on polling.
490 * Since it takes up to 10 seconds
491 * for offsets to stabilize, polling
492 * each second should suffice.
494 result = poll_delay_min_max(1000, 2000);
498 state_jr3_init_wait_for_offset;
499 result = poll_delay_min_max(1000, 2000);
503 case state_jr3_init_wait_for_offset:
505 if (p->retries < 10) {
506 /* Wait for offeset to stabilize
507 * (< 10 s according to manual) */
508 result = poll_delay_min_max(1000, 2000);
510 struct transform_t transf;
512 p->model_no = get_u16(&channel->model_no);
513 p->serial_no = get_u16(&channel->serial_no);
515 /* Transformation all zeros */
516 for (i = 0; i < ARRAY_SIZE(transf.link); i++) {
517 transf.link[i].link_type =
519 transf.link[i].link_amount = 0;
522 set_transforms(channel, transf, 0);
523 use_transform(channel, 0);
524 p->state = state_jr3_init_transform_complete;
525 /* Allow 20 ms for completion */
526 result = poll_delay_min_max(20, 100);
529 case state_jr3_init_transform_complete:
530 if (!is_complete(channel)) {
531 result = poll_delay_min_max(20, 100);
534 struct six_axis_t min_full_scale;
535 struct six_axis_t max_full_scale;
537 min_full_scale = get_min_full_scales(channel);
538 max_full_scale = get_max_full_scales(channel);
539 set_full_scales(channel, max_full_scale);
542 state_jr3_init_set_full_scale_complete;
543 /* Allow 20 ms for completion */
544 result = poll_delay_min_max(20, 100);
547 case state_jr3_init_set_full_scale_complete:
548 if (!is_complete(channel)) {
549 result = poll_delay_min_max(20, 100);
551 struct force_array __iomem *full_scale;
553 /* Use ranges in kN or we will
554 * overflow around 2000N! */
555 full_scale = &channel->full_scale;
556 p->range[0].range.min =
557 -get_s16(&full_scale->fx) * 1000;
558 p->range[0].range.max =
559 get_s16(&full_scale->fx) * 1000;
560 p->range[1].range.min =
561 -get_s16(&full_scale->fy) * 1000;
562 p->range[1].range.max =
563 get_s16(&full_scale->fy) * 1000;
564 p->range[2].range.min =
565 -get_s16(&full_scale->fz) * 1000;
566 p->range[2].range.max =
567 get_s16(&full_scale->fz) * 1000;
568 p->range[3].range.min =
569 -get_s16(&full_scale->mx) * 100;
570 p->range[3].range.max =
571 get_s16(&full_scale->mx) * 100;
572 p->range[4].range.min =
573 -get_s16(&full_scale->my) * 100;
574 p->range[4].range.max =
575 get_s16(&full_scale->my) * 100;
576 p->range[5].range.min =
577 -get_s16(&full_scale->mz) * 100;
578 p->range[5].range.max =
579 get_s16(&full_scale->mz) * 100; /* ?? */
580 p->range[6].range.min =
581 -get_s16(&full_scale->v1) * 100;/* ?? */
582 p->range[6].range.max =
583 get_s16(&full_scale->v1) * 100; /* ?? */
584 p->range[7].range.min =
585 -get_s16(&full_scale->v2) * 100;/* ?? */
586 p->range[7].range.max =
587 get_s16(&full_scale->v2) * 100; /* ?? */
588 p->range[8].range.min = 0;
589 p->range[8].range.max = 65535;
591 use_offset(channel, 0);
592 p->state = state_jr3_init_use_offset_complete;
593 /* Allow 40 ms for completion */
594 result = poll_delay_min_max(40, 100);
597 case state_jr3_init_use_offset_complete:
598 if (!is_complete(channel)) {
599 result = poll_delay_min_max(20, 100);
601 set_s16(&channel->offsets.fx, 0);
602 set_s16(&channel->offsets.fy, 0);
603 set_s16(&channel->offsets.fz, 0);
604 set_s16(&channel->offsets.mx, 0);
605 set_s16(&channel->offsets.my, 0);
606 set_s16(&channel->offsets.mz, 0);
610 p->state = state_jr3_done;
614 poll_delay_min_max(10000, 20000);
617 poll_delay_min_max(1000, 2000);
624 static void jr3_pci_poll_dev(unsigned long data)
627 struct comedi_device *dev = (struct comedi_device *)data;
628 struct jr3_pci_dev_private *devpriv = dev->private;
633 spin_lock_irqsave(&dev->spinlock, flags);
636 /* Poll all channels that are ready to be polled */
637 for (i = 0; i < devpriv->n_channels; i++) {
638 struct jr3_pci_subdev_private *subdevpriv =
639 dev->subdevices[i].private;
640 if (now > subdevpriv->next_time_min) {
641 struct poll_delay_t sub_delay;
643 sub_delay = jr3_pci_poll_subdevice(&dev->subdevices[i]);
644 subdevpriv->next_time_min =
645 jiffies + msecs_to_jiffies(sub_delay.min);
646 subdevpriv->next_time_max =
647 jiffies + msecs_to_jiffies(sub_delay.max);
648 if (sub_delay.max && sub_delay.max < delay)
650 * Wake up as late as possible ->
651 * poll as many channels as possible at once.
653 delay = sub_delay.max;
656 spin_unlock_irqrestore(&dev->spinlock, flags);
658 devpriv->timer.expires = jiffies + msecs_to_jiffies(delay);
659 add_timer(&devpriv->timer);
662 static int jr3_pci_auto_attach(struct comedi_device *dev,
663 unsigned long context_unused)
666 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
668 struct jr3_pci_dev_private *devpriv;
670 if (sizeof(struct jr3_channel) != 0xc00) {
671 dev_err(dev->class_dev,
672 "sizeof(struct jr3_channel) = %x [expected %x]\n",
673 (unsigned)sizeof(struct jr3_channel), 0xc00);
677 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
680 dev->private = devpriv;
682 init_timer(&devpriv->timer);
683 switch (pcidev->device) {
684 case PCI_DEVICE_ID_JR3_1_CHANNEL:
685 case PCI_DEVICE_ID_JR3_1_CHANNEL_NEW:
686 devpriv->n_channels = 1;
688 case PCI_DEVICE_ID_JR3_2_CHANNEL:
689 devpriv->n_channels = 2;
691 case PCI_DEVICE_ID_JR3_3_CHANNEL:
692 devpriv->n_channels = 3;
694 case PCI_DEVICE_ID_JR3_4_CHANNEL:
695 devpriv->n_channels = 4;
698 dev_err(dev->class_dev, "jr3_pci: pci %s not supported\n",
703 dev->board_name = "jr3_pci";
705 result = comedi_pci_enable(pcidev, "jr3_pci");
709 dev->iobase = 1; /* the "detach" needs this */
710 devpriv->iobase = ioremap(pci_resource_start(pcidev, 0),
711 offsetof(struct jr3_t,
712 channel[devpriv->n_channels]));
713 if (!devpriv->iobase)
716 result = comedi_alloc_subdevices(dev, devpriv->n_channels);
720 dev->open = jr3_pci_open;
721 for (i = 0; i < devpriv->n_channels; i++) {
722 dev->subdevices[i].type = COMEDI_SUBD_AI;
723 dev->subdevices[i].subdev_flags = SDF_READABLE | SDF_GROUND;
724 dev->subdevices[i].n_chan = 8 * 7 + 2;
725 dev->subdevices[i].insn_read = jr3_pci_ai_insn_read;
726 dev->subdevices[i].private =
727 kzalloc(sizeof(struct jr3_pci_subdev_private),
729 if (dev->subdevices[i].private) {
730 struct jr3_pci_subdev_private *p;
733 p = dev->subdevices[i].private;
734 p->channel = &devpriv->iobase->channel[i].data;
735 dev_dbg(dev->class_dev, "p->channel %p %p (%tx)\n",
736 p->channel, devpriv->iobase,
737 ((char __iomem *)p->channel -
738 (char __iomem *)devpriv->iobase));
740 for (j = 0; j < 8; j++) {
743 p->range[j].length = 1;
744 p->range[j].range.min = -1000000;
745 p->range[j].range.max = 1000000;
746 for (k = 0; k < 7; k++) {
747 p->range_table_list[j + k * 8] =
748 (struct comedi_lrange *)&p->
750 p->maxdata_list[j + k * 8] = 0x7fff;
753 p->range[8].length = 1;
754 p->range[8].range.min = 0;
755 p->range[8].range.max = 65536;
757 p->range_table_list[56] =
758 (struct comedi_lrange *)&p->range[8];
759 p->range_table_list[57] =
760 (struct comedi_lrange *)&p->range[8];
761 p->maxdata_list[56] = 0xffff;
762 p->maxdata_list[57] = 0xffff;
763 /* Channel specific range and maxdata */
764 dev->subdevices[i].range_table = NULL;
765 dev->subdevices[i].range_table_list =
767 dev->subdevices[i].maxdata = 0;
768 dev->subdevices[i].maxdata_list = p->maxdata_list;
773 writel(0, &devpriv->iobase->channel[0].reset);
775 result = comedi_load_firmware(dev, "jr3pci.idm", jr3_download_firmware);
776 dev_dbg(dev->class_dev, "Firmare load %d\n", result);
781 * TODO: use firmware to load preferred offset tables. Suggested
783 * model serial Fx Fy Fz Mx My Mz\n
785 * comedi_load_firmware(dev, "jr3_offsets_table",
786 * jr3_download_firmware);
790 * It takes a few milliseconds for software to settle as much as we
791 * can read firmware version
793 msleep_interruptible(25);
794 for (i = 0; i < 0x18; i++) {
795 dev_dbg(dev->class_dev, "%c\n",
796 get_u16(&devpriv->iobase->channel[0].
797 data.copyright[i]) >> 8);
800 /* Start card timer */
801 for (i = 0; i < devpriv->n_channels; i++) {
802 struct jr3_pci_subdev_private *p = dev->subdevices[i].private;
804 p->next_time_min = jiffies + msecs_to_jiffies(500);
805 p->next_time_max = jiffies + msecs_to_jiffies(2000);
808 devpriv->timer.data = (unsigned long)dev;
809 devpriv->timer.function = jr3_pci_poll_dev;
810 devpriv->timer.expires = jiffies + msecs_to_jiffies(1000);
811 add_timer(&devpriv->timer);
816 static void jr3_pci_detach(struct comedi_device *dev)
819 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
820 struct jr3_pci_dev_private *devpriv = dev->private;
823 del_timer_sync(&devpriv->timer);
825 if (dev->subdevices) {
826 for (i = 0; i < devpriv->n_channels; i++)
827 kfree(dev->subdevices[i].private);
830 iounmap(devpriv->iobase);
832 comedi_pci_disable(pcidev);
836 static struct comedi_driver jr3_pci_driver = {
837 .driver_name = "jr3_pci",
838 .module = THIS_MODULE,
839 .auto_attach = jr3_pci_auto_attach,
840 .detach = jr3_pci_detach,
843 static int jr3_pci_pci_probe(struct pci_dev *dev,
844 const struct pci_device_id *ent)
846 return comedi_pci_auto_config(dev, &jr3_pci_driver);
849 static DEFINE_PCI_DEVICE_TABLE(jr3_pci_pci_table) = {
850 { PCI_DEVICE(PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_1_CHANNEL) },
851 { PCI_DEVICE(PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_1_CHANNEL_NEW) },
852 { PCI_DEVICE(PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_2_CHANNEL) },
853 { PCI_DEVICE(PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_3_CHANNEL) },
854 { PCI_DEVICE(PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_4_CHANNEL) },
857 MODULE_DEVICE_TABLE(pci, jr3_pci_pci_table);
859 static struct pci_driver jr3_pci_pci_driver = {
861 .id_table = jr3_pci_pci_table,
862 .probe = jr3_pci_pci_probe,
863 .remove = comedi_pci_auto_unconfig,
865 module_comedi_pci_driver(jr3_pci_driver, jr3_pci_pci_driver);
867 MODULE_AUTHOR("Comedi http://www.comedi.org");
868 MODULE_DESCRIPTION("Comedi low-level driver");
869 MODULE_LICENSE("GPL");
870 MODULE_FIRMWARE("comedi/jr3pci.idm");