blob: e248257fe517a5f62fc909826640d82d4e15a66f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 i2c Support for the Apple `Hydra' Mac I/O
3
4 Copyright (c) 1999-2004 Geert Uytterhoeven <geert@linux-m68k.org>
5
6 Based on i2c Support for Via Technologies 82C586B South Bridge
Jan Engelhardt96de0e22007-10-19 23:21:04 +02007 Copyright (c) 1998, 1999 Kyösti Mälkki <kmalkki@cc.hut.fi>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22*/
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/pci.h>
27#include <linux/types.h>
28#include <linux/i2c.h>
29#include <linux/i2c-algo-bit.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020030#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/hydra.h>
32
33
34#define HYDRA_CPD_PD0 0x00000001 /* CachePD lines */
35#define HYDRA_CPD_PD1 0x00000002
36#define HYDRA_CPD_PD2 0x00000004
37#define HYDRA_CPD_PD3 0x00000008
38
39#define HYDRA_SCLK HYDRA_CPD_PD0
40#define HYDRA_SDAT HYDRA_CPD_PD1
41#define HYDRA_SCLK_OE 0x00000010
42#define HYDRA_SDAT_OE 0x00000020
43
44static inline void pdregw(void *data, u32 val)
45{
46 struct Hydra *hydra = (struct Hydra *)data;
47 writel(val, &hydra->CachePD);
48}
49
50static inline u32 pdregr(void *data)
51{
52 struct Hydra *hydra = (struct Hydra *)data;
53 return readl(&hydra->CachePD);
54}
55
56static void hydra_bit_setscl(void *data, int state)
57{
58 u32 val = pdregr(data);
59 if (state)
60 val &= ~HYDRA_SCLK_OE;
61 else {
62 val &= ~HYDRA_SCLK;
63 val |= HYDRA_SCLK_OE;
64 }
65 pdregw(data, val);
66}
67
68static void hydra_bit_setsda(void *data, int state)
69{
70 u32 val = pdregr(data);
71 if (state)
72 val &= ~HYDRA_SDAT_OE;
73 else {
74 val &= ~HYDRA_SDAT;
75 val |= HYDRA_SDAT_OE;
76 }
77 pdregw(data, val);
78}
79
80static int hydra_bit_getscl(void *data)
81{
82 return (pdregr(data) & HYDRA_SCLK) != 0;
83}
84
85static int hydra_bit_getsda(void *data)
86{
87 return (pdregr(data) & HYDRA_SDAT) != 0;
88}
89
90/* ------------------------------------------------------------------------ */
91
92static struct i2c_algo_bit_data hydra_bit_data = {
93 .setsda = hydra_bit_setsda,
94 .setscl = hydra_bit_setscl,
95 .getsda = hydra_bit_getsda,
96 .getscl = hydra_bit_getscl,
97 .udelay = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 .timeout = HZ
99};
100
101static struct i2c_adapter hydra_adap = {
102 .owner = THIS_MODULE,
103 .name = "Hydra i2c",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .algo_data = &hydra_bit_data,
105};
106
Axel Lin3527bd52012-01-12 20:32:04 +0100107static DEFINE_PCI_DEVICE_TABLE(hydra_ids) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_HYDRA) },
109 { 0, }
110};
111
112MODULE_DEVICE_TABLE (pci, hydra_ids);
113
Bill Pemberton0b255e92012-11-27 15:59:38 -0500114static int hydra_probe(struct pci_dev *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 const struct pci_device_id *id)
116{
117 unsigned long base = pci_resource_start(dev, 0);
118 int res;
119
120 if (!request_mem_region(base+offsetof(struct Hydra, CachePD), 4,
121 hydra_adap.name))
122 return -EBUSY;
123
Arjan van de Venc7a5f222008-10-22 20:21:32 +0200124 hydra_bit_data.data = pci_ioremap_bar(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (hydra_bit_data.data == NULL) {
126 release_mem_region(base+offsetof(struct Hydra, CachePD), 4);
127 return -ENODEV;
128 }
129
130 pdregw(hydra_bit_data.data, 0); /* clear SCLK_OE and SDAT_OE */
131 hydra_adap.dev.parent = &dev->dev;
132 res = i2c_bit_add_bus(&hydra_adap);
133 if (res < 0) {
134 iounmap(hydra_bit_data.data);
135 release_mem_region(base+offsetof(struct Hydra, CachePD), 4);
136 return res;
137 }
138 return 0;
139}
140
Bill Pemberton0b255e92012-11-27 15:59:38 -0500141static void hydra_remove(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 pdregw(hydra_bit_data.data, 0); /* clear SCLK_OE and SDAT_OE */
Jean Delvare32697112006-12-10 21:21:33 +0100144 i2c_del_adapter(&hydra_adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 iounmap(hydra_bit_data.data);
146 release_mem_region(pci_resource_start(dev, 0)+
147 offsetof(struct Hydra, CachePD), 4);
148}
149
150
151static struct pci_driver hydra_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 .name = "hydra_smbus",
153 .id_table = hydra_ids,
154 .probe = hydra_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500155 .remove = hydra_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
Axel Lin56f21782012-07-24 14:13:56 +0200158module_pci_driver(hydra_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>");
161MODULE_DESCRIPTION("i2c for Apple Hydra Mac I/O");
162MODULE_LICENSE("GPL");