perf python: Do not force closing original perf descriptor in evlist.get_pollfd()
[linux/fpc-iii.git] / drivers / iio / accel / mma7660.c
blobf1a13724efb383de36139c2ef3d1b8a98f5b28f1
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 .read_raw = mma7660_read_raw,
172 .attrs = &mma7660_attribute_group,
175 static int mma7660_probe(struct i2c_client *client,
176 const struct i2c_device_id *id)
178 int ret;
179 struct iio_dev *indio_dev;
180 struct mma7660_data *data;
182 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
183 if (!indio_dev) {
184 dev_err(&client->dev, "iio allocation failed!\n");
185 return -ENOMEM;
188 data = iio_priv(indio_dev);
189 data->client = client;
190 i2c_set_clientdata(client, indio_dev);
191 mutex_init(&data->lock);
192 data->mode = MMA7660_MODE_STANDBY;
194 indio_dev->dev.parent = &client->dev;
195 indio_dev->info = &mma7660_info;
196 indio_dev->name = MMA7660_DRIVER_NAME;
197 indio_dev->modes = INDIO_DIRECT_MODE;
198 indio_dev->channels = mma7660_channels;
199 indio_dev->num_channels = ARRAY_SIZE(mma7660_channels);
201 ret = mma7660_set_mode(data, MMA7660_MODE_ACTIVE);
202 if (ret < 0)
203 return ret;
205 ret = iio_device_register(indio_dev);
206 if (ret < 0) {
207 dev_err(&client->dev, "device_register failed\n");
208 mma7660_set_mode(data, MMA7660_MODE_STANDBY);
211 return ret;
214 static int mma7660_remove(struct i2c_client *client)
216 struct iio_dev *indio_dev = i2c_get_clientdata(client);
218 iio_device_unregister(indio_dev);
220 return mma7660_set_mode(iio_priv(indio_dev), MMA7660_MODE_STANDBY);
223 #ifdef CONFIG_PM_SLEEP
224 static int mma7660_suspend(struct device *dev)
226 struct mma7660_data *data;
228 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
230 return mma7660_set_mode(data, MMA7660_MODE_STANDBY);
233 static int mma7660_resume(struct device *dev)
235 struct mma7660_data *data;
237 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
239 return mma7660_set_mode(data, MMA7660_MODE_ACTIVE);
242 static SIMPLE_DEV_PM_OPS(mma7660_pm_ops, mma7660_suspend, mma7660_resume);
244 #define MMA7660_PM_OPS (&mma7660_pm_ops)
245 #else
246 #define MMA7660_PM_OPS NULL
247 #endif
249 static const struct i2c_device_id mma7660_i2c_id[] = {
250 {"mma7660", 0},
253 MODULE_DEVICE_TABLE(i2c, mma7660_i2c_id);
255 static const struct of_device_id mma7660_of_match[] = {
256 { .compatible = "fsl,mma7660" },
259 MODULE_DEVICE_TABLE(of, mma7660_of_match);
261 static const struct acpi_device_id mma7660_acpi_id[] = {
262 {"MMA7660", 0},
266 MODULE_DEVICE_TABLE(acpi, mma7660_acpi_id);
268 static struct i2c_driver mma7660_driver = {
269 .driver = {
270 .name = "mma7660",
271 .pm = MMA7660_PM_OPS,
272 .of_match_table = mma7660_of_match,
273 .acpi_match_table = ACPI_PTR(mma7660_acpi_id),
275 .probe = mma7660_probe,
276 .remove = mma7660_remove,
277 .id_table = mma7660_i2c_id,
280 module_i2c_driver(mma7660_driver);
282 MODULE_AUTHOR("Constantin Musca <constantin.musca@intel.com>");
283 MODULE_DESCRIPTION("Freescale MMA7660FC 3-Axis Accelerometer driver");
284 MODULE_LICENSE("GPL v2");