Commit 810ad7b6 authored by Jean Delvare's avatar Jean Delvare Committed by Jean Delvare

hwmon: (ams) Convert to a new-style i2c driver

The legacy i2c binding model is phasing out, so the ams driver needs
to be converted to a new-style i2c driver. Here is a naive approach of
this conversion. Basically it is moving the i2c device creation from
the ams driver to the i2c-powermac driver. This should work, but I
suspect we could come up with something cleaner by declaring the i2c
device as part of the platform setup. This could be done later by
someone more familiar with openfirmware-based platforms than I am
myself.

One nice thing brought by this conversion is that the ams driver
should be loaded automatically on systems where is is needed (at
least when the I2C interface to the chip is used) providing
coldplug-aware user-space environment.
Signed-off-by: default avatarJean Delvare <khali@linux-fr.org>
Acked-by: default avatarJohannes Berg <johannes@sipsolutions.net>
Cc: Stelian Pop <stelian@popies.net>
Cc: Michael Hanselmann <linux-kernel@hansmi.ch>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
parent 47064d64
...@@ -60,26 +60,34 @@ enum ams_i2c_cmd { ...@@ -60,26 +60,34 @@ enum ams_i2c_cmd {
AMS_CMD_START, AMS_CMD_START,
}; };
static int ams_i2c_attach(struct i2c_adapter *adapter); static int ams_i2c_probe(struct i2c_client *client,
static int ams_i2c_detach(struct i2c_adapter *adapter); const struct i2c_device_id *id);
static int ams_i2c_remove(struct i2c_client *client);
static const struct i2c_device_id ams_id[] = {
{ "ams", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c, ams_id);
static struct i2c_driver ams_i2c_driver = { static struct i2c_driver ams_i2c_driver = {
.driver = { .driver = {
.name = "ams", .name = "ams",
.owner = THIS_MODULE, .owner = THIS_MODULE,
}, },
.attach_adapter = ams_i2c_attach, .probe = ams_i2c_probe,
.detach_adapter = ams_i2c_detach, .remove = ams_i2c_remove,
.id_table = ams_id,
}; };
static s32 ams_i2c_read(u8 reg) static s32 ams_i2c_read(u8 reg)
{ {
return i2c_smbus_read_byte_data(&ams_info.i2c_client, reg); return i2c_smbus_read_byte_data(ams_info.i2c_client, reg);
} }
static int ams_i2c_write(u8 reg, u8 value) static int ams_i2c_write(u8 reg, u8 value)
{ {
return i2c_smbus_write_byte_data(&ams_info.i2c_client, reg, value); return i2c_smbus_write_byte_data(ams_info.i2c_client, reg, value);
} }
static int ams_i2c_cmd(enum ams_i2c_cmd cmd) static int ams_i2c_cmd(enum ams_i2c_cmd cmd)
...@@ -152,9 +160,9 @@ static void ams_i2c_get_xyz(s8 *x, s8 *y, s8 *z) ...@@ -152,9 +160,9 @@ static void ams_i2c_get_xyz(s8 *x, s8 *y, s8 *z)
*z = ams_i2c_read(AMS_DATAZ); *z = ams_i2c_read(AMS_DATAZ);
} }
static int ams_i2c_attach(struct i2c_adapter *adapter) static int ams_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{ {
unsigned long bus;
int vmaj, vmin; int vmaj, vmin;
int result; int result;
...@@ -162,17 +170,7 @@ static int ams_i2c_attach(struct i2c_adapter *adapter) ...@@ -162,17 +170,7 @@ static int ams_i2c_attach(struct i2c_adapter *adapter)
if (unlikely(ams_info.has_device)) if (unlikely(ams_info.has_device))
return -ENODEV; return -ENODEV;
if (strncmp(adapter->name, "uni-n", 5)) ams_info.i2c_client = client;
return -ENODEV;
bus = simple_strtoul(adapter->name + 6, NULL, 10);
if (bus != ams_info.i2c_bus)
return -ENODEV;
ams_info.i2c_client.addr = ams_info.i2c_address;
ams_info.i2c_client.adapter = adapter;
ams_info.i2c_client.driver = &ams_i2c_driver;
strcpy(ams_info.i2c_client.name, "Apple Motion Sensor");
if (ams_i2c_cmd(AMS_CMD_RESET)) { if (ams_i2c_cmd(AMS_CMD_RESET)) {
printk(KERN_INFO "ams: Failed to reset the device\n"); printk(KERN_INFO "ams: Failed to reset the device\n");
...@@ -237,7 +235,7 @@ static int ams_i2c_attach(struct i2c_adapter *adapter) ...@@ -237,7 +235,7 @@ static int ams_i2c_attach(struct i2c_adapter *adapter)
return 0; return 0;
} }
static int ams_i2c_detach(struct i2c_adapter *adapter) static int ams_i2c_remove(struct i2c_client *client)
{ {
if (ams_info.has_device) { if (ams_info.has_device) {
/* Disable interrupts */ /* Disable interrupts */
...@@ -261,9 +259,7 @@ static void ams_i2c_exit(void) ...@@ -261,9 +259,7 @@ static void ams_i2c_exit(void)
int __init ams_i2c_init(struct device_node *np) int __init ams_i2c_init(struct device_node *np)
{ {
char *tmp_bus;
int result; int result;
const u32 *prop;
mutex_lock(&ams_info.lock); mutex_lock(&ams_info.lock);
...@@ -275,24 +271,8 @@ int __init ams_i2c_init(struct device_node *np) ...@@ -275,24 +271,8 @@ int __init ams_i2c_init(struct device_node *np)
ams_info.clear_irq = ams_i2c_clear_irq; ams_info.clear_irq = ams_i2c_clear_irq;
ams_info.bustype = BUS_I2C; ams_info.bustype = BUS_I2C;
/* look for bus either using "reg" or by path */
prop = of_get_property(ams_info.of_node, "reg", NULL);
if (!prop) {
result = -ENODEV;
goto exit;
}
tmp_bus = strstr(ams_info.of_node->full_name, "/i2c-bus@");
if (tmp_bus)
ams_info.i2c_bus = *(tmp_bus + 9) - '0';
else
ams_info.i2c_bus = ((*prop) >> 8) & 0x0f;
ams_info.i2c_address = ((*prop) & 0xff) >> 1;
result = i2c_add_driver(&ams_i2c_driver); result = i2c_add_driver(&ams_i2c_driver);
exit:
mutex_unlock(&ams_info.lock); mutex_unlock(&ams_info.lock);
return result; return result;
......
...@@ -46,9 +46,7 @@ struct ams { ...@@ -46,9 +46,7 @@ struct ams {
#ifdef CONFIG_SENSORS_AMS_I2C #ifdef CONFIG_SENSORS_AMS_I2C
/* I2C properties */ /* I2C properties */
int i2c_bus; struct i2c_client *i2c_client;
int i2c_address;
struct i2c_client i2c_client;
#endif #endif
/* Joystick emulation */ /* Joystick emulation */
......
...@@ -259,6 +259,35 @@ static int __devinit i2c_powermac_probe(struct platform_device *dev) ...@@ -259,6 +259,35 @@ static int __devinit i2c_powermac_probe(struct platform_device *dev)
} }
printk(KERN_INFO "PowerMac i2c bus %s registered\n", name); printk(KERN_INFO "PowerMac i2c bus %s registered\n", name);
if (!strncmp(basename, "uni-n", 5)) {
struct device_node *np;
const u32 *prop;
struct i2c_board_info info;
/* Instantiate I2C motion sensor if present */
np = of_find_node_by_name(NULL, "accelerometer");
if (np && of_device_is_compatible(np, "AAPL,accelerometer_1") &&
(prop = of_get_property(np, "reg", NULL))) {
int i2c_bus;
const char *tmp_bus;
/* look for bus either using "reg" or by path */
tmp_bus = strstr(np->full_name, "/i2c-bus@");
if (tmp_bus)
i2c_bus = *(tmp_bus + 9) - '0';
else
i2c_bus = ((*prop) >> 8) & 0x0f;
if (pmac_i2c_get_channel(bus) == i2c_bus) {
memset(&info, 0, sizeof(struct i2c_board_info));
info.addr = ((*prop) & 0xff) >> 1;
strlcpy(info.type, "ams", I2C_NAME_SIZE);
i2c_new_device(adapter, &info);
}
}
}
return rc; return rc;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment