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