33 #include <linux/types.h>
34 #include <linux/sched.h>
36 #include <linux/export.h>
37 #include <linux/adb.h>
38 #include <linux/pmu.h>
45 #include <linux/i2c.h>
46 #include <linux/slab.h>
51 #include <asm/machdep.h>
57 #define DBG(x...) do {\
58 printk(KERN_DEBUG "low_i2c:" x); \
65 #define DBG_LOW(x...) do {\
66 printk(KERN_DEBUG "low_i2c:" x); \
73 static int pmac_i2c_force_poll = 1;
139 #define KW_POLL_TIMEOUT (2*HZ)
142 #define KW_I2C_MODE_100KHZ 0x00
143 #define KW_I2C_MODE_50KHZ 0x01
144 #define KW_I2C_MODE_25KHZ 0x02
145 #define KW_I2C_MODE_DUMB 0x00
146 #define KW_I2C_MODE_STANDARD 0x04
147 #define KW_I2C_MODE_STANDARDSUB 0x08
148 #define KW_I2C_MODE_COMBINED 0x0C
149 #define KW_I2C_MODE_MODE_MASK 0x0C
150 #define KW_I2C_MODE_CHAN_MASK 0xF0
153 #define KW_I2C_CTL_AAK 0x01
154 #define KW_I2C_CTL_XADDR 0x02
155 #define KW_I2C_CTL_STOP 0x04
156 #define KW_I2C_CTL_START 0x08
159 #define KW_I2C_STAT_BUSY 0x01
160 #define KW_I2C_STAT_LAST_AAK 0x02
161 #define KW_I2C_STAT_LAST_RW 0x04
162 #define KW_I2C_STAT_SDA 0x08
163 #define KW_I2C_STAT_SCL 0x10
166 #define KW_I2C_IRQ_DATA 0x01
167 #define KW_I2C_IRQ_ADDR 0x02
168 #define KW_I2C_IRQ_STOP 0x04
169 #define KW_I2C_IRQ_START 0x08
170 #define KW_I2C_IRQ_MASK 0x0F
182 #define WRONG_STATE(name) do {\
183 printk(KERN_DEBUG "KW: wrong state. Got %s, state: %s " \
185 name, __kw_state_names[host->state], isr); \
188 static const char *__kw_state_names[] = {
209 #define kw_write_reg(reg, val) __kw_write_reg(host, reg, val)
210 #define kw_read_reg(reg) __kw_read_reg(host, reg)
217 for (i = 0; i < 1000; i++) {
227 for (j = 1; j < 100000; j++)
247 DBG_LOW(
"kw_handle_interrupt(%s, isr: %x)\n",
248 __kw_state_names[host->
state], isr);
252 " interrupt, ignoring\n");
261 kw_i2c_do_stop(host, -
EIO);
278 kw_i2c_do_stop(host, -
EIO);
283 DBG_LOW(
"KW: NAK on address\n");
286 kw_i2c_do_stop(host, 0);
308 else if (host->
len == 1)
312 if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
313 DBG_LOW(
"KW: nack on data write\n");
316 }
else if (host->
len) {
320 kw_i2c_do_stop(host, 0);
324 kw_i2c_do_stop(host, -
EIO);
359 spin_unlock_irqrestore(&host->
lock, flags);
363 static void kw_i2c_timeout(
unsigned long data)
383 spin_unlock_irqrestore(&host->
lock, flags);
408 case pmac_i2c_mode_dumb:
410 case pmac_i2c_mode_std:
415 case pmac_i2c_mode_stdsub:
420 case pmac_i2c_mode_combined:
447 host->
rw = (addrdir & 1);
475 u8 isr = kw_i2c_wait_interrupt(host);
477 kw_i2c_handle_interrupt(host, isr);
478 spin_unlock_irqrestore(&host->
lock, flags);
520 steps = psteps ? (*psteps) : 0x10;
521 for (host->
bsteps = 0; (steps & 0x01) == 0; host->
bsteps++)
526 if (prate)
switch(*prate) {
540 "low_i2c: Failed to map interrupt for %s\n",
559 "keywest i2c", host))
581 bus->
busnode = of_node_get(busnode);
582 bus->
type = pmac_i2c_bus_keywest;
585 bus->
mode = pmac_i2c_mode_std;
586 bus->
open = kw_i2c_open;
587 bus->
close = kw_i2c_close;
588 bus->
xfer = kw_i2c_xfer;
590 if (controller == busnode)
591 bus->
flags = pmac_i2c_multibus;
592 list_add(&bus->
link, &pmac_i2c_busses);
595 (controller == busnode) ?
"<multibus>" : busnode->
full_name);
598 static void __init kw_i2c_probe(
void)
603 for_each_compatible_node(np,
"i2c",
"keywest-i2c") {
608 host = kw_i2c_host_init(np);
620 multibus = !child ||
strcmp(child->
name,
"i2c-bus");
632 chans = parent->
name[0] ==
'u' ? 2 : 1;
633 for (i = 0; i < chans; i++)
634 kw_i2c_add(host, np, np, i);
642 kw_i2c_add(host, np, child, *reg);
655 #ifdef CONFIG_ADB_PMU
676 static int pmu_i2c_xfer(
struct pmac_i2c_bus *bus,
u8 addrdir,
int subsize,
677 u32 subaddr,
u8 *data,
int len)
680 struct pmu_i2c_hdr *
hdr = (
struct pmu_i2c_hdr *)&req->
data[1];
682 int read = addrdir & 1;
690 init_completion(&comp);
698 case pmac_i2c_mode_std:
701 hdr->address = addrdir;
704 case pmac_i2c_mode_stdsub:
705 case pmac_i2c_mode_combined:
708 hdr->address = addrdir & 0xfe;
709 hdr->comb_addr = addrdir;
710 hdr->sub_addr = subaddr;
711 if (bus->
mode == pmac_i2c_mode_stdsub)
722 req->
reply[0] = 0xff;
723 req->
nbytes =
sizeof(
struct pmu_i2c_hdr) + 1;
724 req->
done = pmu_i2c_complete;
727 memcpy(hdr->data, data, len);
753 req->
reply[0] = 0xff;
755 req->
done = pmu_i2c_complete;
769 " bytes, expected %d !\n", rlen, len);
780 static void __init pmu_i2c_probe(
void)
804 for (channel = 1; channel <= 2; channel++) {
812 bus->type = pmac_i2c_bus_pmu;
814 bus->mode = pmac_i2c_mode_std;
815 bus->hostdata = bus + 1;
816 bus->xfer = pmu_i2c_xfer;
818 bus->flags = pmac_i2c_multibus;
819 list_add(&bus->link, &pmac_i2c_busses);
834 #ifdef CONFIG_PMAC_SMU
836 static void smu_i2c_complete(
struct smu_i2c_cmd *
cmd,
void *misc)
841 static int smu_i2c_xfer(
struct pmac_i2c_bus *bus,
u8 addrdir,
int subsize,
842 u32 subaddr,
u8 *data,
int len)
846 int read = addrdir & 1;
849 if ((read && len > SMU_I2C_READ_MAX) ||
850 ((!read) && len > SMU_I2C_WRITE_MAX))
853 memset(cmd, 0,
sizeof(
struct smu_i2c_cmd));
855 cmd->info.devaddr = addrdir;
856 cmd->info.datalen = len;
859 case pmac_i2c_mode_std:
864 case pmac_i2c_mode_stdsub:
865 case pmac_i2c_mode_combined:
866 if (subsize > 3 || subsize < 1)
868 cmd->info.sublen = subsize;
870 memcpy(&cmd->info.subaddr, ((
char *)&subaddr) + (4 - subsize),
872 if (bus->
mode == pmac_i2c_mode_stdsub)
881 memcpy(cmd->info.data, data, len);
883 init_completion(&comp);
884 cmd->done = smu_i2c_complete;
893 memcpy(data, cmd->info.data, len);
894 return rc < 0 ? rc : 0;
897 static void __init smu_i2c_probe(
void)
908 if (controller ==
NULL)
910 if (controller ==
NULL)
934 bus->busnode = of_node_get(busnode);
935 bus->type = pmac_i2c_bus_smu;
937 bus->mode = pmac_i2c_mode_std;
938 bus->hostdata = bus + 1;
939 bus->xfer = smu_i2c_xfer;
942 list_add(&bus->link, &pmac_i2c_busses);
967 if (prev && bus->
flags & pmac_i2c_multibus) {
973 if (((*reg) >> 8) != bus->
channel)
996 return (*reg) & 0xff;
1054 return (&bus->
adapter == adapter);
1097 bus->
polled = polled || pmac_i2c_force_poll;
1099 bus->
mode = pmac_i2c_mode_std;
1100 if (bus->
open && (rc = bus->
open(bus)) != 0) {
1126 if (mode < pmac_i2c_mode_dumb || mode > pmac_i2c_mode_combined) {
1128 " bus %s !\n", mode, bus->
busnode->full_name);
1138 u32 subaddr,
u8 *data,
int len)
1144 DBG(
"xfer() chan=%d, addrdir=0x%x, mode=%d, subsize=%d, subaddr=0x%x,"
1145 " %d bytes, bus %s\n", bus->
channel, addrdir, bus->
mode, subsize,
1146 subaddr, len, bus->
busnode->full_name);
1148 rc = bus->
xfer(bus, addrdir, subsize, subaddr, data, len);
1152 DBG(
"xfer error %d\n", rc);
1169 static struct whitelist_ent {
1187 {
"i2c-cpu-voltage",
NULL, 0},
1188 {
"temp-monitor",
NULL, 0 },
1189 {
"supply-monitor",
NULL, 0 },
1201 struct whitelist_ent *
p;
1203 if (bus->
flags & pmac_i2c_multibus)
1209 if (p->compatible &&
1221 #define MAX_I2C_DATA 64
1241 func->
node->full_name);
1246 func->
node->full_name);
1266 static void pmac_i2c_do_end(
struct pmf_function *func,
void *instdata)
1305 for (i = 0; i < len; i ++)
1308 for (i = 0; i < len; i ++)
1310 | (val[
i] & mask[
i]);
1315 u32 totallen,
const u8 *maskdata,
1316 const u8 *valuedata)
1320 if (masklen > inst->
bytes || valuelen > inst->
bytes ||
1321 totallen > inst->
bytes || valuelen > masklen)
1324 pmac_i2c_do_apply_rmw(inst, masklen, maskdata, valuedata);
1345 subaddr, (
u8 *)data, len);
1356 u32 valuelen,
u32 totallen,
const u8 *maskdata,
1357 const u8 *valuedata)
1361 if (masklen > inst->
bytes || valuelen > inst->
bytes ||
1362 totallen > inst->
bytes || valuelen > masklen)
1365 pmac_i2c_do_apply_rmw(inst, masklen, maskdata, valuedata);
1368 subaddr, inst->
scratch, totallen);
1373 const u8 *valuedata)
1379 if (!args || !args->
count || !args->
u[0].
p)
1383 if (len > inst->
bytes)
1386 for (i = 0, match = 1; match && i < len; i ++)
1387 if ((inst->
buffer[i] & maskdata[i]) != valuedata[i])
1395 msleep((duration + 999) / 1000);
1401 .begin = pmac_i2c_do_begin,
1402 .end = pmac_i2c_do_end,
1403 .read_i2c = pmac_i2c_do_read,
1404 .write_i2c = pmac_i2c_do_write,
1405 .rmw_i2c = pmac_i2c_do_rmw,
1406 .read_i2c_sub = pmac_i2c_do_read_sub,
1407 .write_i2c_sub = pmac_i2c_do_write_sub,
1408 .rmw_i2c_sub = pmac_i2c_do_rmw_sub,
1409 .set_i2c_mode = pmac_i2c_do_set_mode,
1410 .mask_and_compare = pmac_i2c_do_mask_and_comp,
1411 .delay = pmac_i2c_do_delay,
1419 (
void *)(
long)quirks);
1429 static void pmac_i2c_dev_suspend(
struct device_node *np,
int quirks)
1435 static void pmac_i2c_dev_resume(
struct device_node *np,
int quirks)
1443 pmac_i2c_devscan(pmac_i2c_dev_suspend);
1448 pmac_i2c_devscan(pmac_i2c_dev_resume);
1458 static int i2c_inited;
1467 #ifdef CONFIG_ADB_PMU
1472 #ifdef CONFIG_PMAC_SMU
1478 pmac_i2c_devscan(pmac_i2c_dev_create);
1488 static int __init pmac_i2c_create_platform_devices(
void)
1497 pmac_i2c_force_poll = 0;
1511 pmac_i2c_devscan(pmac_i2c_dev_init);