blob: c02fea029dcf1bcd971199eea32c9c79cb9fa0fc [file] [log] [blame]
Samu Onkalo312ea072009-12-17 15:27:07 -08001/*
2 * drivers/hwmon/lis3lv02d_i2c.c
3 *
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> ).
7 *
8 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
9 *
10 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
11 *
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.
15 *
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.
20 *
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
25 */
26
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>
Samu Onkalo2a346992010-10-22 07:57:23 -040032#include <linux/pm_runtime.h>
Samu Onkalof9deb412010-10-22 07:57:24 -040033#include <linux/delay.h>
Samu Onkalo312ea072009-12-17 15:27:07 -080034#include "lis3lv02d.h"
35
Jean Delvareff606672011-03-21 17:59:36 +010036#define DRV_NAME "lis3lv02d_i2c"
Samu Onkalo312ea072009-12-17 15:27:07 -080037
Samu Onkalof9deb412010-10-22 07:57:24 -040038static const char reg_vdd[] = "Vdd";
39static const char reg_vdd_io[] = "Vdd_IO";
40
41static int lis3_reg_ctrl(struct lis3lv02d *lis3, bool state)
42{
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);
52 }
53 return ret;
54}
55
Samu Onkalo312ea072009-12-17 15:27:07 -080056static inline s32 lis3_i2c_write(struct lis3lv02d *lis3, int reg, u8 value)
57{
58 struct i2c_client *c = lis3->bus_priv;
59 return i2c_smbus_write_byte_data(c, reg, value);
60}
61
62static inline s32 lis3_i2c_read(struct lis3lv02d *lis3, int reg, u8 *v)
63{
64 struct i2c_client *c = lis3->bus_priv;
65 *v = i2c_smbus_read_byte_data(c, reg);
66 return 0;
67}
68
Samu Onkalof10a5402010-10-22 07:57:31 -040069static inline s32 lis3_i2c_blockread(struct lis3lv02d *lis3, int reg, int len,
70 u8 *v)
71{
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);
75}
76
Samu Onkalo312ea072009-12-17 15:27:07 -080077static int lis3_i2c_init(struct lis3lv02d *lis3)
78{
79 u8 reg;
80 int ret;
81
Mark Brownec400c92011-10-31 17:11:07 -070082 lis3_reg_ctrl(lis3, LIS3_REG_ON);
Samu Onkalof9deb412010-10-22 07:57:24 -040083
84 lis3->read(lis3, WHO_AM_I, &reg);
85 if (reg != lis3->whoami)
86 printk(KERN_ERR "lis3: power on failure\n");
87
Samu Onkalo312ea072009-12-17 15:27:07 -080088 /* power up the device */
89 ret = lis3->read(lis3, CTRL_REG1, &reg);
90 if (ret < 0)
91 return ret;
92
Samu Onkaloed37d7f2010-10-22 07:57:28 -040093 reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
Samu Onkalo312ea072009-12-17 15:27:07 -080094 return lis3->write(lis3, CTRL_REG1, reg);
95}
96
97/* Default axis mapping but it can be overwritten by platform data */
Takashi Iwai2ee32142010-10-01 17:14:25 -040098static union axis_conversion lis3lv02d_axis_map =
99 { .as_array = { LIS3_DEV_X, LIS3_DEV_Y, LIS3_DEV_Z } };
Samu Onkalo312ea072009-12-17 15:27:07 -0800100
101static int __devinit lis3lv02d_i2c_probe(struct i2c_client *client,
102 const struct i2c_device_id *id)
103{
104 int ret = 0;
105 struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
106
107 if (pdata) {
Samu Onkalof10a5402010-10-22 07:57:31 -0400108 if ((pdata->driver_features & LIS3_USE_BLOCK_READ) &&
109 (i2c_check_functionality(client->adapter,
110 I2C_FUNC_SMBUS_I2C_BLOCK)))
111 lis3_dev.blkread = lis3_i2c_blockread;
112
Samu Onkalo312ea072009-12-17 15:27:07 -0800113 if (pdata->axis_x)
114 lis3lv02d_axis_map.x = pdata->axis_x;
115
116 if (pdata->axis_y)
117 lis3lv02d_axis_map.y = pdata->axis_y;
118
119 if (pdata->axis_z)
120 lis3lv02d_axis_map.z = pdata->axis_z;
121
122 if (pdata->setup_resources)
123 ret = pdata->setup_resources();
124
125 if (ret)
126 goto fail;
127 }
128
Mark Brownec400c92011-10-31 17:11:07 -0700129 lis3_dev.regulators[0].supply = reg_vdd;
130 lis3_dev.regulators[1].supply = reg_vdd_io;
131 ret = regulator_bulk_get(&client->dev,
132 ARRAY_SIZE(lis3_dev.regulators),
133 lis3_dev.regulators);
134 if (ret < 0)
135 goto fail;
Samu Onkalof9deb412010-10-22 07:57:24 -0400136
Samu Onkalo312ea072009-12-17 15:27:07 -0800137 lis3_dev.pdata = pdata;
138 lis3_dev.bus_priv = client;
139 lis3_dev.init = lis3_i2c_init;
140 lis3_dev.read = lis3_i2c_read;
141 lis3_dev.write = lis3_i2c_write;
142 lis3_dev.irq = client->irq;
143 lis3_dev.ac = lis3lv02d_axis_map;
Samu Onkalo2a346992010-10-22 07:57:23 -0400144 lis3_dev.pm_dev = &client->dev;
Samu Onkalo312ea072009-12-17 15:27:07 -0800145
146 i2c_set_clientdata(client, &lis3_dev);
Samu Onkalof9deb412010-10-22 07:57:24 -0400147
148 /* Provide power over the init call */
Mark Brownec400c92011-10-31 17:11:07 -0700149 lis3_reg_ctrl(&lis3_dev, LIS3_REG_ON);
Samu Onkalof9deb412010-10-22 07:57:24 -0400150
Samu Onkalo312ea072009-12-17 15:27:07 -0800151 ret = lis3lv02d_init_device(&lis3_dev);
Samu Onkalof9deb412010-10-22 07:57:24 -0400152
Mark Brownec400c92011-10-31 17:11:07 -0700153 lis3_reg_ctrl(&lis3_dev, LIS3_REG_OFF);
Samu Onkalob11e7b32010-10-22 07:57:34 -0400154
Éric Piel00215862011-10-31 17:10:54 -0700155 if (ret)
156 goto fail2;
157 return 0;
158
159fail2:
160 regulator_bulk_free(ARRAY_SIZE(lis3_dev.regulators),
161 lis3_dev.regulators);
Samu Onkalo312ea072009-12-17 15:27:07 -0800162fail:
Samu Onkalob11e7b32010-10-22 07:57:34 -0400163 if (pdata && pdata->release_resources)
164 pdata->release_resources();
Samu Onkalo312ea072009-12-17 15:27:07 -0800165 return ret;
166}
167
168static int __devexit lis3lv02d_i2c_remove(struct i2c_client *client)
169{
Samu Onkalof9deb412010-10-22 07:57:24 -0400170 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
Samu Onkalo312ea072009-12-17 15:27:07 -0800171 struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
172
173 if (pdata && pdata->release_resources)
174 pdata->release_resources();
175
Éric Piele1e56872011-10-31 17:11:02 -0700176 lis3lv02d_joystick_disable(lis3);
Samu Onkalof9deb412010-10-22 07:57:24 -0400177 lis3lv02d_remove_fs(&lis3_dev);
Samu Onkalo312ea072009-12-17 15:27:07 -0800178
Mark Brownec400c92011-10-31 17:11:07 -0700179 regulator_bulk_free(ARRAY_SIZE(lis3->regulators),
180 lis3_dev.regulators);
Samu Onkalof9deb412010-10-22 07:57:24 -0400181 return 0;
Samu Onkalo312ea072009-12-17 15:27:07 -0800182}
183
Guenter Roeck32292f42010-11-18 21:06:45 -0800184#ifdef CONFIG_PM_SLEEP
Samu Onkalo2a346992010-10-22 07:57:23 -0400185static int lis3lv02d_i2c_suspend(struct device *dev)
Samu Onkalo312ea072009-12-17 15:27:07 -0800186{
Samu Onkalo2a346992010-10-22 07:57:23 -0400187 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
Samu Onkalo312ea072009-12-17 15:27:07 -0800188 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
189
Kuninori Morimoto5facb092010-09-17 17:24:10 +0200190 if (!lis3->pdata || !lis3->pdata->wakeup_flags)
Samu Onkalo312ea072009-12-17 15:27:07 -0800191 lis3lv02d_poweroff(lis3);
192 return 0;
193}
194
Samu Onkalo2a346992010-10-22 07:57:23 -0400195static int lis3lv02d_i2c_resume(struct device *dev)
Samu Onkalo312ea072009-12-17 15:27:07 -0800196{
Samu Onkalo2a346992010-10-22 07:57:23 -0400197 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
Samu Onkalo312ea072009-12-17 15:27:07 -0800198 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
199
Samu Onkalo2a346992010-10-22 07:57:23 -0400200 /*
201 * pm_runtime documentation says that devices should always
202 * be powered on at resume. Pm_runtime turns them off after system
203 * wide resume is complete.
204 */
205 if (!lis3->pdata || !lis3->pdata->wakeup_flags ||
206 pm_runtime_suspended(dev))
Samu Onkalo312ea072009-12-17 15:27:07 -0800207 lis3lv02d_poweron(lis3);
Samu Onkalo312ea072009-12-17 15:27:07 -0800208
Samu Onkalo2a346992010-10-22 07:57:23 -0400209 return 0;
Samu Onkalo312ea072009-12-17 15:27:07 -0800210}
Guenter Roeck32292f42010-11-18 21:06:45 -0800211#endif /* CONFIG_PM_SLEEP */
Samu Onkalo312ea072009-12-17 15:27:07 -0800212
Guenter Roeck32292f42010-11-18 21:06:45 -0800213#ifdef CONFIG_PM_RUNTIME
Samu Onkalo2a346992010-10-22 07:57:23 -0400214static int lis3_i2c_runtime_suspend(struct device *dev)
215{
216 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
217 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
218
219 lis3lv02d_poweroff(lis3);
220 return 0;
221}
222
223static int lis3_i2c_runtime_resume(struct device *dev)
224{
225 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
226 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
227
228 lis3lv02d_poweron(lis3);
229 return 0;
230}
Guenter Roeck32292f42010-11-18 21:06:45 -0800231#endif /* CONFIG_PM_RUNTIME */
Samu Onkalo2a346992010-10-22 07:57:23 -0400232
Samu Onkalo312ea072009-12-17 15:27:07 -0800233static const struct i2c_device_id lis3lv02d_id[] = {
234 {"lis3lv02d", 0 },
235 {}
236};
237
238MODULE_DEVICE_TABLE(i2c, lis3lv02d_id);
239
Samu Onkalo2a346992010-10-22 07:57:23 -0400240static const struct dev_pm_ops lis3_pm_ops = {
241 SET_SYSTEM_SLEEP_PM_OPS(lis3lv02d_i2c_suspend,
242 lis3lv02d_i2c_resume)
243 SET_RUNTIME_PM_OPS(lis3_i2c_runtime_suspend,
244 lis3_i2c_runtime_resume,
245 NULL)
246};
247
Samu Onkalo312ea072009-12-17 15:27:07 -0800248static struct i2c_driver lis3lv02d_i2c_driver = {
249 .driver = {
250 .name = DRV_NAME,
251 .owner = THIS_MODULE,
Samu Onkalo2a346992010-10-22 07:57:23 -0400252 .pm = &lis3_pm_ops,
Samu Onkalo312ea072009-12-17 15:27:07 -0800253 },
Samu Onkalo312ea072009-12-17 15:27:07 -0800254 .probe = lis3lv02d_i2c_probe,
255 .remove = __devexit_p(lis3lv02d_i2c_remove),
256 .id_table = lis3lv02d_id,
257};
258
259static int __init lis3lv02d_init(void)
260{
261 return i2c_add_driver(&lis3lv02d_i2c_driver);
262}
263
264static void __exit lis3lv02d_exit(void)
265{
266 i2c_del_driver(&lis3lv02d_i2c_driver);
267}
268
269MODULE_AUTHOR("Nokia Corporation");
270MODULE_DESCRIPTION("lis3lv02d I2C interface");
271MODULE_LICENSE("GPL");
272
273module_init(lis3lv02d_init);
274module_exit(lis3lv02d_exit);