Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 2 | * matrox_w1.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * 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 |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/types.h> |
Evgeniy Polyakov | bd529cf | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 23 | #include <asm/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <asm/io.h> |
| 25 | |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/list.h> |
| 30 | #include <linux/interrupt.h> |
| 31 | #include <linux/spinlock.h> |
| 32 | #include <linux/timer.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <linux/pci_ids.h> |
| 35 | #include <linux/pci.h> |
| 36 | #include <linux/timer.h> |
| 37 | |
Evgeniy Polyakov | bd529cf | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 38 | #include "../w1.h" |
| 39 | #include "../w1_int.h" |
| 40 | #include "../w1_log.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | MODULE_LICENSE("GPL"); |
| 43 | MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>"); |
| 44 | MODULE_DESCRIPTION("Driver for transport(Dallas 1-wire prtocol) over VGA DDC(matrox gpio)."); |
| 45 | |
| 46 | static struct pci_device_id matrox_w1_tbl[] = { |
| 47 | { PCI_DEVICE(PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G400) }, |
| 48 | { }, |
| 49 | }; |
| 50 | MODULE_DEVICE_TABLE(pci, matrox_w1_tbl); |
| 51 | |
| 52 | static int __devinit matrox_w1_probe(struct pci_dev *, const struct pci_device_id *); |
| 53 | static void __devexit matrox_w1_remove(struct pci_dev *); |
| 54 | |
| 55 | static struct pci_driver matrox_w1_pci_driver = { |
| 56 | .name = "matrox_w1", |
| 57 | .id_table = matrox_w1_tbl, |
| 58 | .probe = matrox_w1_probe, |
| 59 | .remove = __devexit_p(matrox_w1_remove), |
| 60 | }; |
| 61 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 62 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | * Matrox G400 DDC registers. |
| 64 | */ |
| 65 | |
| 66 | #define MATROX_G400_DDC_CLK (1<<4) |
| 67 | #define MATROX_G400_DDC_DATA (1<<1) |
| 68 | |
| 69 | #define MATROX_BASE 0x3C00 |
| 70 | #define MATROX_STATUS 0x1e14 |
| 71 | |
| 72 | #define MATROX_PORT_INDEX_OFFSET 0x00 |
| 73 | #define MATROX_PORT_DATA_OFFSET 0x0A |
| 74 | |
| 75 | #define MATROX_GET_CONTROL 0x2A |
| 76 | #define MATROX_GET_DATA 0x2B |
| 77 | #define MATROX_CURSOR_CTL 0x06 |
| 78 | |
| 79 | struct matrox_device |
| 80 | { |
| 81 | void __iomem *base_addr; |
| 82 | void __iomem *port_index; |
| 83 | void __iomem *port_data; |
| 84 | u8 data_mask; |
| 85 | |
| 86 | unsigned long phys_addr; |
| 87 | void __iomem *virt_addr; |
| 88 | unsigned long found; |
| 89 | |
| 90 | struct w1_bus_master *bus_master; |
| 91 | }; |
| 92 | |
Evgeniy Polyakov | ccd6994 | 2005-12-06 13:38:27 +0300 | [diff] [blame] | 93 | static u8 matrox_w1_read_ddc_bit(void *); |
| 94 | static void matrox_w1_write_ddc_bit(void *, u8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | * These functions read and write DDC Data bit. |
| 98 | * |
| 99 | * Using tristate pins, since i can't find any open-drain pin in whole motherboard. |
| 100 | * Unfortunately we can't connect to Intel's 82801xx IO controller |
Uwe Zeisberger | c30fe7f | 2006-03-24 18:23:14 +0100 | [diff] [blame] | 101 | * since we don't know motherboard schema, which has pretty unused(may be not) GPIO. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | * |
| 103 | * I've heard that PIIX also has open drain pin. |
| 104 | * |
| 105 | * Port mapping. |
| 106 | */ |
| 107 | static __inline__ u8 matrox_w1_read_reg(struct matrox_device *dev, u8 reg) |
| 108 | { |
| 109 | u8 ret; |
| 110 | |
| 111 | writeb(reg, dev->port_index); |
| 112 | ret = readb(dev->port_data); |
| 113 | barrier(); |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | static __inline__ void matrox_w1_write_reg(struct matrox_device *dev, u8 reg, u8 val) |
| 119 | { |
| 120 | writeb(reg, dev->port_index); |
| 121 | writeb(val, dev->port_data); |
| 122 | wmb(); |
| 123 | } |
| 124 | |
Evgeniy Polyakov | ccd6994 | 2005-12-06 13:38:27 +0300 | [diff] [blame] | 125 | static void matrox_w1_write_ddc_bit(void *data, u8 bit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
| 127 | u8 ret; |
Evgeniy Polyakov | ccd6994 | 2005-12-06 13:38:27 +0300 | [diff] [blame] | 128 | struct matrox_device *dev = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | if (bit) |
| 131 | bit = 0; |
| 132 | else |
| 133 | bit = dev->data_mask; |
| 134 | |
| 135 | ret = matrox_w1_read_reg(dev, MATROX_GET_CONTROL); |
| 136 | matrox_w1_write_reg(dev, MATROX_GET_CONTROL, ((ret & ~dev->data_mask) | bit)); |
| 137 | matrox_w1_write_reg(dev, MATROX_GET_DATA, 0x00); |
| 138 | } |
| 139 | |
Evgeniy Polyakov | ccd6994 | 2005-12-06 13:38:27 +0300 | [diff] [blame] | 140 | static u8 matrox_w1_read_ddc_bit(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
| 142 | u8 ret; |
Evgeniy Polyakov | ccd6994 | 2005-12-06 13:38:27 +0300 | [diff] [blame] | 143 | struct matrox_device *dev = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | ret = matrox_w1_read_reg(dev, MATROX_GET_DATA); |
| 146 | |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | static void matrox_w1_hw_init(struct matrox_device *dev) |
| 151 | { |
| 152 | matrox_w1_write_reg(dev, MATROX_GET_DATA, 0xFF); |
| 153 | matrox_w1_write_reg(dev, MATROX_GET_CONTROL, 0x00); |
| 154 | } |
| 155 | |
| 156 | static int __devinit matrox_w1_probe(struct pci_dev *pdev, const struct pci_device_id *ent) |
| 157 | { |
| 158 | struct matrox_device *dev; |
| 159 | int err; |
| 160 | |
| 161 | assert(pdev != NULL); |
| 162 | assert(ent != NULL); |
| 163 | |
| 164 | if (pdev->vendor != PCI_VENDOR_ID_MATROX || pdev->device != PCI_DEVICE_ID_MATROX_G400) |
| 165 | return -ENODEV; |
| 166 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame^] | 167 | dev = kzalloc(sizeof(struct matrox_device) + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | sizeof(struct w1_bus_master), GFP_KERNEL); |
| 169 | if (!dev) { |
| 170 | dev_err(&pdev->dev, |
| 171 | "%s: Failed to create new matrox_device object.\n", |
| 172 | __func__); |
| 173 | return -ENOMEM; |
| 174 | } |
| 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | dev->bus_master = (struct w1_bus_master *)(dev + 1); |
| 178 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 179 | /* |
| 180 | * True for G400, for some other we need resource 0, see drivers/video/matrox/matroxfb_base.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | */ |
| 182 | |
| 183 | dev->phys_addr = pci_resource_start(pdev, 1); |
| 184 | |
| 185 | dev->virt_addr = ioremap_nocache(dev->phys_addr, 16384); |
| 186 | if (!dev->virt_addr) { |
| 187 | dev_err(&pdev->dev, "%s: failed to ioremap(0x%lx, %d).\n", |
| 188 | __func__, dev->phys_addr, 16384); |
| 189 | err = -EIO; |
| 190 | goto err_out_free_device; |
| 191 | } |
| 192 | |
| 193 | dev->base_addr = dev->virt_addr + MATROX_BASE; |
| 194 | dev->port_index = dev->base_addr + MATROX_PORT_INDEX_OFFSET; |
| 195 | dev->port_data = dev->base_addr + MATROX_PORT_DATA_OFFSET; |
| 196 | dev->data_mask = (MATROX_G400_DDC_DATA); |
| 197 | |
| 198 | matrox_w1_hw_init(dev); |
| 199 | |
Evgeniy Polyakov | ccd6994 | 2005-12-06 13:38:27 +0300 | [diff] [blame] | 200 | dev->bus_master->data = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | dev->bus_master->read_bit = &matrox_w1_read_ddc_bit; |
| 202 | dev->bus_master->write_bit = &matrox_w1_write_ddc_bit; |
| 203 | |
| 204 | err = w1_add_master_device(dev->bus_master); |
| 205 | if (err) |
| 206 | goto err_out_free_device; |
| 207 | |
| 208 | pci_set_drvdata(pdev, dev); |
| 209 | |
| 210 | dev->found = 1; |
| 211 | |
| 212 | dev_info(&pdev->dev, "Matrox G400 GPIO transport layer for 1-wire.\n"); |
| 213 | |
| 214 | return 0; |
| 215 | |
| 216 | err_out_free_device: |
Amol Lad | e45413e | 2006-10-05 14:26:02 +0400 | [diff] [blame] | 217 | if (dev->virt_addr) |
| 218 | iounmap(dev->virt_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | kfree(dev); |
| 220 | |
| 221 | return err; |
| 222 | } |
| 223 | |
| 224 | static void __devexit matrox_w1_remove(struct pci_dev *pdev) |
| 225 | { |
| 226 | struct matrox_device *dev = pci_get_drvdata(pdev); |
| 227 | |
| 228 | assert(dev != NULL); |
| 229 | |
| 230 | if (dev->found) { |
| 231 | w1_remove_master_device(dev->bus_master); |
| 232 | iounmap(dev->virt_addr); |
| 233 | } |
| 234 | kfree(dev); |
| 235 | } |
| 236 | |
| 237 | static int __init matrox_w1_init(void) |
| 238 | { |
| 239 | return pci_register_driver(&matrox_w1_pci_driver); |
| 240 | } |
| 241 | |
| 242 | static void __exit matrox_w1_fini(void) |
| 243 | { |
| 244 | pci_unregister_driver(&matrox_w1_pci_driver); |
| 245 | } |
| 246 | |
| 247 | module_init(matrox_w1_init); |
| 248 | module_exit(matrox_w1_fini); |