Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / misc / lis3lv02d / lis3lv02d_i2c.c
blob6cdc38f6a9a83f671d6030f23aef59911a84568d
1 /*
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
24 * 02110-1301 USA
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 <linux/pm_runtime.h>
33 #include <linux/delay.h>
34 #include "lis3lv02d.h"
36 #define DRV_NAME "lis3lv02d_i2c"
38 static const char reg_vdd[] = "Vdd";
39 static const char reg_vdd_io[] = "Vdd_IO";
41 static int lis3_reg_ctrl(struct lis3lv02d *lis3, bool state)
43 int ret;
44 if (state == LIS3_REG_OFF) {
45 ret = regulator_bulk_disable(ARRAY_SIZE(lis3->regulators),
46 lis3->regulators);
47 } else {
48 ret = regulator_bulk_enable(ARRAY_SIZE(lis3->regulators),
49 lis3->regulators);
50 /* Chip needs time to wakeup. Not mentioned in datasheet */
51 usleep_range(10000, 20000);
53 return ret;
56 static inline s32 lis3_i2c_write(struct lis3lv02d *lis3, int reg, u8 value)
58 struct i2c_client *c = lis3->bus_priv;
59 return i2c_smbus_write_byte_data(c, reg, value);
62 static inline s32 lis3_i2c_read(struct lis3lv02d *lis3, int reg, u8 *v)
64 struct i2c_client *c = lis3->bus_priv;
65 *v = i2c_smbus_read_byte_data(c, reg);
66 return 0;
69 static inline s32 lis3_i2c_blockread(struct lis3lv02d *lis3, int reg, int len,
70 u8 *v)
72 struct i2c_client *c = lis3->bus_priv;
73 reg |= (1 << 7); /* 7th bit enables address auto incrementation */
74 return i2c_smbus_read_i2c_block_data(c, reg, len, v);
77 static int lis3_i2c_init(struct lis3lv02d *lis3)
79 u8 reg;
80 int ret;
82 if (lis3->reg_ctrl)
83 lis3_reg_ctrl(lis3, LIS3_REG_ON);
85 lis3->read(lis3, WHO_AM_I, &reg);
86 if (reg != lis3->whoami)
87 printk(KERN_ERR "lis3: power on failure\n");
89 /* power up the device */
90 ret = lis3->read(lis3, CTRL_REG1, &reg);
91 if (ret < 0)
92 return ret;
94 reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
95 return lis3->write(lis3, CTRL_REG1, reg);
98 /* Default axis mapping but it can be overwritten by platform data */
99 static union axis_conversion lis3lv02d_axis_map =
100 { .as_array = { LIS3_DEV_X, LIS3_DEV_Y, LIS3_DEV_Z } };
102 static int __devinit lis3lv02d_i2c_probe(struct i2c_client *client,
103 const struct i2c_device_id *id)
105 int ret = 0;
106 struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
108 if (pdata) {
109 /* Regulator control is optional */
110 if (pdata->driver_features & LIS3_USE_REGULATOR_CTRL)
111 lis3_dev.reg_ctrl = lis3_reg_ctrl;
113 if ((pdata->driver_features & LIS3_USE_BLOCK_READ) &&
114 (i2c_check_functionality(client->adapter,
115 I2C_FUNC_SMBUS_I2C_BLOCK)))
116 lis3_dev.blkread = lis3_i2c_blockread;
118 if (pdata->axis_x)
119 lis3lv02d_axis_map.x = pdata->axis_x;
121 if (pdata->axis_y)
122 lis3lv02d_axis_map.y = pdata->axis_y;
124 if (pdata->axis_z)
125 lis3lv02d_axis_map.z = pdata->axis_z;
127 if (pdata->setup_resources)
128 ret = pdata->setup_resources();
130 if (ret)
131 goto fail;
134 if (lis3_dev.reg_ctrl) {
135 lis3_dev.regulators[0].supply = reg_vdd;
136 lis3_dev.regulators[1].supply = reg_vdd_io;
137 ret = regulator_bulk_get(&client->dev,
138 ARRAY_SIZE(lis3_dev.regulators),
139 lis3_dev.regulators);
140 if (ret < 0)
141 goto fail;
144 lis3_dev.pdata = pdata;
145 lis3_dev.bus_priv = client;
146 lis3_dev.init = lis3_i2c_init;
147 lis3_dev.read = lis3_i2c_read;
148 lis3_dev.write = lis3_i2c_write;
149 lis3_dev.irq = client->irq;
150 lis3_dev.ac = lis3lv02d_axis_map;
151 lis3_dev.pm_dev = &client->dev;
153 i2c_set_clientdata(client, &lis3_dev);
155 /* Provide power over the init call */
156 if (lis3_dev.reg_ctrl)
157 lis3_reg_ctrl(&lis3_dev, LIS3_REG_ON);
159 ret = lis3lv02d_init_device(&lis3_dev);
161 if (lis3_dev.reg_ctrl)
162 lis3_reg_ctrl(&lis3_dev, LIS3_REG_OFF);
164 if (ret)
165 goto fail2;
166 return 0;
168 fail2:
169 regulator_bulk_free(ARRAY_SIZE(lis3_dev.regulators),
170 lis3_dev.regulators);
171 fail:
172 if (pdata && pdata->release_resources)
173 pdata->release_resources();
174 return ret;
177 static int __devexit lis3lv02d_i2c_remove(struct i2c_client *client)
179 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
180 struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
182 if (pdata && pdata->release_resources)
183 pdata->release_resources();
185 lis3lv02d_joystick_disable(lis3);
186 lis3lv02d_remove_fs(&lis3_dev);
188 if (lis3_dev.reg_ctrl)
189 regulator_bulk_free(ARRAY_SIZE(lis3->regulators),
190 lis3_dev.regulators);
191 return 0;
194 #ifdef CONFIG_PM_SLEEP
195 static int lis3lv02d_i2c_suspend(struct device *dev)
197 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
198 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
200 if (!lis3->pdata || !lis3->pdata->wakeup_flags)
201 lis3lv02d_poweroff(lis3);
202 return 0;
205 static int lis3lv02d_i2c_resume(struct device *dev)
207 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
208 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
211 * pm_runtime documentation says that devices should always
212 * be powered on at resume. Pm_runtime turns them off after system
213 * wide resume is complete.
215 if (!lis3->pdata || !lis3->pdata->wakeup_flags ||
216 pm_runtime_suspended(dev))
217 lis3lv02d_poweron(lis3);
219 return 0;
221 #endif /* CONFIG_PM_SLEEP */
223 #ifdef CONFIG_PM_RUNTIME
224 static int lis3_i2c_runtime_suspend(struct device *dev)
226 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
227 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
229 lis3lv02d_poweroff(lis3);
230 return 0;
233 static int lis3_i2c_runtime_resume(struct device *dev)
235 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
236 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
238 lis3lv02d_poweron(lis3);
239 return 0;
241 #endif /* CONFIG_PM_RUNTIME */
243 static const struct i2c_device_id lis3lv02d_id[] = {
244 {"lis3lv02d", 0 },
248 MODULE_DEVICE_TABLE(i2c, lis3lv02d_id);
250 static const struct dev_pm_ops lis3_pm_ops = {
251 SET_SYSTEM_SLEEP_PM_OPS(lis3lv02d_i2c_suspend,
252 lis3lv02d_i2c_resume)
253 SET_RUNTIME_PM_OPS(lis3_i2c_runtime_suspend,
254 lis3_i2c_runtime_resume,
255 NULL)
258 static struct i2c_driver lis3lv02d_i2c_driver = {
259 .driver = {
260 .name = DRV_NAME,
261 .owner = THIS_MODULE,
262 .pm = &lis3_pm_ops,
264 .probe = lis3lv02d_i2c_probe,
265 .remove = __devexit_p(lis3lv02d_i2c_remove),
266 .id_table = lis3lv02d_id,
269 static int __init lis3lv02d_init(void)
271 return i2c_add_driver(&lis3lv02d_i2c_driver);
274 static void __exit lis3lv02d_exit(void)
276 i2c_del_driver(&lis3lv02d_i2c_driver);
279 MODULE_AUTHOR("Nokia Corporation");
280 MODULE_DESCRIPTION("lis3lv02d I2C interface");
281 MODULE_LICENSE("GPL");
283 module_init(lis3lv02d_init);
284 module_exit(lis3lv02d_exit);