blob: 19c73574458e8201bc8c36d3b083e241cf1fc841 [file] [log] [blame]
Eric Miao3ead8b52011-06-22 01:02:50 -07001/*
2 * Driver for Freescale's 3-Axis Accelerometer MMA8450
3 *
4 * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
5 *
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.
10 *
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.
15 *
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.
19 */
20
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>
Shawn Guo71ff0692011-07-31 19:56:10 -070027#include <linux/of_device.h>
Eric Miao3ead8b52011-06-22 01:02:50 -070028
29#define MMA8450_DRV_NAME "mma8450"
30
31#define MODE_CHANGE_DELAY_MS 100
32#define POLL_INTERVAL 100
33#define POLL_INTERVAL_MAX 500
34
35/* register definitions */
36#define MMA8450_STATUS 0x00
37#define MMA8450_STATUS_ZXYDR 0x08
38
39#define MMA8450_OUT_X8 0x01
40#define MMA8450_OUT_Y8 0x02
41#define MMA8450_OUT_Z8 0x03
42
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
49
50#define MMA8450_XYZ_DATA_CFG 0x16
51
52#define MMA8450_CTRL_REG1 0x38
53#define MMA8450_CTRL_REG2 0x39
54
55/* mma8450 status */
56struct mma8450 {
57 struct i2c_client *client;
58 struct input_polled_dev *idev;
59};
60
61static int mma8450_read(struct mma8450 *m, unsigned off)
62{
63 struct i2c_client *c = m->client;
64 int ret;
65
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);
71
72 return ret;
73}
74
75static int mma8450_write(struct mma8450 *m, unsigned off, u8 v)
76{
77 struct i2c_client *c = m->client;
78 int error;
79
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;
86 }
87
88 return 0;
89}
90
Dmitry Torokhovcb31f892011-10-06 15:26:50 -070091static int mma8450_read_block(struct mma8450 *m, unsigned off,
92 u8 *buf, size_t size)
Eric Miao3ead8b52011-06-22 01:02:50 -070093{
94 struct i2c_client *c = m->client;
Eric Miao3ead8b52011-06-22 01:02:50 -070095 int err;
96
Dmitry Torokhovcb31f892011-10-06 15:26:50 -070097 err = i2c_smbus_read_i2c_block_data(c, off, size, buf);
Eric Miao3ead8b52011-06-22 01:02:50 -070098 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;
103 }
104
Eric Miao3ead8b52011-06-22 01:02:50 -0700105 return 0;
106}
107
108static void mma8450_poll(struct input_polled_dev *dev)
109{
110 struct mma8450 *m = dev->private;
111 int x, y, z;
112 int ret;
Dmitry Torokhovcb31f892011-10-06 15:26:50 -0700113 u8 buf[6];
Eric Miao3ead8b52011-06-22 01:02:50 -0700114
115 ret = mma8450_read(m, MMA8450_STATUS);
116 if (ret < 0)
117 return;
118
119 if (!(ret & MMA8450_STATUS_ZXYDR))
120 return;
121
Dmitry Torokhovcb31f892011-10-06 15:26:50 -0700122 ret = mma8450_read_block(m, MMA8450_OUT_X_LSB, buf, sizeof(buf));
123 if (ret < 0)
Eric Miao3ead8b52011-06-22 01:02:50 -0700124 return;
125
Sebastien Royen257a1ec2013-03-31 00:24:13 -0700126 x = ((int)(s8)buf[1] << 4) | (buf[0] & 0xf);
127 y = ((int)(s8)buf[3] << 4) | (buf[2] & 0xf);
128 z = ((int)(s8)buf[5] << 4) | (buf[4] & 0xf);
Dmitry Torokhovcb31f892011-10-06 15:26:50 -0700129
Eric Miao3ead8b52011-06-22 01:02:50 -0700130 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);
134}
135
136/* Initialize the MMA8450 chip */
137static void mma8450_open(struct input_polled_dev *dev)
138{
139 struct mma8450 *m = dev->private;
140 int err;
141
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;
146
147 /*
148 * Sleep mode poll rate - 50Hz
149 * System output data rate - 400Hz
150 * Full scale selection - Active, +/- 2G
151 */
152 err = mma8450_write(m, MMA8450_CTRL_REG1, 0x01);
153 if (err < 0)
154 return;
155
156 msleep(MODE_CHANGE_DELAY_MS);
157}
158
159static void mma8450_close(struct input_polled_dev *dev)
160{
161 struct mma8450 *m = dev->private;
162
163 mma8450_write(m, MMA8450_CTRL_REG1, 0x00);
164 mma8450_write(m, MMA8450_CTRL_REG2, 0x01);
165}
166
167/*
168 * I2C init/probing/exit functions
169 */
Bill Pemberton5298cc42012-11-23 21:38:25 -0800170static int mma8450_probe(struct i2c_client *c,
Wei Yongjunacc84662013-11-10 23:35:45 -0800171 const struct i2c_device_id *id)
Eric Miao3ead8b52011-06-22 01:02:50 -0700172{
173 struct input_polled_dev *idev;
174 struct mma8450 *m;
175 int err;
176
Dmitry Torokhov5037a172015-02-25 17:44:55 -0800177 m = devm_kzalloc(&c->dev, sizeof(*m), GFP_KERNEL);
178 if (!m)
179 return -ENOMEM;
180
181 idev = devm_input_allocate_polled_device(&c->dev);
182 if (!idev)
183 return -ENOMEM;
Eric Miao3ead8b52011-06-22 01:02:50 -0700184
185 m->client = c;
186 m->idev = idev;
187
188 idev->private = m;
189 idev->input->name = MMA8450_DRV_NAME;
190 idev->input->id.bustype = BUS_I2C;
191 idev->poll = mma8450_poll;
192 idev->poll_interval = POLL_INTERVAL;
193 idev->poll_interval_max = POLL_INTERVAL_MAX;
194 idev->open = mma8450_open;
195 idev->close = mma8450_close;
196
197 __set_bit(EV_ABS, idev->input->evbit);
198 input_set_abs_params(idev->input, ABS_X, -2048, 2047, 32, 32);
199 input_set_abs_params(idev->input, ABS_Y, -2048, 2047, 32, 32);
200 input_set_abs_params(idev->input, ABS_Z, -2048, 2047, 32, 32);
201
202 err = input_register_polled_device(idev);
203 if (err) {
204 dev_err(&c->dev, "failed to register polled input device\n");
Dmitry Torokhov5037a172015-02-25 17:44:55 -0800205 return err;
Eric Miao3ead8b52011-06-22 01:02:50 -0700206 }
207
Wei Yongjunacc84662013-11-10 23:35:45 -0800208 i2c_set_clientdata(c, m);
209
Eric Miao3ead8b52011-06-22 01:02:50 -0700210 return 0;
Eric Miao3ead8b52011-06-22 01:02:50 -0700211}
212
213static const struct i2c_device_id mma8450_id[] = {
214 { MMA8450_DRV_NAME, 0 },
215 { },
216};
217MODULE_DEVICE_TABLE(i2c, mma8450_id);
218
Shawn Guo71ff0692011-07-31 19:56:10 -0700219static const struct of_device_id mma8450_dt_ids[] = {
220 { .compatible = "fsl,mma8450", },
221 { /* sentinel */ }
222};
Axel Lincd566c62011-08-08 23:39:59 -0700223MODULE_DEVICE_TABLE(of, mma8450_dt_ids);
Shawn Guo71ff0692011-07-31 19:56:10 -0700224
Eric Miao3ead8b52011-06-22 01:02:50 -0700225static struct i2c_driver mma8450_driver = {
226 .driver = {
227 .name = MMA8450_DRV_NAME,
Shawn Guo71ff0692011-07-31 19:56:10 -0700228 .of_match_table = mma8450_dt_ids,
Eric Miao3ead8b52011-06-22 01:02:50 -0700229 },
230 .probe = mma8450_probe,
Eric Miao3ead8b52011-06-22 01:02:50 -0700231 .id_table = mma8450_id,
232};
233
Axel Lin1b92c1c2012-03-16 23:05:41 -0700234module_i2c_driver(mma8450_driver);
Eric Miao3ead8b52011-06-22 01:02:50 -0700235
236MODULE_AUTHOR("Freescale Semiconductor, Inc.");
237MODULE_DESCRIPTION("MMA8450 3-Axis Accelerometer Driver");
238MODULE_LICENSE("GPL");