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.
20 * Description: JR3/PCI force sensor board
21 * Author: Anders Blomdell <anders.blomdell@control.lth.se>
22 * Updated: Thu, 01 Nov 2012 17:34:55 +0000
24 * Devices: [JR3] PCI force sensor board (jr3_pci)
26 * Configuration options:
29 * Manual configuration of comedi devices is not supported by this
30 * driver; supported PCI devices are configured as comedi devices
33 * The DSP on the board requires initialization code, which can be
34 * loaded by placing it in /lib/firmware/comedi. The initialization
35 * code should be somewhere on the media you got with your card. One
36 * version is available from http://www.comedi.org in the
37 * comedi_nonfree_firmware tarball. The file is called "jr3pci.idm".
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/pci.h>
43 #include <linux/delay.h>
44 #include <linux/ctype.h>
45 #include <linux/jiffies.h>
46 #include <linux/slab.h>
47 #include <linux/timer.h>
49 #include "../comedidev.h"
53 #define PCI_VENDOR_ID_JR3 0x1762
54 #define PCI_DEVICE_ID_JR3_1_CHANNEL 0x3111
55 #define PCI_DEVICE_ID_JR3_1_CHANNEL_NEW 0x1111
56 #define PCI_DEVICE_ID_JR3_2_CHANNEL 0x3112
57 #define PCI_DEVICE_ID_JR3_3_CHANNEL 0x3113
58 #define PCI_DEVICE_ID_JR3_4_CHANNEL 0x3114
60 struct jr3_pci_dev_private {
61 struct jr3_t __iomem *iobase;
63 struct timer_list timer;
71 struct jr3_pci_subdev_private {
72 struct jr3_channel __iomem *channel;
73 unsigned long next_time_min;
74 unsigned long next_time_max;
75 enum { state_jr3_poll,
76 state_jr3_init_wait_for_offset,
77 state_jr3_init_transform_complete,
78 state_jr3_init_set_full_scale_complete,
79 state_jr3_init_use_offset_complete,
87 struct comedi_krange range;
89 const struct comedi_lrange *range_table_list[8 * 7 + 2];
90 unsigned int maxdata_list[8 * 7 + 2];
95 static struct poll_delay_t poll_delay_min_max(int min, int max)
97 struct poll_delay_t result;
104 static int is_complete(struct jr3_channel __iomem *channel)
106 return get_s16(&channel->command_word0) == 0;
116 static void set_transforms(struct jr3_channel __iomem *channel,
117 struct transform_t transf, short num)
121 num &= 0x000f; /* Make sure that 0 <= num <= 15 */
122 for (i = 0; i < 8; i++) {
123 set_u16(&channel->transforms[num].link[i].link_type,
124 transf.link[i].link_type);
126 set_s16(&channel->transforms[num].link[i].link_amount,
127 transf.link[i].link_amount);
129 if (transf.link[i].link_type == end_x_form)
134 static void use_transform(struct jr3_channel __iomem *channel,
137 set_s16(&channel->command_word0, 0x0500 + (transf_num & 0x000f));
140 static void use_offset(struct jr3_channel __iomem *channel, short offset_num)
142 set_s16(&channel->command_word0, 0x0600 + (offset_num & 0x000f));
145 static void set_offset(struct jr3_channel __iomem *channel)
147 set_s16(&channel->command_word0, 0x0700);
159 static void set_full_scales(struct jr3_channel __iomem *channel,
160 struct six_axis_t full_scale)
162 set_s16(&channel->full_scale.fx, full_scale.fx);
163 set_s16(&channel->full_scale.fy, full_scale.fy);
164 set_s16(&channel->full_scale.fz, full_scale.fz);
165 set_s16(&channel->full_scale.mx, full_scale.mx);
166 set_s16(&channel->full_scale.my, full_scale.my);
167 set_s16(&channel->full_scale.mz, full_scale.mz);
168 set_s16(&channel->command_word0, 0x0a00);
171 static struct six_axis_t get_min_full_scales(struct jr3_channel __iomem
174 struct six_axis_t result;
175 result.fx = get_s16(&channel->min_full_scale.fx);
176 result.fy = get_s16(&channel->min_full_scale.fy);
177 result.fz = get_s16(&channel->min_full_scale.fz);
178 result.mx = get_s16(&channel->min_full_scale.mx);
179 result.my = get_s16(&channel->min_full_scale.my);
180 result.mz = get_s16(&channel->min_full_scale.mz);
184 static struct six_axis_t get_max_full_scales(struct jr3_channel __iomem
187 struct six_axis_t result;
188 result.fx = get_s16(&channel->max_full_scale.fx);
189 result.fy = get_s16(&channel->max_full_scale.fy);
190 result.fz = get_s16(&channel->max_full_scale.fz);
191 result.mx = get_s16(&channel->max_full_scale.mx);
192 result.my = get_s16(&channel->max_full_scale.my);
193 result.mz = get_s16(&channel->max_full_scale.mz);
197 static int jr3_pci_ai_insn_read(struct comedi_device *dev,
198 struct comedi_subdevice *s,
199 struct comedi_insn *insn, unsigned int *data)
202 struct jr3_pci_subdev_private *p;
206 channel = CR_CHAN(insn->chanspec);
207 if (p == NULL || channel > 57) {
213 if (p->state != state_jr3_done ||
214 (get_u16(&p->channel->errors) & (watch_dog | watch_dog2 |
216 /* No sensor or sensor changed */
217 if (p->state == state_jr3_done) {
218 /* Restart polling */
219 p->state = state_jr3_poll;
223 for (i = 0; i < insn->n; i++) {
228 filter = channel / 8;
229 if (p->state != state_jr3_done) {
235 F = get_s16(&p->channel->
239 F = get_s16(&p->channel->
243 F = get_s16(&p->channel->
247 F = get_s16(&p->channel->
251 F = get_s16(&p->channel->
255 F = get_s16(&p->channel->
259 F = get_s16(&p->channel->
263 F = get_s16(&p->channel->
267 data[i] = F + 0x4000;
269 } else if (channel == 56) {
270 if (p->state != state_jr3_done)
274 get_u16(&p->channel->model_no);
275 } else if (channel == 57) {
276 if (p->state != state_jr3_done)
280 get_u16(&p->channel->serial_no);
287 static int jr3_pci_open(struct comedi_device *dev)
290 struct jr3_pci_dev_private *devpriv = dev->private;
292 dev_dbg(dev->class_dev, "jr3_pci_open\n");
293 for (i = 0; i < devpriv->n_channels; i++) {
294 struct jr3_pci_subdev_private *p;
296 p = dev->subdevices[i].private;
298 dev_dbg(dev->class_dev, "serial: %p %d (%d)\n", p,
299 p->serial_no, p->channel_no);
305 static int read_idm_word(const u8 *data, size_t size, int *pos,
310 /* Skip over non hex */
311 for (; *pos < size && !isxdigit(data[*pos]); (*pos)++)
315 for (; *pos < size; (*pos)++) {
317 value = hex_to_bin(data[*pos]);
320 *val = (*val << 4) + value;
329 static int jr3_download_firmware(struct comedi_device *dev,
330 const u8 *data, size_t size,
331 unsigned long context)
334 * IDM file format is:
335 * { count, address, data <count> } *
338 int result, more, pos, OK;
345 unsigned int count, addr;
347 more = more && read_idm_word(data, size, &pos, &count);
348 if (more && count == 0xffff) {
352 more = more && read_idm_word(data, size, &pos, &addr);
353 while (more && count > 0) {
355 more = more && read_idm_word(data, size, &pos, &dummy);
364 struct jr3_pci_dev_private *p = dev->private;
366 for (i = 0; i < p->n_channels; i++) {
367 struct jr3_pci_subdev_private *sp;
369 sp = dev->subdevices[i].private;
373 unsigned int count, addr;
375 read_idm_word(data, size, &pos, &count);
376 if (more && count == 0xffff)
379 read_idm_word(data, size, &pos, &addr);
380 dev_dbg(dev->class_dev,
381 "Loading#%d %4.4x bytes at %4.4x\n",
383 while (more && count > 0) {
385 /* 16 bit data, never seen
394 /* jr3[addr + 0x20000 * pnum] =
397 /* Download 24 bit program */
398 unsigned int data1, data2;
405 read_idm_word(data, size,
430 static struct poll_delay_t jr3_pci_poll_subdevice(struct comedi_subdevice *s)
432 struct poll_delay_t result = poll_delay_min_max(1000, 2000);
433 struct jr3_pci_subdev_private *p = s->private;
437 struct jr3_channel __iomem *channel = p->channel;
438 int errors = get_u16(&channel->errors);
440 if (errors != p->errors)
443 if (errors & (watch_dog | watch_dog2 | sensor_change))
444 /* Sensor communication lost, force poll mode */
445 p->state = state_jr3_poll;
448 case state_jr3_poll: {
449 u16 model_no = get_u16(&channel->model_no);
450 u16 serial_no = get_u16(&channel->serial_no);
451 if ((errors & (watch_dog | watch_dog2)) ||
452 model_no == 0 || serial_no == 0) {
454 * Still no sensor, keep on polling.
455 * Since it takes up to 10 seconds
456 * for offsets to stabilize, polling
457 * each second should suffice.
459 result = poll_delay_min_max(1000, 2000);
463 state_jr3_init_wait_for_offset;
464 result = poll_delay_min_max(1000, 2000);
468 case state_jr3_init_wait_for_offset:
470 if (p->retries < 10) {
471 /* Wait for offeset to stabilize
472 * (< 10 s according to manual) */
473 result = poll_delay_min_max(1000, 2000);
475 struct transform_t transf;
477 p->model_no = get_u16(&channel->model_no);
478 p->serial_no = get_u16(&channel->serial_no);
480 /* Transformation all zeros */
481 for (i = 0; i < ARRAY_SIZE(transf.link); i++) {
482 transf.link[i].link_type =
484 transf.link[i].link_amount = 0;
487 set_transforms(channel, transf, 0);
488 use_transform(channel, 0);
489 p->state = state_jr3_init_transform_complete;
490 /* Allow 20 ms for completion */
491 result = poll_delay_min_max(20, 100);
494 case state_jr3_init_transform_complete:
495 if (!is_complete(channel)) {
496 result = poll_delay_min_max(20, 100);
499 struct six_axis_t min_full_scale;
500 struct six_axis_t max_full_scale;
502 min_full_scale = get_min_full_scales(channel);
503 max_full_scale = get_max_full_scales(channel);
504 set_full_scales(channel, max_full_scale);
507 state_jr3_init_set_full_scale_complete;
508 /* Allow 20 ms for completion */
509 result = poll_delay_min_max(20, 100);
512 case state_jr3_init_set_full_scale_complete:
513 if (!is_complete(channel)) {
514 result = poll_delay_min_max(20, 100);
516 struct force_array __iomem *full_scale;
518 /* Use ranges in kN or we will
519 * overflow around 2000N! */
520 full_scale = &channel->full_scale;
521 p->range[0].range.min =
522 -get_s16(&full_scale->fx) * 1000;
523 p->range[0].range.max =
524 get_s16(&full_scale->fx) * 1000;
525 p->range[1].range.min =
526 -get_s16(&full_scale->fy) * 1000;
527 p->range[1].range.max =
528 get_s16(&full_scale->fy) * 1000;
529 p->range[2].range.min =
530 -get_s16(&full_scale->fz) * 1000;
531 p->range[2].range.max =
532 get_s16(&full_scale->fz) * 1000;
533 p->range[3].range.min =
534 -get_s16(&full_scale->mx) * 100;
535 p->range[3].range.max =
536 get_s16(&full_scale->mx) * 100;
537 p->range[4].range.min =
538 -get_s16(&full_scale->my) * 100;
539 p->range[4].range.max =
540 get_s16(&full_scale->my) * 100;
541 p->range[5].range.min =
542 -get_s16(&full_scale->mz) * 100;
543 p->range[5].range.max =
544 get_s16(&full_scale->mz) * 100; /* ?? */
545 p->range[6].range.min =
546 -get_s16(&full_scale->v1) * 100;/* ?? */
547 p->range[6].range.max =
548 get_s16(&full_scale->v1) * 100; /* ?? */
549 p->range[7].range.min =
550 -get_s16(&full_scale->v2) * 100;/* ?? */
551 p->range[7].range.max =
552 get_s16(&full_scale->v2) * 100; /* ?? */
553 p->range[8].range.min = 0;
554 p->range[8].range.max = 65535;
556 use_offset(channel, 0);
557 p->state = state_jr3_init_use_offset_complete;
558 /* Allow 40 ms for completion */
559 result = poll_delay_min_max(40, 100);
562 case state_jr3_init_use_offset_complete:
563 if (!is_complete(channel)) {
564 result = poll_delay_min_max(20, 100);
566 set_s16(&channel->offsets.fx, 0);
567 set_s16(&channel->offsets.fy, 0);
568 set_s16(&channel->offsets.fz, 0);
569 set_s16(&channel->offsets.mx, 0);
570 set_s16(&channel->offsets.my, 0);
571 set_s16(&channel->offsets.mz, 0);
575 p->state = state_jr3_done;
579 poll_delay_min_max(10000, 20000);
582 poll_delay_min_max(1000, 2000);
589 static void jr3_pci_poll_dev(unsigned long data)
592 struct comedi_device *dev = (struct comedi_device *)data;
593 struct jr3_pci_dev_private *devpriv = dev->private;
598 spin_lock_irqsave(&dev->spinlock, flags);
601 /* Poll all channels that are ready to be polled */
602 for (i = 0; i < devpriv->n_channels; i++) {
603 struct jr3_pci_subdev_private *subdevpriv =
604 dev->subdevices[i].private;
605 if (now > subdevpriv->next_time_min) {
606 struct poll_delay_t sub_delay;
608 sub_delay = jr3_pci_poll_subdevice(&dev->subdevices[i]);
609 subdevpriv->next_time_min =
610 jiffies + msecs_to_jiffies(sub_delay.min);
611 subdevpriv->next_time_max =
612 jiffies + msecs_to_jiffies(sub_delay.max);
613 if (sub_delay.max && sub_delay.max < delay)
615 * Wake up as late as possible ->
616 * poll as many channels as possible at once.
618 delay = sub_delay.max;
621 spin_unlock_irqrestore(&dev->spinlock, flags);
623 devpriv->timer.expires = jiffies + msecs_to_jiffies(delay);
624 add_timer(&devpriv->timer);
627 static int jr3_pci_auto_attach(struct comedi_device *dev,
628 unsigned long context_unused)
631 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
633 struct jr3_pci_dev_private *devpriv;
635 if (sizeof(struct jr3_channel) != 0xc00) {
636 dev_err(dev->class_dev,
637 "sizeof(struct jr3_channel) = %x [expected %x]\n",
638 (unsigned)sizeof(struct jr3_channel), 0xc00);
642 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
646 init_timer(&devpriv->timer);
647 switch (pcidev->device) {
648 case PCI_DEVICE_ID_JR3_1_CHANNEL:
649 case PCI_DEVICE_ID_JR3_1_CHANNEL_NEW:
650 devpriv->n_channels = 1;
652 case PCI_DEVICE_ID_JR3_2_CHANNEL:
653 devpriv->n_channels = 2;
655 case PCI_DEVICE_ID_JR3_3_CHANNEL:
656 devpriv->n_channels = 3;
658 case PCI_DEVICE_ID_JR3_4_CHANNEL:
659 devpriv->n_channels = 4;
662 dev_err(dev->class_dev, "jr3_pci: pci %s not supported\n",
668 result = comedi_pci_enable(dev);
672 devpriv->iobase = pci_ioremap_bar(pcidev, 0);
673 if (!devpriv->iobase)
676 result = comedi_alloc_subdevices(dev, devpriv->n_channels);
680 dev->open = jr3_pci_open;
681 for (i = 0; i < devpriv->n_channels; i++) {
682 dev->subdevices[i].type = COMEDI_SUBD_AI;
683 dev->subdevices[i].subdev_flags = SDF_READABLE | SDF_GROUND;
684 dev->subdevices[i].n_chan = 8 * 7 + 2;
685 dev->subdevices[i].insn_read = jr3_pci_ai_insn_read;
686 dev->subdevices[i].private =
687 kzalloc(sizeof(struct jr3_pci_subdev_private),
689 if (dev->subdevices[i].private) {
690 struct jr3_pci_subdev_private *p;
693 p = dev->subdevices[i].private;
694 p->channel = &devpriv->iobase->channel[i].data;
695 dev_dbg(dev->class_dev, "p->channel %p %p (%tx)\n",
696 p->channel, devpriv->iobase,
697 ((char __iomem *)p->channel -
698 (char __iomem *)devpriv->iobase));
700 for (j = 0; j < 8; j++) {
703 p->range[j].length = 1;
704 p->range[j].range.min = -1000000;
705 p->range[j].range.max = 1000000;
706 for (k = 0; k < 7; k++) {
707 p->range_table_list[j + k * 8] =
708 (struct comedi_lrange *)&p->
710 p->maxdata_list[j + k * 8] = 0x7fff;
713 p->range[8].length = 1;
714 p->range[8].range.min = 0;
715 p->range[8].range.max = 65536;
717 p->range_table_list[56] =
718 (struct comedi_lrange *)&p->range[8];
719 p->range_table_list[57] =
720 (struct comedi_lrange *)&p->range[8];
721 p->maxdata_list[56] = 0xffff;
722 p->maxdata_list[57] = 0xffff;
723 /* Channel specific range and maxdata */
724 dev->subdevices[i].range_table = NULL;
725 dev->subdevices[i].range_table_list =
727 dev->subdevices[i].maxdata = 0;
728 dev->subdevices[i].maxdata_list = p->maxdata_list;
733 writel(0, &devpriv->iobase->channel[0].reset);
735 result = comedi_load_firmware(dev, &comedi_to_pci_dev(dev)->dev,
737 jr3_download_firmware, 0);
738 dev_dbg(dev->class_dev, "Firmare load %d\n", result);
743 * TODO: use firmware to load preferred offset tables. Suggested
745 * model serial Fx Fy Fz Mx My Mz\n
747 * comedi_load_firmware(dev, &comedi_to_pci_dev(dev)->dev,
748 * "comedi/jr3_offsets_table",
749 * jr3_download_firmware, 1);
753 * It takes a few milliseconds for software to settle as much as we
754 * can read firmware version
756 msleep_interruptible(25);
757 for (i = 0; i < 0x18; i++) {
758 dev_dbg(dev->class_dev, "%c\n",
759 get_u16(&devpriv->iobase->channel[0].
760 data.copyright[i]) >> 8);
763 /* Start card timer */
764 for (i = 0; i < devpriv->n_channels; i++) {
765 struct jr3_pci_subdev_private *p = dev->subdevices[i].private;
767 p->next_time_min = jiffies + msecs_to_jiffies(500);
768 p->next_time_max = jiffies + msecs_to_jiffies(2000);
771 devpriv->timer.data = (unsigned long)dev;
772 devpriv->timer.function = jr3_pci_poll_dev;
773 devpriv->timer.expires = jiffies + msecs_to_jiffies(1000);
774 add_timer(&devpriv->timer);
779 static void jr3_pci_detach(struct comedi_device *dev)
782 struct jr3_pci_dev_private *devpriv = dev->private;
785 del_timer_sync(&devpriv->timer);
787 if (dev->subdevices) {
788 for (i = 0; i < devpriv->n_channels; i++)
789 kfree(dev->subdevices[i].private);
792 iounmap(devpriv->iobase);
794 comedi_pci_disable(dev);
797 static struct comedi_driver jr3_pci_driver = {
798 .driver_name = "jr3_pci",
799 .module = THIS_MODULE,
800 .auto_attach = jr3_pci_auto_attach,
801 .detach = jr3_pci_detach,
804 static int jr3_pci_pci_probe(struct pci_dev *dev,
805 const struct pci_device_id *id)
807 return comedi_pci_auto_config(dev, &jr3_pci_driver, id->driver_data);
810 static DEFINE_PCI_DEVICE_TABLE(jr3_pci_pci_table) = {
811 { PCI_DEVICE(PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_1_CHANNEL) },
812 { PCI_DEVICE(PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_1_CHANNEL_NEW) },
813 { PCI_DEVICE(PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_2_CHANNEL) },
814 { PCI_DEVICE(PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_3_CHANNEL) },
815 { PCI_DEVICE(PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_4_CHANNEL) },
818 MODULE_DEVICE_TABLE(pci, jr3_pci_pci_table);
820 static struct pci_driver jr3_pci_pci_driver = {
822 .id_table = jr3_pci_pci_table,
823 .probe = jr3_pci_pci_probe,
824 .remove = comedi_pci_auto_unconfig,
826 module_comedi_pci_driver(jr3_pci_driver, jr3_pci_pci_driver);
828 MODULE_AUTHOR("Comedi http://www.comedi.org");
829 MODULE_DESCRIPTION("Comedi low-level driver");
830 MODULE_LICENSE("GPL");
831 MODULE_FIRMWARE("comedi/jr3pci.idm");