blob: d52095603e033e209d5e96054329416496beb06c [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 Onkalo312ea072009-12-17 15:27:07 -080033#include "lis3lv02d.h"
34
35#define DRV_NAME "lis3lv02d_i2c"
36
37static inline s32 lis3_i2c_write(struct lis3lv02d *lis3, int reg, u8 value)
38{
39 struct i2c_client *c = lis3->bus_priv;
40 return i2c_smbus_write_byte_data(c, reg, value);
41}
42
43static inline s32 lis3_i2c_read(struct lis3lv02d *lis3, int reg, u8 *v)
44{
45 struct i2c_client *c = lis3->bus_priv;
46 *v = i2c_smbus_read_byte_data(c, reg);
47 return 0;
48}
49
50static int lis3_i2c_init(struct lis3lv02d *lis3)
51{
52 u8 reg;
53 int ret;
54
55 /* power up the device */
56 ret = lis3->read(lis3, CTRL_REG1, &reg);
57 if (ret < 0)
58 return ret;
59
60 reg |= CTRL1_PD0;
61 return lis3->write(lis3, CTRL_REG1, reg);
62}
63
64/* Default axis mapping but it can be overwritten by platform data */
Takashi Iwai2ee32142010-10-01 17:14:25 -040065static union axis_conversion lis3lv02d_axis_map =
66 { .as_array = { LIS3_DEV_X, LIS3_DEV_Y, LIS3_DEV_Z } };
Samu Onkalo312ea072009-12-17 15:27:07 -080067
68static int __devinit lis3lv02d_i2c_probe(struct i2c_client *client,
69 const struct i2c_device_id *id)
70{
71 int ret = 0;
72 struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
73
74 if (pdata) {
75 if (pdata->axis_x)
76 lis3lv02d_axis_map.x = pdata->axis_x;
77
78 if (pdata->axis_y)
79 lis3lv02d_axis_map.y = pdata->axis_y;
80
81 if (pdata->axis_z)
82 lis3lv02d_axis_map.z = pdata->axis_z;
83
84 if (pdata->setup_resources)
85 ret = pdata->setup_resources();
86
87 if (ret)
88 goto fail;
89 }
90
91 lis3_dev.pdata = pdata;
92 lis3_dev.bus_priv = client;
93 lis3_dev.init = lis3_i2c_init;
94 lis3_dev.read = lis3_i2c_read;
95 lis3_dev.write = lis3_i2c_write;
96 lis3_dev.irq = client->irq;
97 lis3_dev.ac = lis3lv02d_axis_map;
Samu Onkalo2a346992010-10-22 07:57:23 -040098 lis3_dev.pm_dev = &client->dev;
Samu Onkalo312ea072009-12-17 15:27:07 -080099
100 i2c_set_clientdata(client, &lis3_dev);
101 ret = lis3lv02d_init_device(&lis3_dev);
102fail:
103 return ret;
104}
105
106static int __devexit lis3lv02d_i2c_remove(struct i2c_client *client)
107{
Samu Onkalo312ea072009-12-17 15:27:07 -0800108 struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
109
110 if (pdata && pdata->release_resources)
111 pdata->release_resources();
112
113 lis3lv02d_joystick_disable();
Samu Onkalo312ea072009-12-17 15:27:07 -0800114
115 return lis3lv02d_remove_fs(&lis3_dev);
116}
117
118#ifdef CONFIG_PM
Samu Onkalo2a346992010-10-22 07:57:23 -0400119static int lis3lv02d_i2c_suspend(struct device *dev)
Samu Onkalo312ea072009-12-17 15:27:07 -0800120{
Samu Onkalo2a346992010-10-22 07:57:23 -0400121 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
Samu Onkalo312ea072009-12-17 15:27:07 -0800122 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
123
Kuninori Morimoto5facb092010-09-17 17:24:10 +0200124 if (!lis3->pdata || !lis3->pdata->wakeup_flags)
Samu Onkalo312ea072009-12-17 15:27:07 -0800125 lis3lv02d_poweroff(lis3);
126 return 0;
127}
128
Samu Onkalo2a346992010-10-22 07:57:23 -0400129static int lis3lv02d_i2c_resume(struct device *dev)
Samu Onkalo312ea072009-12-17 15:27:07 -0800130{
Samu Onkalo2a346992010-10-22 07:57:23 -0400131 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
Samu Onkalo312ea072009-12-17 15:27:07 -0800132 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
133
Samu Onkalo2a346992010-10-22 07:57:23 -0400134 /*
135 * pm_runtime documentation says that devices should always
136 * be powered on at resume. Pm_runtime turns them off after system
137 * wide resume is complete.
138 */
139 if (!lis3->pdata || !lis3->pdata->wakeup_flags ||
140 pm_runtime_suspended(dev))
Samu Onkalo312ea072009-12-17 15:27:07 -0800141 lis3lv02d_poweron(lis3);
Samu Onkalo312ea072009-12-17 15:27:07 -0800142
Samu Onkalo2a346992010-10-22 07:57:23 -0400143 return 0;
Samu Onkalo312ea072009-12-17 15:27:07 -0800144}
145#else
146#define lis3lv02d_i2c_suspend NULL
147#define lis3lv02d_i2c_resume NULL
148#define lis3lv02d_i2c_shutdown NULL
149#endif
150
Samu Onkalo2a346992010-10-22 07:57:23 -0400151static int lis3_i2c_runtime_suspend(struct device *dev)
152{
153 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
154 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
155
156 lis3lv02d_poweroff(lis3);
157 return 0;
158}
159
160static int lis3_i2c_runtime_resume(struct device *dev)
161{
162 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
163 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
164
165 lis3lv02d_poweron(lis3);
166 return 0;
167}
168
Samu Onkalo312ea072009-12-17 15:27:07 -0800169static const struct i2c_device_id lis3lv02d_id[] = {
170 {"lis3lv02d", 0 },
171 {}
172};
173
174MODULE_DEVICE_TABLE(i2c, lis3lv02d_id);
175
Samu Onkalo2a346992010-10-22 07:57:23 -0400176static const struct dev_pm_ops lis3_pm_ops = {
177 SET_SYSTEM_SLEEP_PM_OPS(lis3lv02d_i2c_suspend,
178 lis3lv02d_i2c_resume)
179 SET_RUNTIME_PM_OPS(lis3_i2c_runtime_suspend,
180 lis3_i2c_runtime_resume,
181 NULL)
182};
183
Samu Onkalo312ea072009-12-17 15:27:07 -0800184static struct i2c_driver lis3lv02d_i2c_driver = {
185 .driver = {
186 .name = DRV_NAME,
187 .owner = THIS_MODULE,
Samu Onkalo2a346992010-10-22 07:57:23 -0400188 .pm = &lis3_pm_ops,
Samu Onkalo312ea072009-12-17 15:27:07 -0800189 },
Samu Onkalo312ea072009-12-17 15:27:07 -0800190 .probe = lis3lv02d_i2c_probe,
191 .remove = __devexit_p(lis3lv02d_i2c_remove),
192 .id_table = lis3lv02d_id,
193};
194
195static int __init lis3lv02d_init(void)
196{
197 return i2c_add_driver(&lis3lv02d_i2c_driver);
198}
199
200static void __exit lis3lv02d_exit(void)
201{
202 i2c_del_driver(&lis3lv02d_i2c_driver);
203}
204
205MODULE_AUTHOR("Nokia Corporation");
206MODULE_DESCRIPTION("lis3lv02d I2C interface");
207MODULE_LICENSE("GPL");
208
209module_init(lis3lv02d_init);
210module_exit(lis3lv02d_exit);