blob: 537e55cd038d11a286b3e2eda32a178fb91bb5ec [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
David S. Millere21e2452008-08-29 22:34:14 -07002/* bbc_i2c.c: I2C low-level driver for BBC device on UltraSPARC-III
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * platforms.
4 *
David S. Millere21e2452008-08-29 22:34:14 -07005 * Copyright (C) 2001, 2008 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8#include <linux/module.h>
9#include <linux/kernel.h>
10#include <linux/types.h>
11#include <linux/slab.h>
12#include <linux/sched.h>
13#include <linux/wait.h>
14#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/interrupt.h>
David S. Millere21e2452008-08-29 22:34:14 -070016#include <linux/of.h>
17#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/bbc.h>
David S. Miller4b5dff72007-05-13 22:22:13 -070019#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include "bbc_i2c.h"
22
23/* Convert this driver to use i2c bus layer someday... */
24#define I2C_PCF_PIN 0x80
25#define I2C_PCF_ESO 0x40
26#define I2C_PCF_ES1 0x20
27#define I2C_PCF_ES2 0x10
28#define I2C_PCF_ENI 0x08
29#define I2C_PCF_STA 0x04
30#define I2C_PCF_STO 0x02
31#define I2C_PCF_ACK 0x01
32
33#define I2C_PCF_START (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_ENI | I2C_PCF_STA | I2C_PCF_ACK)
34#define I2C_PCF_STOP (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_STO | I2C_PCF_ACK)
35#define I2C_PCF_REPSTART ( I2C_PCF_ESO | I2C_PCF_STA | I2C_PCF_ACK)
36#define I2C_PCF_IDLE (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_ACK)
37
38#define I2C_PCF_INI 0x40 /* 1 if not initialized */
39#define I2C_PCF_STS 0x20
40#define I2C_PCF_BER 0x10
41#define I2C_PCF_AD0 0x08
42#define I2C_PCF_LRB 0x08
43#define I2C_PCF_AAS 0x04
44#define I2C_PCF_LAB 0x02
45#define I2C_PCF_BB 0x01
46
47/* The BBC devices have two I2C controllers. The first I2C controller
48 * connects mainly to configuration proms (NVRAM, cpu configuration,
49 * dimm types, etc.). Whereas the second I2C controller connects to
50 * environmental control devices such as fans and temperature sensors.
51 * The second controller also connects to the smartcard reader, if present.
52 */
53
Grant Likely2dc11582010-08-06 09:25:50 -060054static void set_device_claimage(struct bbc_i2c_bus *bp, struct platform_device *op, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 int i;
57
58 for (i = 0; i < NUM_CHILDREN; i++) {
David S. Millere21e2452008-08-29 22:34:14 -070059 if (bp->devs[i].device == op) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 bp->devs[i].client_claimed = val;
61 return;
62 }
63 }
64}
65
66#define claim_device(BP,ECHILD) set_device_claimage(BP,ECHILD,1)
67#define release_device(BP,ECHILD) set_device_claimage(BP,ECHILD,0)
68
Grant Likely2dc11582010-08-06 09:25:50 -060069struct platform_device *bbc_i2c_getdev(struct bbc_i2c_bus *bp, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Grant Likely2dc11582010-08-06 09:25:50 -060071 struct platform_device *op = NULL;
David S. Millere21e2452008-08-29 22:34:14 -070072 int curidx = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
David S. Millere21e2452008-08-29 22:34:14 -070074 for (i = 0; i < NUM_CHILDREN; i++) {
75 if (!(op = bp->devs[i].device))
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 break;
David S. Millere21e2452008-08-29 22:34:14 -070077 if (curidx == index)
78 goto out;
79 op = NULL;
80 curidx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083out:
84 if (curidx == index)
David S. Millere21e2452008-08-29 22:34:14 -070085 return op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return NULL;
87}
88
Grant Likely2dc11582010-08-06 09:25:50 -060089struct bbc_i2c_client *bbc_i2c_attach(struct bbc_i2c_bus *bp, struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 struct bbc_i2c_client *client;
David S. Millere21e2452008-08-29 22:34:14 -070092 const u32 *reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Yoann Padioleaudd00cc42007-07-19 01:49:03 -070094 client = kzalloc(sizeof(*client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (!client)
96 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 client->bp = bp;
David S. Millere21e2452008-08-29 22:34:14 -070098 client->op = op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Grant Likely61c7a082010-04-13 16:12:29 -0700100 reg = of_get_property(op->dev.of_node, "reg", NULL);
David S. Millere21e2452008-08-29 22:34:14 -0700101 if (!reg) {
102 kfree(client);
103 return NULL;
104 }
105
106 client->bus = reg[0];
107 client->address = reg[1];
108
109 claim_device(bp, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 return client;
112}
113
114void bbc_i2c_detach(struct bbc_i2c_client *client)
115{
116 struct bbc_i2c_bus *bp = client->bp;
Grant Likely2dc11582010-08-06 09:25:50 -0600117 struct platform_device *op = client->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
David S. Millere21e2452008-08-29 22:34:14 -0700119 release_device(bp, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 kfree(client);
121}
122
123static int wait_for_pin(struct bbc_i2c_bus *bp, u8 *status)
124{
125 DECLARE_WAITQUEUE(wait, current);
126 int limit = 32;
127 int ret = 1;
128
129 bp->waiting = 1;
130 add_wait_queue(&bp->wq, &wait);
131 while (limit-- > 0) {
Roel Kluinf4c13632009-03-04 00:19:28 -0800132 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
David S. Miller3b36fb82007-02-26 10:11:35 -0800134 val = wait_event_interruptible_timeout(
135 bp->wq,
136 (((*status = readb(bp->i2c_control_regs + 0))
137 & I2C_PCF_PIN) == 0),
138 msecs_to_jiffies(250));
139 if (val > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 ret = 0;
141 break;
142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144 remove_wait_queue(&bp->wq, &wait);
145 bp->waiting = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 return ret;
148}
149
150int bbc_i2c_writeb(struct bbc_i2c_client *client, unsigned char val, int off)
151{
152 struct bbc_i2c_bus *bp = client->bp;
153 int address = client->address;
154 u8 status;
155 int ret = -1;
156
157 if (bp->i2c_bussel_reg != NULL)
158 writeb(client->bus, bp->i2c_bussel_reg);
159
160 writeb(address, bp->i2c_control_regs + 0x1);
161 writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
162 if (wait_for_pin(bp, &status))
163 goto out;
164
165 writeb(off, bp->i2c_control_regs + 0x1);
166 if (wait_for_pin(bp, &status) ||
167 (status & I2C_PCF_LRB) != 0)
168 goto out;
169
170 writeb(val, bp->i2c_control_regs + 0x1);
171 if (wait_for_pin(bp, &status))
172 goto out;
173
174 ret = 0;
175
176out:
177 writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
178 return ret;
179}
180
181int bbc_i2c_readb(struct bbc_i2c_client *client, unsigned char *byte, int off)
182{
183 struct bbc_i2c_bus *bp = client->bp;
184 unsigned char address = client->address, status;
185 int ret = -1;
186
187 if (bp->i2c_bussel_reg != NULL)
188 writeb(client->bus, bp->i2c_bussel_reg);
189
190 writeb(address, bp->i2c_control_regs + 0x1);
191 writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
192 if (wait_for_pin(bp, &status))
193 goto out;
194
195 writeb(off, bp->i2c_control_regs + 0x1);
196 if (wait_for_pin(bp, &status) ||
197 (status & I2C_PCF_LRB) != 0)
198 goto out;
199
200 writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
201
202 address |= 0x1; /* READ */
203
204 writeb(address, bp->i2c_control_regs + 0x1);
205 writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
206 if (wait_for_pin(bp, &status))
207 goto out;
208
209 /* Set PIN back to one so the device sends the first
210 * byte.
211 */
212 (void) readb(bp->i2c_control_regs + 0x1);
213 if (wait_for_pin(bp, &status))
214 goto out;
215
216 writeb(I2C_PCF_ESO | I2C_PCF_ENI, bp->i2c_control_regs + 0x0);
217 *byte = readb(bp->i2c_control_regs + 0x1);
218 if (wait_for_pin(bp, &status))
219 goto out;
220
221 ret = 0;
222
223out:
224 writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
225 (void) readb(bp->i2c_control_regs + 0x1);
226
227 return ret;
228}
229
230int bbc_i2c_write_buf(struct bbc_i2c_client *client,
231 char *buf, int len, int off)
232{
233 int ret = 0;
234
235 while (len > 0) {
Axel Line4104712011-11-21 21:42:20 +0000236 ret = bbc_i2c_writeb(client, *buf, off);
237 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 len--;
240 buf++;
241 off++;
242 }
243 return ret;
244}
245
246int bbc_i2c_read_buf(struct bbc_i2c_client *client,
247 char *buf, int len, int off)
248{
249 int ret = 0;
250
251 while (len > 0) {
Axel Line4104712011-11-21 21:42:20 +0000252 ret = bbc_i2c_readb(client, buf, off);
253 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 len--;
256 buf++;
257 off++;
258 }
259
260 return ret;
261}
262
263EXPORT_SYMBOL(bbc_i2c_getdev);
264EXPORT_SYMBOL(bbc_i2c_attach);
265EXPORT_SYMBOL(bbc_i2c_detach);
266EXPORT_SYMBOL(bbc_i2c_writeb);
267EXPORT_SYMBOL(bbc_i2c_readb);
268EXPORT_SYMBOL(bbc_i2c_write_buf);
269EXPORT_SYMBOL(bbc_i2c_read_buf);
270
David Howells7d12e782006-10-05 14:55:46 +0100271static irqreturn_t bbc_i2c_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct bbc_i2c_bus *bp = dev_id;
274
275 /* PIN going from set to clear is the only event which
276 * makes the i2c assert an interrupt.
277 */
278 if (bp->waiting &&
279 !(readb(bp->i2c_control_regs + 0x0) & I2C_PCF_PIN))
David S. Miller3b36fb82007-02-26 10:11:35 -0800280 wake_up_interruptible(&bp->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 return IRQ_HANDLED;
283}
284
Sam Ravnborg6e51f852013-03-30 11:44:24 +0000285static void reset_one_i2c(struct bbc_i2c_bus *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0);
288 writeb(bp->own, bp->i2c_control_regs + 0x1);
289 writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0);
290 writeb(bp->clock, bp->i2c_control_regs + 0x1);
291 writeb(I2C_PCF_IDLE, bp->i2c_control_regs + 0x0);
292}
293
Sam Ravnborg6e51f852013-03-30 11:44:24 +0000294static struct bbc_i2c_bus * attach_one_i2c(struct platform_device *op, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Mariusz Kozlowski50aa4852007-07-31 14:04:57 -0700296 struct bbc_i2c_bus *bp;
David S. Millere21e2452008-08-29 22:34:14 -0700297 struct device_node *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int entry;
299
Mariusz Kozlowski50aa4852007-07-31 14:04:57 -0700300 bp = kzalloc(sizeof(*bp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (!bp)
David S. Millere21e2452008-08-29 22:34:14 -0700302 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Christopher Alexander Tobias Schulze5cdceab2014-08-03 15:44:52 +0200304 INIT_LIST_HEAD(&bp->temps);
305 INIT_LIST_HEAD(&bp->fans);
306
David S. Millere21e2452008-08-29 22:34:14 -0700307 bp->i2c_control_regs = of_ioremap(&op->resource[0], 0, 0x2, "bbc_i2c_regs");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (!bp->i2c_control_regs)
309 goto fail;
310
Christopher Alexander Tobias Schulze5cdceab2014-08-03 15:44:52 +0200311 if (op->num_resources == 2) {
312 bp->i2c_bussel_reg = of_ioremap(&op->resource[1], 0, 0x1, "bbc_i2c_bussel");
313 if (!bp->i2c_bussel_reg)
314 goto fail;
315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 bp->waiting = 0;
318 init_waitqueue_head(&bp->wq);
Grant Likely1636f8a2010-06-18 11:09:58 -0600319 if (request_irq(op->archdata.irqs[0], bbc_i2c_interrupt,
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700320 IRQF_SHARED, "bbc_i2c", bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 goto fail;
322
323 bp->index = index;
David S. Millere21e2452008-08-29 22:34:14 -0700324 bp->op = op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 spin_lock_init(&bp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 entry = 0;
Grant Likely61c7a082010-04-13 16:12:29 -0700329 for (dp = op->dev.of_node->child;
David S. Millere21e2452008-08-29 22:34:14 -0700330 dp && entry < 8;
331 dp = dp->sibling, entry++) {
Grant Likely2dc11582010-08-06 09:25:50 -0600332 struct platform_device *child_op;
David S. Millere21e2452008-08-29 22:34:14 -0700333
334 child_op = of_find_device_by_node(dp);
335 bp->devs[entry].device = child_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 bp->devs[entry].client_claimed = 0;
337 }
338
339 writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0);
340 bp->own = readb(bp->i2c_control_regs + 0x01);
341 writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0);
342 bp->clock = readb(bp->i2c_control_regs + 0x01);
343
344 printk(KERN_INFO "i2c-%d: Regs at %p, %d devices, own %02x, clock %02x.\n",
345 bp->index, bp->i2c_control_regs, entry, bp->own, bp->clock);
346
347 reset_one_i2c(bp);
348
David S. Millere21e2452008-08-29 22:34:14 -0700349 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351fail:
352 if (bp->i2c_bussel_reg)
David S. Millere21e2452008-08-29 22:34:14 -0700353 of_iounmap(&op->resource[1], bp->i2c_bussel_reg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (bp->i2c_control_regs)
David S. Millere21e2452008-08-29 22:34:14 -0700355 of_iounmap(&op->resource[0], bp->i2c_control_regs, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 kfree(bp);
David S. Millere21e2452008-08-29 22:34:14 -0700357 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
David S. Millere21e2452008-08-29 22:34:14 -0700360extern int bbc_envctrl_init(struct bbc_i2c_bus *bp);
361extern void bbc_envctrl_cleanup(struct bbc_i2c_bus *bp);
362
Greg Kroah-Hartman082a2002012-12-21 13:24:23 -0800363static int bbc_i2c_probe(struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
David S. Millere21e2452008-08-29 22:34:14 -0700365 struct bbc_i2c_bus *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 int err, index = 0;
367
David S. Millere21e2452008-08-29 22:34:14 -0700368 bp = attach_one_i2c(op, index);
369 if (!bp)
370 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
David S. Millere21e2452008-08-29 22:34:14 -0700372 err = bbc_envctrl_init(bp);
373 if (err) {
Grant Likely1636f8a2010-06-18 11:09:58 -0600374 free_irq(op->archdata.irqs[0], bp);
David S. Millere21e2452008-08-29 22:34:14 -0700375 if (bp->i2c_bussel_reg)
376 of_iounmap(&op->resource[0], bp->i2c_bussel_reg, 1);
377 if (bp->i2c_control_regs)
378 of_iounmap(&op->resource[1], bp->i2c_control_regs, 2);
379 kfree(bp);
380 } else {
381 dev_set_drvdata(&op->dev, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return err;
385}
386
Greg Kroah-Hartman082a2002012-12-21 13:24:23 -0800387static int bbc_i2c_remove(struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
David S. Millere21e2452008-08-29 22:34:14 -0700389 struct bbc_i2c_bus *bp = dev_get_drvdata(&op->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
David S. Millere21e2452008-08-29 22:34:14 -0700391 bbc_envctrl_cleanup(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Grant Likely1636f8a2010-06-18 11:09:58 -0600393 free_irq(op->archdata.irqs[0], bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
David S. Millere21e2452008-08-29 22:34:14 -0700395 if (bp->i2c_bussel_reg)
396 of_iounmap(&op->resource[0], bp->i2c_bussel_reg, 1);
397 if (bp->i2c_control_regs)
398 of_iounmap(&op->resource[1], bp->i2c_control_regs, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
David S. Millere21e2452008-08-29 22:34:14 -0700400 kfree(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
David S. Millere21e2452008-08-29 22:34:14 -0700402 return 0;
403}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
David S. Millerfd098312008-08-31 01:23:17 -0700405static const struct of_device_id bbc_i2c_match[] = {
David S. Millere21e2452008-08-29 22:34:14 -0700406 {
407 .name = "i2c",
408 .compatible = "SUNW,bbc-i2c",
409 },
410 {},
411};
412MODULE_DEVICE_TABLE(of, bbc_i2c_match);
413
Grant Likely4ebb24f2011-02-22 20:01:33 -0700414static struct platform_driver bbc_i2c_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700415 .driver = {
416 .name = "bbc_i2c",
Grant Likely40182942010-04-13 16:13:02 -0700417 .of_match_table = bbc_i2c_match,
418 },
David S. Millere21e2452008-08-29 22:34:14 -0700419 .probe = bbc_i2c_probe,
Greg Kroah-Hartman082a2002012-12-21 13:24:23 -0800420 .remove = bbc_i2c_remove,
David S. Millere21e2452008-08-29 22:34:14 -0700421};
422
Axel Lindbf2b922011-11-26 19:04:25 +0000423module_platform_driver(bbc_i2c_driver);
David S. Millere21e2452008-08-29 22:34:14 -0700424
David S. Millerb5e7ae52006-03-17 13:23:56 -0800425MODULE_LICENSE("GPL");