Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 3 | * matrox_w1.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Evgeniy Polyakov | a801876 | 2011-08-25 15:59:06 -0700 | [diff] [blame] | 5 | * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <asm/types.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 9 | #include <linux/atomic.h> |
Krzysztof Kozlowski | 01bc199 | 2023-04-15 12:42:58 +0200 | [diff] [blame] | 10 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/list.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/spinlock.h> |
| 18 | #include <linux/timer.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/pci_ids.h> |
| 21 | #include <linux/pci.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Andrew F. Davis | de0d6db | 2017-06-05 08:52:08 -0500 | [diff] [blame] | 23 | #include <linux/w1.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 25 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | * Matrox G400 DDC registers. |
| 27 | */ |
| 28 | |
| 29 | #define MATROX_G400_DDC_CLK (1<<4) |
| 30 | #define MATROX_G400_DDC_DATA (1<<1) |
| 31 | |
| 32 | #define MATROX_BASE 0x3C00 |
| 33 | #define MATROX_STATUS 0x1e14 |
| 34 | |
| 35 | #define MATROX_PORT_INDEX_OFFSET 0x00 |
| 36 | #define MATROX_PORT_DATA_OFFSET 0x0A |
| 37 | |
| 38 | #define MATROX_GET_CONTROL 0x2A |
| 39 | #define MATROX_GET_DATA 0x2B |
| 40 | #define MATROX_CURSOR_CTL 0x06 |
| 41 | |
Krzysztof Kozlowski | ad9c36b | 2023-04-15 12:42:52 +0200 | [diff] [blame] | 42 | struct matrox_device { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | void __iomem *base_addr; |
| 44 | void __iomem *port_index; |
| 45 | void __iomem *port_data; |
| 46 | u8 data_mask; |
| 47 | |
| 48 | unsigned long phys_addr; |
| 49 | void __iomem *virt_addr; |
| 50 | unsigned long found; |
| 51 | |
| 52 | struct w1_bus_master *bus_master; |
| 53 | }; |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | /* |
| 56 | * These functions read and write DDC Data bit. |
| 57 | * |
| 58 | * Using tristate pins, since i can't find any open-drain pin in whole motherboard. |
| 59 | * Unfortunately we can't connect to Intel's 82801xx IO controller |
Uwe Zeisberger | c30fe7f | 2006-03-24 18:23:14 +0100 | [diff] [blame] | 60 | * 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] | 61 | * |
| 62 | * I've heard that PIIX also has open drain pin. |
| 63 | * |
| 64 | * Port mapping. |
| 65 | */ |
Krzysztof Kozlowski | 115ddc7 | 2023-04-15 12:42:59 +0200 | [diff] [blame] | 66 | static inline u8 matrox_w1_read_reg(struct matrox_device *dev, u8 reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
| 68 | u8 ret; |
| 69 | |
| 70 | writeb(reg, dev->port_index); |
| 71 | ret = readb(dev->port_data); |
| 72 | barrier(); |
| 73 | |
| 74 | return ret; |
| 75 | } |
| 76 | |
Krzysztof Kozlowski | 115ddc7 | 2023-04-15 12:42:59 +0200 | [diff] [blame] | 77 | static inline void matrox_w1_write_reg(struct matrox_device *dev, u8 reg, u8 val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
| 79 | writeb(reg, dev->port_index); |
| 80 | writeb(val, dev->port_data); |
| 81 | wmb(); |
| 82 | } |
| 83 | |
Evgeniy Polyakov | ccd6994 | 2005-12-06 13:38:27 +0300 | [diff] [blame] | 84 | static void matrox_w1_write_ddc_bit(void *data, u8 bit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
| 86 | u8 ret; |
Evgeniy Polyakov | ccd6994 | 2005-12-06 13:38:27 +0300 | [diff] [blame] | 87 | struct matrox_device *dev = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | if (bit) |
| 90 | bit = 0; |
| 91 | else |
| 92 | bit = dev->data_mask; |
| 93 | |
| 94 | ret = matrox_w1_read_reg(dev, MATROX_GET_CONTROL); |
| 95 | matrox_w1_write_reg(dev, MATROX_GET_CONTROL, ((ret & ~dev->data_mask) | bit)); |
| 96 | matrox_w1_write_reg(dev, MATROX_GET_DATA, 0x00); |
| 97 | } |
| 98 | |
Evgeniy Polyakov | ccd6994 | 2005-12-06 13:38:27 +0300 | [diff] [blame] | 99 | static u8 matrox_w1_read_ddc_bit(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
| 101 | u8 ret; |
Evgeniy Polyakov | ccd6994 | 2005-12-06 13:38:27 +0300 | [diff] [blame] | 102 | struct matrox_device *dev = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
| 104 | ret = matrox_w1_read_reg(dev, MATROX_GET_DATA); |
| 105 | |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | static void matrox_w1_hw_init(struct matrox_device *dev) |
| 110 | { |
| 111 | matrox_w1_write_reg(dev, MATROX_GET_DATA, 0xFF); |
| 112 | matrox_w1_write_reg(dev, MATROX_GET_CONTROL, 0x00); |
| 113 | } |
| 114 | |
Bill Pemberton | 479e2bc | 2012-11-19 13:21:43 -0500 | [diff] [blame] | 115 | static int matrox_w1_probe(struct pci_dev *pdev, const struct pci_device_id *ent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
| 117 | struct matrox_device *dev; |
| 118 | int err; |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | if (pdev->vendor != PCI_VENDOR_ID_MATROX || pdev->device != PCI_DEVICE_ID_MATROX_G400) |
| 121 | return -ENODEV; |
| 122 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 123 | dev = kzalloc(sizeof(struct matrox_device) + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | sizeof(struct w1_bus_master), GFP_KERNEL); |
Krzysztof Kozlowski | 4f4129e | 2023-04-15 12:43:00 +0200 | [diff] [blame] | 125 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | dev->bus_master = (struct w1_bus_master *)(dev + 1); |
| 129 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 130 | /* |
| 131 | * 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] | 132 | */ |
| 133 | |
| 134 | dev->phys_addr = pci_resource_start(pdev, 1); |
| 135 | |
Christoph Hellwig | 4bdc0d6 | 2020-01-06 09:43:50 +0100 | [diff] [blame] | 136 | dev->virt_addr = ioremap(dev->phys_addr, 16384); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | if (!dev->virt_addr) { |
| 138 | dev_err(&pdev->dev, "%s: failed to ioremap(0x%lx, %d).\n", |
| 139 | __func__, dev->phys_addr, 16384); |
| 140 | err = -EIO; |
| 141 | goto err_out_free_device; |
| 142 | } |
| 143 | |
| 144 | dev->base_addr = dev->virt_addr + MATROX_BASE; |
| 145 | dev->port_index = dev->base_addr + MATROX_PORT_INDEX_OFFSET; |
| 146 | dev->port_data = dev->base_addr + MATROX_PORT_DATA_OFFSET; |
| 147 | dev->data_mask = (MATROX_G400_DDC_DATA); |
| 148 | |
| 149 | matrox_w1_hw_init(dev); |
| 150 | |
Evgeniy Polyakov | ccd6994 | 2005-12-06 13:38:27 +0300 | [diff] [blame] | 151 | dev->bus_master->data = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | dev->bus_master->read_bit = &matrox_w1_read_ddc_bit; |
| 153 | dev->bus_master->write_bit = &matrox_w1_write_ddc_bit; |
| 154 | |
| 155 | err = w1_add_master_device(dev->bus_master); |
| 156 | if (err) |
| 157 | goto err_out_free_device; |
| 158 | |
| 159 | pci_set_drvdata(pdev, dev); |
| 160 | |
| 161 | dev->found = 1; |
| 162 | |
| 163 | dev_info(&pdev->dev, "Matrox G400 GPIO transport layer for 1-wire.\n"); |
| 164 | |
| 165 | return 0; |
| 166 | |
| 167 | err_out_free_device: |
Amol Lad | e45413e | 2006-10-05 14:26:02 +0400 | [diff] [blame] | 168 | if (dev->virt_addr) |
| 169 | iounmap(dev->virt_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | kfree(dev); |
| 171 | |
| 172 | return err; |
| 173 | } |
| 174 | |
Bill Pemberton | 82849a9 | 2012-11-19 13:26:23 -0500 | [diff] [blame] | 175 | static void matrox_w1_remove(struct pci_dev *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { |
| 177 | struct matrox_device *dev = pci_get_drvdata(pdev); |
| 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | if (dev->found) { |
| 180 | w1_remove_master_device(dev->bus_master); |
| 181 | iounmap(dev->virt_addr); |
| 182 | } |
| 183 | kfree(dev); |
| 184 | } |
Andrew F. Davis | 50fa295 | 2017-05-16 15:02:12 -0500 | [diff] [blame] | 185 | |
| 186 | static struct pci_device_id matrox_w1_tbl[] = { |
| 187 | { PCI_DEVICE(PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G400) }, |
| 188 | { }, |
| 189 | }; |
| 190 | MODULE_DEVICE_TABLE(pci, matrox_w1_tbl); |
| 191 | |
| 192 | static struct pci_driver matrox_w1_pci_driver = { |
| 193 | .name = "matrox_w1", |
| 194 | .id_table = matrox_w1_tbl, |
| 195 | .probe = matrox_w1_probe, |
| 196 | .remove = matrox_w1_remove, |
| 197 | }; |
Vaishali Thakkar | e7390d7 | 2015-07-07 12:23:49 +0530 | [diff] [blame] | 198 | module_pci_driver(matrox_w1_pci_driver); |
Andrew F. Davis | 50fa295 | 2017-05-16 15:02:12 -0500 | [diff] [blame] | 199 | |
| 200 | MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>"); |
| 201 | MODULE_DESCRIPTION("Driver for transport(Dallas 1-wire protocol) over VGA DDC(matrox gpio)."); |
| 202 | MODULE_LICENSE("GPL"); |