Merge git://git.kernel.org/pub/scm/linux/kernel/git/lwfinger/r8192E into staging...
[zen-stable.git] / drivers / input / misc / mma8450.c
blob6c76cf79299143ce16542af8e734e49e4a21bf12
1 /*
2 * Driver for Freescale's 3-Axis Accelerometer MMA8450
4 * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/delay.h>
25 #include <linux/i2c.h>
26 #include <linux/input-polldev.h>
27 #include <linux/of_device.h>
29 #define MMA8450_DRV_NAME "mma8450"
31 #define MODE_CHANGE_DELAY_MS 100
32 #define POLL_INTERVAL 100
33 #define POLL_INTERVAL_MAX 500
35 /* register definitions */
36 #define MMA8450_STATUS 0x00
37 #define MMA8450_STATUS_ZXYDR 0x08
39 #define MMA8450_OUT_X8 0x01
40 #define MMA8450_OUT_Y8 0x02
41 #define MMA8450_OUT_Z8 0x03
43 #define MMA8450_OUT_X_LSB 0x05
44 #define MMA8450_OUT_X_MSB 0x06
45 #define MMA8450_OUT_Y_LSB 0x07
46 #define MMA8450_OUT_Y_MSB 0x08
47 #define MMA8450_OUT_Z_LSB 0x09
48 #define MMA8450_OUT_Z_MSB 0x0a
50 #define MMA8450_XYZ_DATA_CFG 0x16
52 #define MMA8450_CTRL_REG1 0x38
53 #define MMA8450_CTRL_REG2 0x39
55 /* mma8450 status */
56 struct mma8450 {
57 struct i2c_client *client;
58 struct input_polled_dev *idev;
61 static int mma8450_read(struct mma8450 *m, unsigned off)
63 struct i2c_client *c = m->client;
64 int ret;
66 ret = i2c_smbus_read_byte_data(c, off);
67 if (ret < 0)
68 dev_err(&c->dev,
69 "failed to read register 0x%02x, error %d\n",
70 off, ret);
72 return ret;
75 static int mma8450_write(struct mma8450 *m, unsigned off, u8 v)
77 struct i2c_client *c = m->client;
78 int error;
80 error = i2c_smbus_write_byte_data(c, off, v);
81 if (error < 0) {
82 dev_err(&c->dev,
83 "failed to write to register 0x%02x, error %d\n",
84 off, error);
85 return error;
88 return 0;
91 static int mma8450_read_xyz(struct mma8450 *m, int *x, int *y, int *z)
93 struct i2c_client *c = m->client;
94 u8 buff[6];
95 int err;
97 err = i2c_smbus_read_i2c_block_data(c, MMA8450_OUT_X_LSB, 6, buff);
98 if (err < 0) {
99 dev_err(&c->dev,
100 "failed to read block data at 0x%02x, error %d\n",
101 MMA8450_OUT_X_LSB, err);
102 return err;
105 *x = ((buff[1] << 4) & 0xff0) | (buff[0] & 0xf);
106 *y = ((buff[3] << 4) & 0xff0) | (buff[2] & 0xf);
107 *z = ((buff[5] << 4) & 0xff0) | (buff[4] & 0xf);
109 return 0;
112 static void mma8450_poll(struct input_polled_dev *dev)
114 struct mma8450 *m = dev->private;
115 int x, y, z;
116 int ret;
117 int err;
119 ret = mma8450_read(m, MMA8450_STATUS);
120 if (ret < 0)
121 return;
123 if (!(ret & MMA8450_STATUS_ZXYDR))
124 return;
126 err = mma8450_read_xyz(m, &x, &y, &z);
127 if (err)
128 return;
130 input_report_abs(dev->input, ABS_X, x);
131 input_report_abs(dev->input, ABS_Y, y);
132 input_report_abs(dev->input, ABS_Z, z);
133 input_sync(dev->input);
136 /* Initialize the MMA8450 chip */
137 static void mma8450_open(struct input_polled_dev *dev)
139 struct mma8450 *m = dev->private;
140 int err;
142 /* enable all events from X/Y/Z, no FIFO */
143 err = mma8450_write(m, MMA8450_XYZ_DATA_CFG, 0x07);
144 if (err)
145 return;
148 * Sleep mode poll rate - 50Hz
149 * System output data rate - 400Hz
150 * Full scale selection - Active, +/- 2G
152 err = mma8450_write(m, MMA8450_CTRL_REG1, 0x01);
153 if (err < 0)
154 return;
156 msleep(MODE_CHANGE_DELAY_MS);
159 static void mma8450_close(struct input_polled_dev *dev)
161 struct mma8450 *m = dev->private;
163 mma8450_write(m, MMA8450_CTRL_REG1, 0x00);
164 mma8450_write(m, MMA8450_CTRL_REG2, 0x01);
168 * I2C init/probing/exit functions
170 static int __devinit mma8450_probe(struct i2c_client *c,
171 const struct i2c_device_id *id)
173 struct input_polled_dev *idev;
174 struct mma8450 *m;
175 int err;
177 m = kzalloc(sizeof(struct mma8450), GFP_KERNEL);
178 idev = input_allocate_polled_device();
179 if (!m || !idev) {
180 err = -ENOMEM;
181 goto err_free_mem;
184 m->client = c;
185 m->idev = idev;
187 idev->private = m;
188 idev->input->name = MMA8450_DRV_NAME;
189 idev->input->id.bustype = BUS_I2C;
190 idev->poll = mma8450_poll;
191 idev->poll_interval = POLL_INTERVAL;
192 idev->poll_interval_max = POLL_INTERVAL_MAX;
193 idev->open = mma8450_open;
194 idev->close = mma8450_close;
196 __set_bit(EV_ABS, idev->input->evbit);
197 input_set_abs_params(idev->input, ABS_X, -2048, 2047, 32, 32);
198 input_set_abs_params(idev->input, ABS_Y, -2048, 2047, 32, 32);
199 input_set_abs_params(idev->input, ABS_Z, -2048, 2047, 32, 32);
201 err = input_register_polled_device(idev);
202 if (err) {
203 dev_err(&c->dev, "failed to register polled input device\n");
204 goto err_free_mem;
207 return 0;
209 err_free_mem:
210 input_free_polled_device(idev);
211 kfree(m);
212 return err;
215 static int __devexit mma8450_remove(struct i2c_client *c)
217 struct mma8450 *m = i2c_get_clientdata(c);
218 struct input_polled_dev *idev = m->idev;
220 input_unregister_polled_device(idev);
221 input_free_polled_device(idev);
222 kfree(m);
224 return 0;
227 static const struct i2c_device_id mma8450_id[] = {
228 { MMA8450_DRV_NAME, 0 },
229 { },
231 MODULE_DEVICE_TABLE(i2c, mma8450_id);
233 static const struct of_device_id mma8450_dt_ids[] = {
234 { .compatible = "fsl,mma8450", },
235 { /* sentinel */ }
237 MODULE_DEVICE_TABLE(i2c, mma8450_dt_ids);
239 static struct i2c_driver mma8450_driver = {
240 .driver = {
241 .name = MMA8450_DRV_NAME,
242 .owner = THIS_MODULE,
243 .of_match_table = mma8450_dt_ids,
245 .probe = mma8450_probe,
246 .remove = __devexit_p(mma8450_remove),
247 .id_table = mma8450_id,
250 static int __init mma8450_init(void)
252 return i2c_add_driver(&mma8450_driver);
254 module_init(mma8450_init);
256 static void __exit mma8450_exit(void)
258 i2c_del_driver(&mma8450_driver);
260 module_exit(mma8450_exit);
262 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
263 MODULE_DESCRIPTION("MMA8450 3-Axis Accelerometer Driver");
264 MODULE_LICENSE("GPL");