Path: blob/master/drivers/i2c/busses/i2c-amd756-s4882.c
15112 views
/*1* i2c-amd756-s4882.c - i2c-amd756 extras for the Tyan S4882 motherboard2*3* Copyright (C) 2004, 2008 Jean Delvare <[email protected]>4*5* This program is free software; you can redistribute it and/or modify6* it under the terms of the GNU General Public License as published by7* the Free Software Foundation; either version 2 of the License, or8* (at your option) any later version.9*10* This program is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13* GNU General Public License for more details.14*15* You should have received a copy of the GNU General Public License16* along with this program; if not, write to the Free Software17* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.18*/1920/*21* We select the channels by sending commands to the Philips22* PCA9556 chip at I2C address 0x18. The main adapter is used for23* the non-multiplexed part of the bus, and 4 virtual adapters24* are defined for the multiplexed addresses: 0x50-0x53 (memory25* module EEPROM) located on channels 1-4, and 0x4c (LM63)26* located on multiplexed channels 0 and 5-7. We define one27* virtual adapter per CPU, which corresponds to two multiplexed28* channels:29* CPU0: virtual adapter 1, channels 1 and 030* CPU1: virtual adapter 2, channels 2 and 531* CPU2: virtual adapter 3, channels 3 and 632* CPU3: virtual adapter 4, channels 4 and 733*/3435#include <linux/module.h>36#include <linux/kernel.h>37#include <linux/slab.h>38#include <linux/init.h>39#include <linux/i2c.h>40#include <linux/mutex.h>4142extern struct i2c_adapter amd756_smbus;4344static struct i2c_adapter *s4882_adapter;45static struct i2c_algorithm *s4882_algo;4647/* Wrapper access functions for multiplexed SMBus */48static DEFINE_MUTEX(amd756_lock);4950static s32 amd756_access_virt0(struct i2c_adapter * adap, u16 addr,51unsigned short flags, char read_write,52u8 command, int size,53union i2c_smbus_data * data)54{55int error;5657/* We exclude the multiplexed addresses */58if (addr == 0x4c || (addr & 0xfc) == 0x50 || (addr & 0xfc) == 0x3059|| addr == 0x18)60return -ENXIO;6162mutex_lock(&amd756_lock);6364error = amd756_smbus.algo->smbus_xfer(adap, addr, flags, read_write,65command, size, data);6667mutex_unlock(&amd756_lock);6869return error;70}7172/* We remember the last used channels combination so as to only switch73channels when it is really needed. This greatly reduces the SMBus74overhead, but also assumes that nobody will be writing to the PCA955675in our back. */76static u8 last_channels;7778static inline s32 amd756_access_channel(struct i2c_adapter * adap, u16 addr,79unsigned short flags, char read_write,80u8 command, int size,81union i2c_smbus_data * data,82u8 channels)83{84int error;8586/* We exclude the non-multiplexed addresses */87if (addr != 0x4c && (addr & 0xfc) != 0x50 && (addr & 0xfc) != 0x30)88return -ENXIO;8990mutex_lock(&amd756_lock);9192if (last_channels != channels) {93union i2c_smbus_data mplxdata;94mplxdata.byte = channels;9596error = amd756_smbus.algo->smbus_xfer(adap, 0x18, 0,97I2C_SMBUS_WRITE, 0x01,98I2C_SMBUS_BYTE_DATA,99&mplxdata);100if (error)101goto UNLOCK;102last_channels = channels;103}104error = amd756_smbus.algo->smbus_xfer(adap, addr, flags, read_write,105command, size, data);106107UNLOCK:108mutex_unlock(&amd756_lock);109return error;110}111112static s32 amd756_access_virt1(struct i2c_adapter * adap, u16 addr,113unsigned short flags, char read_write,114u8 command, int size,115union i2c_smbus_data * data)116{117/* CPU0: channels 1 and 0 enabled */118return amd756_access_channel(adap, addr, flags, read_write, command,119size, data, 0x03);120}121122static s32 amd756_access_virt2(struct i2c_adapter * adap, u16 addr,123unsigned short flags, char read_write,124u8 command, int size,125union i2c_smbus_data * data)126{127/* CPU1: channels 2 and 5 enabled */128return amd756_access_channel(adap, addr, flags, read_write, command,129size, data, 0x24);130}131132static s32 amd756_access_virt3(struct i2c_adapter * adap, u16 addr,133unsigned short flags, char read_write,134u8 command, int size,135union i2c_smbus_data * data)136{137/* CPU2: channels 3 and 6 enabled */138return amd756_access_channel(adap, addr, flags, read_write, command,139size, data, 0x48);140}141142static s32 amd756_access_virt4(struct i2c_adapter * adap, u16 addr,143unsigned short flags, char read_write,144u8 command, int size,145union i2c_smbus_data * data)146{147/* CPU3: channels 4 and 7 enabled */148return amd756_access_channel(adap, addr, flags, read_write, command,149size, data, 0x90);150}151152static int __init amd756_s4882_init(void)153{154int i, error;155union i2c_smbus_data ioconfig;156157if (!amd756_smbus.dev.parent)158return -ENODEV;159160/* Configure the PCA9556 multiplexer */161ioconfig.byte = 0x00; /* All I/O to output mode */162error = i2c_smbus_xfer(&amd756_smbus, 0x18, 0, I2C_SMBUS_WRITE, 0x03,163I2C_SMBUS_BYTE_DATA, &ioconfig);164if (error) {165dev_err(&amd756_smbus.dev, "PCA9556 configuration failed\n");166error = -EIO;167goto ERROR0;168}169170/* Unregister physical bus */171error = i2c_del_adapter(&amd756_smbus);172if (error) {173dev_err(&amd756_smbus.dev, "Physical bus removal failed\n");174goto ERROR0;175}176177printk(KERN_INFO "Enabling SMBus multiplexing for Tyan S4882\n");178/* Define the 5 virtual adapters and algorithms structures */179if (!(s4882_adapter = kzalloc(5 * sizeof(struct i2c_adapter),180GFP_KERNEL))) {181error = -ENOMEM;182goto ERROR1;183}184if (!(s4882_algo = kzalloc(5 * sizeof(struct i2c_algorithm),185GFP_KERNEL))) {186error = -ENOMEM;187goto ERROR2;188}189190/* Fill in the new structures */191s4882_algo[0] = *(amd756_smbus.algo);192s4882_algo[0].smbus_xfer = amd756_access_virt0;193s4882_adapter[0] = amd756_smbus;194s4882_adapter[0].algo = s4882_algo;195s4882_adapter[0].dev.parent = amd756_smbus.dev.parent;196for (i = 1; i < 5; i++) {197s4882_algo[i] = *(amd756_smbus.algo);198s4882_adapter[i] = amd756_smbus;199snprintf(s4882_adapter[i].name, sizeof(s4882_adapter[i].name),200"SMBus 8111 adapter (CPU%d)", i-1);201s4882_adapter[i].algo = s4882_algo+i;202s4882_adapter[i].dev.parent = amd756_smbus.dev.parent;203}204s4882_algo[1].smbus_xfer = amd756_access_virt1;205s4882_algo[2].smbus_xfer = amd756_access_virt2;206s4882_algo[3].smbus_xfer = amd756_access_virt3;207s4882_algo[4].smbus_xfer = amd756_access_virt4;208209/* Register virtual adapters */210for (i = 0; i < 5; i++) {211error = i2c_add_adapter(s4882_adapter+i);212if (error) {213printk(KERN_ERR "i2c-amd756-s4882: "214"Virtual adapter %d registration "215"failed, module not inserted\n", i);216for (i--; i >= 0; i--)217i2c_del_adapter(s4882_adapter+i);218goto ERROR3;219}220}221222return 0;223224ERROR3:225kfree(s4882_algo);226s4882_algo = NULL;227ERROR2:228kfree(s4882_adapter);229s4882_adapter = NULL;230ERROR1:231/* Restore physical bus */232i2c_add_adapter(&amd756_smbus);233ERROR0:234return error;235}236237static void __exit amd756_s4882_exit(void)238{239if (s4882_adapter) {240int i;241242for (i = 0; i < 5; i++)243i2c_del_adapter(s4882_adapter+i);244kfree(s4882_adapter);245s4882_adapter = NULL;246}247kfree(s4882_algo);248s4882_algo = NULL;249250/* Restore physical bus */251if (i2c_add_adapter(&amd756_smbus))252printk(KERN_ERR "i2c-amd756-s4882: "253"Physical bus restoration failed\n");254}255256MODULE_AUTHOR("Jean Delvare <[email protected]>");257MODULE_DESCRIPTION("S4882 SMBus multiplexing");258MODULE_LICENSE("GPL");259260module_init(amd756_s4882_init);261module_exit(amd756_s4882_exit);262263264