at master 227 lines 5.1 kB view raw
1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Driver for Freescale's 3-Axis Accelerometer MMA8450 4 * 5 * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved. 6 */ 7 8#include <linux/kernel.h> 9#include <linux/module.h> 10#include <linux/slab.h> 11#include <linux/delay.h> 12#include <linux/i2c.h> 13#include <linux/input.h> 14#include <linux/mod_devicetable.h> 15 16#define MMA8450_DRV_NAME "mma8450" 17 18#define MODE_CHANGE_DELAY_MS 100 19#define POLL_INTERVAL 100 20#define POLL_INTERVAL_MAX 500 21 22/* register definitions */ 23#define MMA8450_STATUS 0x00 24#define MMA8450_STATUS_ZXYDR 0x08 25 26#define MMA8450_OUT_X8 0x01 27#define MMA8450_OUT_Y8 0x02 28#define MMA8450_OUT_Z8 0x03 29 30#define MMA8450_OUT_X_LSB 0x05 31#define MMA8450_OUT_X_MSB 0x06 32#define MMA8450_OUT_Y_LSB 0x07 33#define MMA8450_OUT_Y_MSB 0x08 34#define MMA8450_OUT_Z_LSB 0x09 35#define MMA8450_OUT_Z_MSB 0x0a 36 37#define MMA8450_XYZ_DATA_CFG 0x16 38 39#define MMA8450_CTRL_REG1 0x38 40#define MMA8450_CTRL_REG2 0x39 41#define MMA8450_ID 0xc6 42#define MMA8450_WHO_AM_I 0x0f 43 44static int mma8450_read(struct i2c_client *c, unsigned int off) 45{ 46 int ret; 47 48 ret = i2c_smbus_read_byte_data(c, off); 49 if (ret < 0) 50 dev_err(&c->dev, 51 "failed to read register 0x%02x, error %d\n", 52 off, ret); 53 54 return ret; 55} 56 57static int mma8450_write(struct i2c_client *c, unsigned int off, u8 v) 58{ 59 int error; 60 61 error = i2c_smbus_write_byte_data(c, off, v); 62 if (error < 0) { 63 dev_err(&c->dev, 64 "failed to write to register 0x%02x, error %d\n", 65 off, error); 66 return error; 67 } 68 69 return 0; 70} 71 72static int mma8450_read_block(struct i2c_client *c, unsigned int off, 73 u8 *buf, size_t size) 74{ 75 int err; 76 77 err = i2c_smbus_read_i2c_block_data(c, off, size, buf); 78 if (err < 0) { 79 dev_err(&c->dev, 80 "failed to read block data at 0x%02x, error %d\n", 81 MMA8450_OUT_X_LSB, err); 82 return err; 83 } 84 85 return 0; 86} 87 88static void mma8450_poll(struct input_dev *input) 89{ 90 struct i2c_client *c = input_get_drvdata(input); 91 int x, y, z; 92 int ret; 93 u8 buf[6]; 94 95 ret = mma8450_read(c, MMA8450_STATUS); 96 if (ret < 0) 97 return; 98 99 if (!(ret & MMA8450_STATUS_ZXYDR)) 100 return; 101 102 ret = mma8450_read_block(c, MMA8450_OUT_X_LSB, buf, sizeof(buf)); 103 if (ret < 0) 104 return; 105 106 x = ((int)(s8)buf[1] << 4) | (buf[0] & 0xf); 107 y = ((int)(s8)buf[3] << 4) | (buf[2] & 0xf); 108 z = ((int)(s8)buf[5] << 4) | (buf[4] & 0xf); 109 110 input_report_abs(input, ABS_X, x); 111 input_report_abs(input, ABS_Y, y); 112 input_report_abs(input, ABS_Z, z); 113 input_sync(input); 114} 115 116/* Initialize the MMA8450 chip */ 117static int mma8450_open(struct input_dev *input) 118{ 119 struct i2c_client *c = input_get_drvdata(input); 120 int err; 121 122 /* enable all events from X/Y/Z, no FIFO */ 123 err = mma8450_write(c, MMA8450_XYZ_DATA_CFG, 0x07); 124 if (err) 125 return err; 126 127 /* 128 * Sleep mode poll rate - 50Hz 129 * System output data rate - 400Hz 130 * Full scale selection - Active, +/- 2G 131 */ 132 err = mma8450_write(c, MMA8450_CTRL_REG1, 0x01); 133 if (err) 134 return err; 135 136 msleep(MODE_CHANGE_DELAY_MS); 137 return 0; 138} 139 140static void mma8450_close(struct input_dev *input) 141{ 142 struct i2c_client *c = input_get_drvdata(input); 143 144 mma8450_write(c, MMA8450_CTRL_REG1, 0x00); 145 mma8450_write(c, MMA8450_CTRL_REG2, 0x01); 146} 147 148/* 149 * I2C init/probing/exit functions 150 */ 151static int mma8450_probe(struct i2c_client *c) 152{ 153 struct i2c_adapter *adapter = c->adapter; 154 struct input_dev *input; 155 int err, client_id; 156 157 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE | 158 I2C_FUNC_SMBUS_BYTE_DATA)) 159 return dev_err_probe(&c->dev, -EINVAL, 160 "I2C adapter doesn't support SMBUS BYTE"); 161 162 client_id = i2c_smbus_read_byte_data(c, MMA8450_WHO_AM_I); 163 if (client_id != MMA8450_ID) 164 return dev_err_probe(&c->dev, -EINVAL, 165 "unexpected chip ID 0x%x (vs 0x%x)\n", 166 client_id, MMA8450_ID); 167 168 input = devm_input_allocate_device(&c->dev); 169 if (!input) 170 return -ENOMEM; 171 172 input_set_drvdata(input, c); 173 174 input->name = MMA8450_DRV_NAME; 175 input->id.bustype = BUS_I2C; 176 177 input->open = mma8450_open; 178 input->close = mma8450_close; 179 180 input_set_abs_params(input, ABS_X, -2048, 2047, 32, 32); 181 input_set_abs_params(input, ABS_Y, -2048, 2047, 32, 32); 182 input_set_abs_params(input, ABS_Z, -2048, 2047, 32, 32); 183 184 err = input_setup_polling(input, mma8450_poll); 185 if (err) { 186 dev_err(&c->dev, "failed to set up polling\n"); 187 return err; 188 } 189 190 input_set_poll_interval(input, POLL_INTERVAL); 191 input_set_max_poll_interval(input, POLL_INTERVAL_MAX); 192 193 err = input_register_device(input); 194 if (err) { 195 dev_err(&c->dev, "failed to register input device\n"); 196 return err; 197 } 198 199 return 0; 200} 201 202static const struct i2c_device_id mma8450_id[] = { 203 { MMA8450_DRV_NAME }, 204 { } 205}; 206MODULE_DEVICE_TABLE(i2c, mma8450_id); 207 208static const struct of_device_id mma8450_dt_ids[] = { 209 { .compatible = "fsl,mma8450", }, 210 { /* sentinel */ } 211}; 212MODULE_DEVICE_TABLE(of, mma8450_dt_ids); 213 214static struct i2c_driver mma8450_driver = { 215 .driver = { 216 .name = MMA8450_DRV_NAME, 217 .of_match_table = mma8450_dt_ids, 218 }, 219 .probe = mma8450_probe, 220 .id_table = mma8450_id, 221}; 222 223module_i2c_driver(mma8450_driver); 224 225MODULE_AUTHOR("Freescale Semiconductor, Inc."); 226MODULE_DESCRIPTION("MMA8450 3-Axis Accelerometer Driver"); 227MODULE_LICENSE("GPL");