x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / drivers / iio / accel / mma7660.c
blob42fa57e41bdd75aa602fe44c154f5db55d092381
1 /**
2 * Freescale MMA7660FC 3-Axis Accelerometer
4 * Copyright (c) 2016, Intel Corporation.
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * IIO driver for Freescale MMA7660FC; 7-bit I2C address: 0x4c.
13 #include <linux/acpi.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
19 #define MMA7660_DRIVER_NAME "mma7660"
21 #define MMA7660_REG_XOUT 0x00
22 #define MMA7660_REG_YOUT 0x01
23 #define MMA7660_REG_ZOUT 0x02
24 #define MMA7660_REG_OUT_BIT_ALERT BIT(6)
26 #define MMA7660_REG_MODE 0x07
27 #define MMA7660_REG_MODE_BIT_MODE BIT(0)
28 #define MMA7660_REG_MODE_BIT_TON BIT(2)
30 #define MMA7660_I2C_READ_RETRIES 5
33 * The accelerometer has one measurement range:
35 * -1.5g - +1.5g (6-bit, signed)
37 * scale = (1.5 + 1.5) * 9.81 / (2^6 - 1) = 0.467142857
40 #define MMA7660_SCALE_AVAIL "0.467142857"
42 static const int mma7660_nscale = 467142857;
44 #define MMA7660_CHANNEL(reg, axis) { \
45 .type = IIO_ACCEL, \
46 .address = reg, \
47 .modified = 1, \
48 .channel2 = IIO_MOD_##axis, \
49 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
50 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
53 static const struct iio_chan_spec mma7660_channels[] = {
54 MMA7660_CHANNEL(MMA7660_REG_XOUT, X),
55 MMA7660_CHANNEL(MMA7660_REG_YOUT, Y),
56 MMA7660_CHANNEL(MMA7660_REG_ZOUT, Z),
59 enum mma7660_mode {
60 MMA7660_MODE_STANDBY,
61 MMA7660_MODE_ACTIVE
64 struct mma7660_data {
65 struct i2c_client *client;
66 struct mutex lock;
67 enum mma7660_mode mode;
70 static IIO_CONST_ATTR(in_accel_scale_available, MMA7660_SCALE_AVAIL);
72 static struct attribute *mma7660_attributes[] = {
73 &iio_const_attr_in_accel_scale_available.dev_attr.attr,
74 NULL,
77 static const struct attribute_group mma7660_attribute_group = {
78 .attrs = mma7660_attributes
81 static int mma7660_set_mode(struct mma7660_data *data,
82 enum mma7660_mode mode)
84 int ret;
85 struct i2c_client *client = data->client;
87 if (mode == data->mode)
88 return 0;
90 ret = i2c_smbus_read_byte_data(client, MMA7660_REG_MODE);
91 if (ret < 0) {
92 dev_err(&client->dev, "failed to read sensor mode\n");
93 return ret;
96 if (mode == MMA7660_MODE_ACTIVE) {
97 ret &= ~MMA7660_REG_MODE_BIT_TON;
98 ret |= MMA7660_REG_MODE_BIT_MODE;
99 } else {
100 ret &= ~MMA7660_REG_MODE_BIT_TON;
101 ret &= ~MMA7660_REG_MODE_BIT_MODE;
104 ret = i2c_smbus_write_byte_data(client, MMA7660_REG_MODE, ret);
105 if (ret < 0) {
106 dev_err(&client->dev, "failed to change sensor mode\n");
107 return ret;
110 data->mode = mode;
112 return ret;
115 static int mma7660_read_accel(struct mma7660_data *data, u8 address)
117 int ret, retries = MMA7660_I2C_READ_RETRIES;
118 struct i2c_client *client = data->client;
121 * Read data. If the Alert bit is set, the register was read at
122 * the same time as the device was attempting to update the content.
123 * The solution is to read the register again. Do this only
124 * MMA7660_I2C_READ_RETRIES times to avoid spending too much time
125 * in the kernel.
127 do {
128 ret = i2c_smbus_read_byte_data(client, address);
129 if (ret < 0) {
130 dev_err(&client->dev, "register read failed\n");
131 return ret;
133 } while (retries-- > 0 && ret & MMA7660_REG_OUT_BIT_ALERT);
135 if (ret & MMA7660_REG_OUT_BIT_ALERT) {
136 dev_err(&client->dev, "all register read retries failed\n");
137 return -ETIMEDOUT;
140 return ret;
143 static int mma7660_read_raw(struct iio_dev *indio_dev,
144 struct iio_chan_spec const *chan,
145 int *val, int *val2, long mask)
147 struct mma7660_data *data = iio_priv(indio_dev);
148 int ret;
150 switch (mask) {
151 case IIO_CHAN_INFO_RAW:
152 mutex_lock(&data->lock);
153 ret = mma7660_read_accel(data, chan->address);
154 mutex_unlock(&data->lock);
155 if (ret < 0)
156 return ret;
157 *val = sign_extend32(ret, 5);
158 return IIO_VAL_INT;
159 case IIO_CHAN_INFO_SCALE:
160 *val = 0;
161 *val2 = mma7660_nscale;
162 return IIO_VAL_INT_PLUS_NANO;
163 default:
164 return -EINVAL;
167 return -EINVAL;
170 static const struct iio_info mma7660_info = {
171 .driver_module = THIS_MODULE,
172 .read_raw = mma7660_read_raw,
173 .attrs = &mma7660_attribute_group,
176 static int mma7660_probe(struct i2c_client *client,
177 const struct i2c_device_id *id)
179 int ret;
180 struct iio_dev *indio_dev;
181 struct mma7660_data *data;
183 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
184 if (!indio_dev) {
185 dev_err(&client->dev, "iio allocation failed!\n");
186 return -ENOMEM;
189 data = iio_priv(indio_dev);
190 data->client = client;
191 i2c_set_clientdata(client, indio_dev);
192 mutex_init(&data->lock);
193 data->mode = MMA7660_MODE_STANDBY;
195 indio_dev->dev.parent = &client->dev;
196 indio_dev->info = &mma7660_info;
197 indio_dev->name = MMA7660_DRIVER_NAME;
198 indio_dev->modes = INDIO_DIRECT_MODE;
199 indio_dev->channels = mma7660_channels;
200 indio_dev->num_channels = ARRAY_SIZE(mma7660_channels);
202 ret = mma7660_set_mode(data, MMA7660_MODE_ACTIVE);
203 if (ret < 0)
204 return ret;
206 ret = iio_device_register(indio_dev);
207 if (ret < 0) {
208 dev_err(&client->dev, "device_register failed\n");
209 mma7660_set_mode(data, MMA7660_MODE_STANDBY);
212 return ret;
215 static int mma7660_remove(struct i2c_client *client)
217 struct iio_dev *indio_dev = i2c_get_clientdata(client);
219 iio_device_unregister(indio_dev);
221 return mma7660_set_mode(iio_priv(indio_dev), MMA7660_MODE_STANDBY);
224 #ifdef CONFIG_PM_SLEEP
225 static int mma7660_suspend(struct device *dev)
227 struct mma7660_data *data;
229 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
231 return mma7660_set_mode(data, MMA7660_MODE_STANDBY);
234 static int mma7660_resume(struct device *dev)
236 struct mma7660_data *data;
238 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
240 return mma7660_set_mode(data, MMA7660_MODE_ACTIVE);
243 static SIMPLE_DEV_PM_OPS(mma7660_pm_ops, mma7660_suspend, mma7660_resume);
245 #define MMA7660_PM_OPS (&mma7660_pm_ops)
246 #else
247 #define MMA7660_PM_OPS NULL
248 #endif
250 static const struct i2c_device_id mma7660_i2c_id[] = {
251 {"mma7660", 0},
254 MODULE_DEVICE_TABLE(i2c, mma7660_i2c_id);
256 static const struct of_device_id mma7660_of_match[] = {
257 { .compatible = "fsl,mma7660" },
260 MODULE_DEVICE_TABLE(of, mma7660_of_match);
262 static const struct acpi_device_id mma7660_acpi_id[] = {
263 {"MMA7660", 0},
267 MODULE_DEVICE_TABLE(acpi, mma7660_acpi_id);
269 static struct i2c_driver mma7660_driver = {
270 .driver = {
271 .name = "mma7660",
272 .pm = MMA7660_PM_OPS,
273 .of_match_table = mma7660_of_match,
274 .acpi_match_table = ACPI_PTR(mma7660_acpi_id),
276 .probe = mma7660_probe,
277 .remove = mma7660_remove,
278 .id_table = mma7660_i2c_id,
281 module_i2c_driver(mma7660_driver);
283 MODULE_AUTHOR("Constantin Musca <constantin.musca@intel.com>");
284 MODULE_DESCRIPTION("Freescale MMA7660FC 3-Axis Accelerometer driver");
285 MODULE_LICENSE("GPL v2");