blob: c6f3f341127697669df47bb2133036fa64fed613 [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>
32#include "lis3lv02d.h"
33
34#define DRV_NAME "lis3lv02d_i2c"
35
36static inline s32 lis3_i2c_write(struct lis3lv02d *lis3, int reg, u8 value)
37{
38 struct i2c_client *c = lis3->bus_priv;
39 return i2c_smbus_write_byte_data(c, reg, value);
40}
41
42static inline s32 lis3_i2c_read(struct lis3lv02d *lis3, int reg, u8 *v)
43{
44 struct i2c_client *c = lis3->bus_priv;
45 *v = i2c_smbus_read_byte_data(c, reg);
46 return 0;
47}
48
49static int lis3_i2c_init(struct lis3lv02d *lis3)
50{
51 u8 reg;
52 int ret;
53
54 /* power up the device */
55 ret = lis3->read(lis3, CTRL_REG1, &reg);
56 if (ret < 0)
57 return ret;
58
59 reg |= CTRL1_PD0;
60 return lis3->write(lis3, CTRL_REG1, reg);
61}
62
63/* Default axis mapping but it can be overwritten by platform data */
Takashi Iwai2ee32142010-10-01 17:14:25 -040064static union axis_conversion lis3lv02d_axis_map =
65 { .as_array = { LIS3_DEV_X, LIS3_DEV_Y, LIS3_DEV_Z } };
Samu Onkalo312ea072009-12-17 15:27:07 -080066
67static int __devinit lis3lv02d_i2c_probe(struct i2c_client *client,
68 const struct i2c_device_id *id)
69{
70 int ret = 0;
71 struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
72
73 if (pdata) {
74 if (pdata->axis_x)
75 lis3lv02d_axis_map.x = pdata->axis_x;
76
77 if (pdata->axis_y)
78 lis3lv02d_axis_map.y = pdata->axis_y;
79
80 if (pdata->axis_z)
81 lis3lv02d_axis_map.z = pdata->axis_z;
82
83 if (pdata->setup_resources)
84 ret = pdata->setup_resources();
85
86 if (ret)
87 goto fail;
88 }
89
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;
97
98 i2c_set_clientdata(client, &lis3_dev);
99 ret = lis3lv02d_init_device(&lis3_dev);
100fail:
101 return ret;
102}
103
104static int __devexit lis3lv02d_i2c_remove(struct i2c_client *client)
105{
106 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
107 struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
108
109 if (pdata && pdata->release_resources)
110 pdata->release_resources();
111
112 lis3lv02d_joystick_disable();
113 lis3lv02d_poweroff(lis3);
114
115 return lis3lv02d_remove_fs(&lis3_dev);
116}
117
118#ifdef CONFIG_PM
119static int lis3lv02d_i2c_suspend(struct i2c_client *client, pm_message_t mesg)
120{
121 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
122
Kuninori Morimoto5facb092010-09-17 17:24:10 +0200123 if (!lis3->pdata || !lis3->pdata->wakeup_flags)
Samu Onkalo312ea072009-12-17 15:27:07 -0800124 lis3lv02d_poweroff(lis3);
125 return 0;
126}
127
128static int lis3lv02d_i2c_resume(struct i2c_client *client)
129{
130 struct lis3lv02d *lis3 = i2c_get_clientdata(client);
131
Kuninori Morimoto5facb092010-09-17 17:24:10 +0200132 if (!lis3->pdata || !lis3->pdata->wakeup_flags)
Samu Onkalo312ea072009-12-17 15:27:07 -0800133 lis3lv02d_poweron(lis3);
134 return 0;
135}
136
137static void lis3lv02d_i2c_shutdown(struct i2c_client *client)
138{
139 lis3lv02d_i2c_suspend(client, PMSG_SUSPEND);
140}
141#else
142#define lis3lv02d_i2c_suspend NULL
143#define lis3lv02d_i2c_resume NULL
144#define lis3lv02d_i2c_shutdown NULL
145#endif
146
147static const struct i2c_device_id lis3lv02d_id[] = {
148 {"lis3lv02d", 0 },
149 {}
150};
151
152MODULE_DEVICE_TABLE(i2c, lis3lv02d_id);
153
154static struct i2c_driver lis3lv02d_i2c_driver = {
155 .driver = {
156 .name = DRV_NAME,
157 .owner = THIS_MODULE,
158 },
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,
165};
166
167static int __init lis3lv02d_init(void)
168{
169 return i2c_add_driver(&lis3lv02d_i2c_driver);
170}
171
172static void __exit lis3lv02d_exit(void)
173{
174 i2c_del_driver(&lis3lv02d_i2c_driver);
175}
176
177MODULE_AUTHOR("Nokia Corporation");
178MODULE_DESCRIPTION("lis3lv02d I2C interface");
179MODULE_LICENSE("GPL");
180
181module_init(lis3lv02d_init);
182module_exit(lis3lv02d_exit);