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) { \
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
),
65 struct i2c_client
*client
;
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
,
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
)
85 struct i2c_client
*client
= data
->client
;
87 if (mode
== data
->mode
)
90 ret
= i2c_smbus_read_byte_data(client
, MMA7660_REG_MODE
);
92 dev_err(&client
->dev
, "failed to read sensor mode\n");
96 if (mode
== MMA7660_MODE_ACTIVE
) {
97 ret
&= ~MMA7660_REG_MODE_BIT_TON
;
98 ret
|= MMA7660_REG_MODE_BIT_MODE
;
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
);
106 dev_err(&client
->dev
, "failed to change sensor mode\n");
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
128 ret
= i2c_smbus_read_byte_data(client
, address
);
130 dev_err(&client
->dev
, "register read failed\n");
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");
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
);
151 case IIO_CHAN_INFO_RAW
:
152 mutex_lock(&data
->lock
);
153 ret
= mma7660_read_accel(data
, chan
->address
);
154 mutex_unlock(&data
->lock
);
157 *val
= sign_extend32(ret
, 5);
159 case IIO_CHAN_INFO_SCALE
:
161 *val2
= mma7660_nscale
;
162 return IIO_VAL_INT_PLUS_NANO
;
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
)
180 struct iio_dev
*indio_dev
;
181 struct mma7660_data
*data
;
183 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
185 dev_err(&client
->dev
, "iio allocation failed!\n");
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
);
206 ret
= iio_device_register(indio_dev
);
208 dev_err(&client
->dev
, "device_register failed\n");
209 mma7660_set_mode(data
, MMA7660_MODE_STANDBY
);
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)
247 #define MMA7660_PM_OPS NULL
250 static const struct i2c_device_id mma7660_i2c_id
[] = {
254 MODULE_DEVICE_TABLE(i2c
, mma7660_i2c_id
);
256 static const struct acpi_device_id mma7660_acpi_id
[] = {
261 MODULE_DEVICE_TABLE(acpi
, mma7660_acpi_id
);
263 static struct i2c_driver mma7660_driver
= {
266 .pm
= MMA7660_PM_OPS
,
267 .acpi_match_table
= ACPI_PTR(mma7660_acpi_id
),
269 .probe
= mma7660_probe
,
270 .remove
= mma7660_remove
,
271 .id_table
= mma7660_i2c_id
,
274 module_i2c_driver(mma7660_driver
);
276 MODULE_AUTHOR("Constantin Musca <constantin.musca@intel.com>");
277 MODULE_DESCRIPTION("Freescale MMA7660FC 3-Axis Accelerometer driver");
278 MODULE_LICENSE("GPL v2");