blob: c52d278a794218bcf33b10f414183352ce858ebf [file] [log] [blame]
Hemanth Vb029ffa2010-11-30 23:03:54 -08001/*
2 * Implements I2C interface for VTI CMA300_D0x Accelerometer driver
3 *
4 * Copyright (C) 2010 Texas Instruments
5 * Author: Hemanth V <hemanthv@ti.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <linux/module.h>
21#include <linux/i2c.h>
22#include <linux/input/cma3000.h>
23#include "cma3000_d0x.h"
24
25static int cma3000_i2c_set(struct device *dev,
26 u8 reg, u8 val, char *msg)
27{
28 struct i2c_client *client = to_i2c_client(dev);
29 int ret;
30
31 ret = i2c_smbus_write_byte_data(client, reg, val);
32 if (ret < 0)
33 dev_err(&client->dev,
34 "%s failed (%s, %d)\n", __func__, msg, ret);
35 return ret;
36}
37
38static int cma3000_i2c_read(struct device *dev, u8 reg, char *msg)
39{
40 struct i2c_client *client = to_i2c_client(dev);
41 int ret;
42
43 ret = i2c_smbus_read_byte_data(client, reg);
44 if (ret < 0)
45 dev_err(&client->dev,
46 "%s failed (%s, %d)\n", __func__, msg, ret);
47 return ret;
48}
49
50static const struct cma3000_bus_ops cma3000_i2c_bops = {
51 .bustype = BUS_I2C,
52#define CMA3000_BUSI2C (0 << 4)
53 .ctrl_mod = CMA3000_BUSI2C,
54 .read = cma3000_i2c_read,
55 .write = cma3000_i2c_set,
56};
57
58static int __devinit cma3000_i2c_probe(struct i2c_client *client,
59 const struct i2c_device_id *id)
60{
61 struct cma3000_accl_data *data;
62
63 data = cma3000_init(&client->dev, client->irq, &cma3000_i2c_bops);
64 if (IS_ERR(data))
65 return PTR_ERR(data);
66
67 i2c_set_clientdata(client, data);
68
69 return 0;
70}
71
72static int __devexit cma3000_i2c_remove(struct i2c_client *client)
73{
74 struct cma3000_accl_data *data = i2c_get_clientdata(client);
75
76 cma3000_exit(data);
77
78 return 0;
79}
80
81#ifdef CONFIG_PM
82static int cma3000_i2c_suspend(struct device *dev)
83{
84 struct i2c_client *client = to_i2c_client(dev);
85 struct cma3000_accl_data *data = i2c_get_clientdata(client);
86
87 cma3000_suspend(data);
88
89 return 0;
90}
91
92static int cma3000_i2c_resume(struct device *dev)
93{
94 struct i2c_client *client = to_i2c_client(dev);
95 struct cma3000_accl_data *data = i2c_get_clientdata(client);
96
97 cma3000_resume(data);
98
99 return 0;
100}
101
102static const struct dev_pm_ops cma3000_i2c_pm_ops = {
103 .suspend = cma3000_i2c_suspend,
104 .resume = cma3000_i2c_resume,
105};
106#endif
107
108static const struct i2c_device_id cma3000_i2c_id[] = {
109 { "cma3000_d01", 0 },
110 { },
111};
112
113static struct i2c_driver cma3000_i2c_driver = {
114 .probe = cma3000_i2c_probe,
115 .remove = __devexit_p(cma3000_i2c_remove),
116 .id_table = cma3000_i2c_id,
117 .driver = {
118 .name = "cma3000_i2c_accl",
119 .owner = THIS_MODULE,
120#ifdef CONFIG_PM
121 .pm = &cma3000_i2c_pm_ops,
122#endif
123 },
124};
125
126static int __init cma3000_i2c_init(void)
127{
128 return i2c_add_driver(&cma3000_i2c_driver);
129}
130
131static void __exit cma3000_i2c_exit(void)
132{
133 i2c_del_driver(&cma3000_i2c_driver);
134}
135
136module_init(cma3000_i2c_init);
137module_exit(cma3000_i2c_exit);
138
139MODULE_DESCRIPTION("CMA3000-D0x Accelerometer I2C Driver");
140MODULE_LICENSE("GPL");
141MODULE_AUTHOR("Hemanth V <hemanthv@ti.com>");