xref: /linux/drivers/input/misc/cma3000_d0x_i2c.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
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 
25 static 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 
38 static 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 
50 static 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 
58 static int 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 
72 static int 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
82 static 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 
92 static 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 
102 static const struct dev_pm_ops cma3000_i2c_pm_ops = {
103 	.suspend	= cma3000_i2c_suspend,
104 	.resume		= cma3000_i2c_resume,
105 };
106 #endif
107 
108 static const struct i2c_device_id cma3000_i2c_id[] = {
109 	{ "cma3000_d01", 0 },
110 	{ },
111 };
112 
113 MODULE_DEVICE_TABLE(i2c, cma3000_i2c_id);
114 
115 static struct i2c_driver cma3000_i2c_driver = {
116 	.probe		= cma3000_i2c_probe,
117 	.remove		= cma3000_i2c_remove,
118 	.id_table	= cma3000_i2c_id,
119 	.driver = {
120 		.name	= "cma3000_i2c_accl",
121 #ifdef CONFIG_PM
122 		.pm	= &cma3000_i2c_pm_ops,
123 #endif
124 	},
125 };
126 
127 module_i2c_driver(cma3000_i2c_driver);
128 
129 MODULE_DESCRIPTION("CMA3000-D0x Accelerometer I2C Driver");
130 MODULE_LICENSE("GPL");
131 MODULE_AUTHOR("Hemanth V <hemanthv@ti.com>");
132