2 * mma8451.c - Linux kernel modules for 3-Axis Orientation/Motion
5 * Copyright (C) 2010-2013 Freescale Semiconductor, Inc. All Rights Reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/i2c.h>
27 #include <linux/mutex.h>
28 #include <linux/delay.h>
29 #include <linux/interrupt.h>
30 #include <linux/irq.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/err.h>
33 #include <linux/hwmon.h>
34 #include <linux/input-polldev.h>
36 #include <linux/regulator/consumer.h>
38 #define MMA8451_I2C_ADDR 0x1C
39 #define MMA8451_ID 0x1A
40 #define MMA8452_ID 0x2A
41 #define MMA8453_ID 0x3A
43 #define POLL_INTERVAL_MIN 1
44 #define POLL_INTERVAL_MAX 500
45 #define POLL_INTERVAL 100 /* msecs */
48 #define MODE_CHANGE_DELAY_MS 100
50 #define MMA8451_STATUS_ZYXDR 0x08
51 #define MMA8451_BUF_SIZE 7
52 #define DEFAULT_POSITION 0
54 /* register enum for mma8451 registers */
56 MMA8451_STATUS = 0x00,
64 MMA8451_F_SETUP = 0x09,
70 MMA8451_HP_FILTER_CUTOFF,
83 MMA8451_TRANSIENT_CFG = 0x1D,
84 MMA8451_TRANSIENT_SRC,
85 MMA8451_TRANSIENT_THS,
86 MMA8451_TRANSIENT_COUNT,
111 /* The sensitivity is represented in counts/g. In 2g mode the
112 sensitivity is 1024 counts/g. In 4g mode the sensitivity is 512
113 counts/g and in 8g mode the sensitivity is 256 counts/g.
127 struct mma8451_status {
134 static struct mma8451_status mma_status;
135 static struct input_polled_dev *mma8451_idev;
136 static struct device *hwmon_dev;
137 static struct i2c_client *mma8451_i2c_client;
139 static int senstive_mode = MODE_2G;
140 static int ACCHAL[8][3][3] = {
141 { {0, -1, 0}, {1, 0, 0}, {0, 0, 1} },
142 { {-1, 0, 0}, {0, -1, 0}, {0, 0, 1} },
143 { {0, 1, 0}, {-1, 0, 0}, {0, 0, 1} },
144 { {1, 0, 0}, {0, 1, 0}, {0, 0, 1} },
146 { {0, -1, 0}, {-1, 0, 0}, {0, 0, -1} },
147 { {-1, 0, 0}, {0, 1, 0}, {0, 0, -1} },
148 { {0, 1, 0}, {1, 0, 0}, {0, 0, -1} },
149 { {1, 0, 0}, {0, -1, 0}, {0, 0, -1} },
152 static DEFINE_MUTEX(mma8451_lock);
153 static int mma8451_adjust_position(short *x, short *y, short *z)
155 short rawdata[3], data[3];
157 int position = mma_status.position;
158 if (position < 0 || position > 7)
163 for (i = 0; i < 3; i++) {
165 for (j = 0; j < 3; j++)
166 data[i] += rawdata[j] * ACCHAL[position][i][j];
174 static int mma8451_change_mode(struct i2c_client *client, int mode)
178 mma_status.ctl_reg1 = 0;
179 result = i2c_smbus_write_byte_data(client, MMA8451_CTRL_REG1, 0);
182 mma_status.active = MMA_STANDBY;
184 result = i2c_smbus_write_byte_data(client, MMA8451_XYZ_DATA_CFG,
188 mdelay(MODE_CHANGE_DELAY_MS);
189 mma_status.mode = mode;
193 dev_err(&client->dev, "error when init mma8451:(%d)", result);
197 static int mma8451_read_data(short *x, short *y, short *z)
199 u8 tmp_data[MMA8451_BUF_SIZE];
202 ret = i2c_smbus_read_i2c_block_data(mma8451_i2c_client,
203 MMA8451_OUT_X_MSB, 7, tmp_data);
204 if (ret < MMA8451_BUF_SIZE) {
205 dev_err(&mma8451_i2c_client->dev, "i2c block read failed\n");
209 *x = ((tmp_data[0] << 8) & 0xff00) | tmp_data[1];
210 *y = ((tmp_data[2] << 8) & 0xff00) | tmp_data[3];
211 *z = ((tmp_data[4] << 8) & 0xff00) | tmp_data[5];
215 static void report_abs(void)
221 mutex_lock(&mma8451_lock);
222 if (mma_status.active == MMA_STANDBY)
224 /* wait for the data ready */
226 result = i2c_smbus_read_byte_data(mma8451_i2c_client,
230 } while (!(result & MMA8451_STATUS_ZYXDR) && retry > 0);
233 if (mma8451_read_data(&x, &y, &z) != 0)
235 mma8451_adjust_position(&x, &y, &z);
236 input_report_abs(mma8451_idev->input, ABS_X, x);
237 input_report_abs(mma8451_idev->input, ABS_Y, y);
238 input_report_abs(mma8451_idev->input, ABS_Z, z);
239 input_sync(mma8451_idev->input);
241 mutex_unlock(&mma8451_lock);
244 static void mma8451_dev_poll(struct input_polled_dev *dev)
249 static ssize_t mma8451_enable_show(struct device *dev,
250 struct device_attribute *attr, char *buf)
252 struct i2c_client *client;
256 mutex_lock(&mma8451_lock);
257 client = mma8451_i2c_client;
258 val = i2c_smbus_read_byte_data(client, MMA8451_CTRL_REG1);
259 if ((val & 0x01) && mma_status.active == MMA_ACTIVED)
263 mutex_unlock(&mma8451_lock);
264 return sprintf(buf, "%d\n", enable);
267 static ssize_t mma8451_enable_store(struct device *dev,
268 struct device_attribute *attr,
269 const char *buf, size_t count)
271 struct i2c_client *client;
273 unsigned long enable;
276 ret = strict_strtoul(buf, 10, &enable);
278 dev_err(dev, "string transform error\n");
282 mutex_lock(&mma8451_lock);
283 client = mma8451_i2c_client;
284 enable = (enable > 0) ? 1 : 0;
285 if (enable && mma_status.active == MMA_STANDBY) {
286 val = i2c_smbus_read_byte_data(client, MMA8451_CTRL_REG1);
288 i2c_smbus_write_byte_data(client, MMA8451_CTRL_REG1,
291 mma_status.active = MMA_ACTIVED;
293 } else if (enable == 0 && mma_status.active == MMA_ACTIVED) {
294 val = i2c_smbus_read_byte_data(client, MMA8451_CTRL_REG1);
296 i2c_smbus_write_byte_data(client, MMA8451_CTRL_REG1,
299 mma_status.active = MMA_STANDBY;
302 mutex_unlock(&mma8451_lock);
306 static ssize_t mma8451_position_show(struct device *dev,
307 struct device_attribute *attr, char *buf)
310 mutex_lock(&mma8451_lock);
311 position = mma_status.position;
312 mutex_unlock(&mma8451_lock);
313 return sprintf(buf, "%d\n", position);
316 static ssize_t mma8451_position_store(struct device *dev,
317 struct device_attribute *attr,
318 const char *buf, size_t count)
320 unsigned long position;
322 ret = strict_strtoul(buf, 10, &position);
324 dev_err(dev, "string transform error\n");
328 mutex_lock(&mma8451_lock);
329 mma_status.position = (int)position;
330 mutex_unlock(&mma8451_lock);
334 static ssize_t mma8451_scalemode_show(struct device *dev,
335 struct device_attribute *attr,
339 mutex_lock(&mma8451_lock);
340 mode = (int)mma_status.mode;
341 mutex_unlock(&mma8451_lock);
343 return sprintf(buf, "%d\n", mode);
346 static ssize_t mma8451_scalemode_store(struct device *dev,
347 struct device_attribute *attr,
348 const char *buf, size_t count)
351 int ret, active_save;
352 struct i2c_client *client = mma8451_i2c_client;
354 ret = strict_strtoul(buf, 10, &mode);
356 dev_err(dev, "string transform error\n");
360 if (mode > MODE_8G) {
361 dev_warn(dev, "not supported mode\n");
366 mutex_lock(&mma8451_lock);
367 if (mode == mma_status.mode) {
372 active_save = mma_status.active;
373 ret = mma8451_change_mode(client, mode);
377 if (active_save == MMA_ACTIVED) {
378 ret = i2c_smbus_write_byte_data(client, MMA8451_CTRL_REG1, 1);
382 mma_status.active = active_save;
386 mutex_unlock(&mma8451_lock);
391 static DEVICE_ATTR(enable, S_IWUSR | S_IRUGO,
392 mma8451_enable_show, mma8451_enable_store);
393 static DEVICE_ATTR(position, S_IWUSR | S_IRUGO,
394 mma8451_position_show, mma8451_position_store);
395 static DEVICE_ATTR(scalemode, S_IWUSR | S_IRUGO,
396 mma8451_scalemode_show, mma8451_scalemode_store);
398 static struct attribute *mma8451_attributes[] = {
399 &dev_attr_enable.attr,
400 &dev_attr_position.attr,
401 &dev_attr_scalemode.attr,
405 static const struct attribute_group mma8451_attr_group = {
406 .attrs = mma8451_attributes,
409 static int mma8451_probe(struct i2c_client *client,
410 const struct i2c_device_id *id)
412 int result, client_id;
413 struct input_dev *idev;
414 struct i2c_adapter *adapter;
416 struct device_node *of_node = client->dev.of_node;
417 struct regulator *vdd, *vdd_io;
419 mma8451_i2c_client = client;
421 vdd = devm_regulator_get(&client->dev, "vdd");
423 result = regulator_enable(vdd);
425 dev_err(&client->dev, "vdd set voltage error\n");
430 vdd_io = devm_regulator_get(&client->dev, "vddio");
431 if (!IS_ERR(vdd_io)) {
432 result = regulator_enable(vdd_io);
434 dev_err(&client->dev, "vddio set voltage error\n");
439 adapter = to_i2c_adapter(client->dev.parent);
440 result = i2c_check_functionality(adapter,
441 I2C_FUNC_SMBUS_BYTE |
442 I2C_FUNC_SMBUS_BYTE_DATA);
446 client_id = i2c_smbus_read_byte_data(client, MMA8451_WHO_AM_I);
447 if (client_id != MMA8451_ID && client_id != MMA8452_ID
448 && client_id != MMA8453_ID) {
449 dev_err(&client->dev,
450 "read chip ID 0x%x is not equal to 0x%x or 0x%x!\n",
451 result, MMA8451_ID, MMA8452_ID);
456 /* Initialize the MMA8451 chip */
457 result = mma8451_change_mode(client, senstive_mode);
459 dev_err(&client->dev,
460 "error when init mma8451 chip:(%d)\n", result);
464 hwmon_dev = hwmon_device_register(&client->dev);
467 dev_err(&client->dev, "error when register hwmon device\n");
471 mma8451_idev = input_allocate_polled_device();
474 dev_err(&client->dev, "alloc poll device failed!\n");
475 goto err_alloc_poll_device;
477 mma8451_idev->poll = mma8451_dev_poll;
478 mma8451_idev->poll_interval = POLL_INTERVAL;
479 mma8451_idev->poll_interval_min = POLL_INTERVAL_MIN;
480 mma8451_idev->poll_interval_max = POLL_INTERVAL_MAX;
481 idev = mma8451_idev->input;
482 idev->name = "mma845x";
483 idev->id.bustype = BUS_I2C;
484 idev->evbit[0] = BIT_MASK(EV_ABS);
486 input_set_abs_params(idev, ABS_X, -8192, 8191, INPUT_FUZZ, INPUT_FLAT);
487 input_set_abs_params(idev, ABS_Y, -8192, 8191, INPUT_FUZZ, INPUT_FLAT);
488 input_set_abs_params(idev, ABS_Z, -8192, 8191, INPUT_FUZZ, INPUT_FLAT);
490 result = input_register_polled_device(mma8451_idev);
492 dev_err(&client->dev, "register poll device failed!\n");
493 goto err_register_polled_device;
495 result = sysfs_create_group(&idev->dev.kobj, &mma8451_attr_group);
497 dev_err(&client->dev, "create device file failed!\n");
499 goto err_register_polled_device;
502 result = of_property_read_u32(of_node, "position", &pos);
504 pos = DEFAULT_POSITION;
505 mma_status.position = (int)pos;
508 err_register_polled_device:
509 input_free_polled_device(mma8451_idev);
510 err_alloc_poll_device:
511 hwmon_device_unregister(&client->dev);
516 static int mma8451_stop_chip(struct i2c_client *client)
519 if (mma_status.active == MMA_ACTIVED) {
520 mma_status.ctl_reg1 = i2c_smbus_read_byte_data(client,
522 ret = i2c_smbus_write_byte_data(client, MMA8451_CTRL_REG1,
523 mma_status.ctl_reg1 & 0xFE);
528 static int mma8451_remove(struct i2c_client *client)
531 ret = mma8451_stop_chip(client);
532 hwmon_device_unregister(hwmon_dev);
537 #ifdef CONFIG_PM_SLEEP
538 static int mma8451_suspend(struct device *dev)
540 struct i2c_client *client = to_i2c_client(dev);
542 return mma8451_stop_chip(client);
545 static int mma8451_resume(struct device *dev)
548 struct i2c_client *client = to_i2c_client(dev);
549 if (mma_status.active == MMA_ACTIVED)
550 ret = i2c_smbus_write_byte_data(client, MMA8451_CTRL_REG1,
551 mma_status.ctl_reg1);
557 static const struct i2c_device_id mma8451_id[] = {
561 MODULE_DEVICE_TABLE(i2c, mma8451_id);
563 static SIMPLE_DEV_PM_OPS(mma8451_pm_ops, mma8451_suspend, mma8451_resume);
564 static struct i2c_driver mma8451_driver = {
567 .owner = THIS_MODULE,
568 .pm = &mma8451_pm_ops,
570 .probe = mma8451_probe,
571 .remove = mma8451_remove,
572 .id_table = mma8451_id,
575 static int __init mma8451_init(void)
577 /* register driver */
580 res = i2c_add_driver(&mma8451_driver);
582 printk(KERN_INFO "add mma8451 i2c driver failed\n");
588 static void __exit mma8451_exit(void)
590 i2c_del_driver(&mma8451_driver);
593 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
594 MODULE_DESCRIPTION("MMA8451 3-Axis Orientation/Motion Detection Sensor driver");
595 MODULE_LICENSE("GPL");
597 module_init(mma8451_init);
598 module_exit(mma8451_exit);