2 * drivers/hwmon/lis3lv02d_i2c.c
4 * Implements I2C interface for lis3lv02d (STMicroelectronics) accelerometer.
5 * Driver is based on corresponding SPI driver written by Daniel Mack
6 * (lis3lv02d_spi.c (C) 2009 Daniel Mack <daniel@caiaq.de> ).
8 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
10 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * version 2 as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/err.h>
31 #include <linux/i2c.h>
32 #include "lis3lv02d.h"
34 #define DRV_NAME "lis3lv02d_i2c"
36 static inline s32 lis3_i2c_write(struct lis3lv02d *lis3, int reg, u8 value)
38 struct i2c_client *c = lis3->bus_priv;
39 return i2c_smbus_write_byte_data(c, reg, value);
42 static inline s32 lis3_i2c_read(struct lis3lv02d *lis3, int reg, u8 *v)
44 struct i2c_client *c = lis3->bus_priv;
45 *v = i2c_smbus_read_byte_data(c, reg);
49 static int lis3_i2c_init(struct lis3lv02d *lis3)
54 /* power up the device */
55 ret = lis3->read(lis3, CTRL_REG1, ®);
60 return lis3->write(lis3, CTRL_REG1, reg);
63 /* Default axis mapping but it can be overwritten by platform data */
64 static union axis_conversion lis3lv02d_axis_map =
65 { .as_array = { LIS3_DEV_X, LIS3_DEV_Y, LIS3_DEV_Z } };
67 static int __devinit lis3lv02d_i2c_probe(struct i2c_client *client,
68 const struct i2c_device_id *id)
71 struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
75 lis3lv02d_axis_map.x = pdata->axis_x;
78 lis3lv02d_axis_map.y = pdata->axis_y;
81 lis3lv02d_axis_map.z = pdata->axis_z;
83 if (pdata->setup_resources)
84 ret = pdata->setup_resources();
90 lis3_dev.pdata = pdata;
91 lis3_dev.bus_priv = client;
92 lis3_dev.init = lis3_i2c_init;
93 lis3_dev.read = lis3_i2c_read;
94 lis3_dev.write = lis3_i2c_write;
95 lis3_dev.irq = client->irq;
96 lis3_dev.ac = lis3lv02d_axis_map;
98 i2c_set_clientdata(client, &lis3_dev);
99 ret = lis3lv02d_init_device(&lis3_dev);
104 static int __devexit lis3lv02d_i2c_remove(struct i2c_client *client)
106 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
107 struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
109 if (pdata && pdata->release_resources)
110 pdata->release_resources();
112 lis3lv02d_joystick_disable();
113 lis3lv02d_poweroff(lis3);
115 return lis3lv02d_remove_fs(&lis3_dev);
119 static int lis3lv02d_i2c_suspend(struct i2c_client *client, pm_message_t mesg)
121 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
123 if (!lis3->pdata || !lis3->pdata->wakeup_flags)
124 lis3lv02d_poweroff(lis3);
128 static int lis3lv02d_i2c_resume(struct i2c_client *client)
130 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
132 if (!lis3->pdata || !lis3->pdata->wakeup_flags)
133 lis3lv02d_poweron(lis3);
137 static void lis3lv02d_i2c_shutdown(struct i2c_client *client)
139 lis3lv02d_i2c_suspend(client, PMSG_SUSPEND);
142 #define lis3lv02d_i2c_suspend NULL
143 #define lis3lv02d_i2c_resume NULL
144 #define lis3lv02d_i2c_shutdown NULL
147 static const struct i2c_device_id lis3lv02d_id[] = {
152 MODULE_DEVICE_TABLE(i2c, lis3lv02d_id);
154 static struct i2c_driver lis3lv02d_i2c_driver = {
157 .owner = THIS_MODULE,
159 .suspend = lis3lv02d_i2c_suspend,
160 .shutdown = lis3lv02d_i2c_shutdown,
161 .resume = lis3lv02d_i2c_resume,
162 .probe = lis3lv02d_i2c_probe,
163 .remove = __devexit_p(lis3lv02d_i2c_remove),
164 .id_table = lis3lv02d_id,
167 static int __init lis3lv02d_init(void)
169 return i2c_add_driver(&lis3lv02d_i2c_driver);
172 static void __exit lis3lv02d_exit(void)
174 i2c_del_driver(&lis3lv02d_i2c_driver);
177 MODULE_AUTHOR("Nokia Corporation");
178 MODULE_DESCRIPTION("lis3lv02d I2C interface");
179 MODULE_LICENSE("GPL");
181 module_init(lis3lv02d_init);
182 module_exit(lis3lv02d_exit);