1 /**
2 * Freescale MMA7660FC 3-Axis Accelerometer
3 *
4 * Copyright (c) 2016, Intel Corporation.
5 *
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.
9 *
10 * IIO driver for Freescale MMA7660FC; 7-bit I2C address: 0x4c.
11 */
12
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>
18
19 #define MMA7660_DRIVER_NAME "mma7660"
20
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)
25
26 #define MMA7660_REG_MODE 0x07
27 #define MMA7660_REG_MODE_BIT_MODE BIT(0)
28 #define MMA7660_REG_MODE_BIT_TON BIT(2)
29
30 #define MMA7660_I2C_READ_RETRIES 5
31
32 /*
33 * The accelerometer has one measurement range:
34 *
35 * -1.5g - +1.5g (6-bit, signed)
36 *
37 * scale = (1.5 + 1.5) * 9.81 / (2^6 - 1) = 0.467142857
38 */
39
40 #define MMA7660_SCALE_AVAIL "0.467142857"
41
42 const int mma7660_nscale = 467142857;
43
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), \
51 }
52
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),
57 };
58
59 enum mma7660_mode {
60 MMA7660_MODE_STANDBY,
61 MMA7660_MODE_ACTIVE
62 };
63
64 struct mma7660_data {
65 struct i2c_client *client;
66 struct mutex lock;
67 enum mma7660_mode mode;
68 };
69
70 static IIO_CONST_ATTR(in_accel_scale_available, MMA7660_SCALE_AVAIL);
71
72 static struct attribute *mma7660_attributes[] = {
73 &iio_const_attr_in_accel_scale_available.dev_attr.attr,
74 NULL,
75 };
76
77 static const struct attribute_group mma7660_attribute_group = {
78 .attrs = mma7660_attributes
79 };
80
mma7660_set_mode(struct mma7660_data * data,enum mma7660_mode mode)81 static int mma7660_set_mode(struct mma7660_data *data,
82 enum mma7660_mode mode)
83 {
84 int ret;
85 struct i2c_client *client = data->client;
86
87 if (mode == data->mode)
88 return 0;
89
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;
94 }
95
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;
102 }
103
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;
108 }
109
110 data->mode = mode;
111
112 return ret;
113 }
114
mma7660_read_accel(struct mma7660_data * data,u8 address)115 static int mma7660_read_accel(struct mma7660_data *data, u8 address)
116 {
117 int ret, retries = MMA7660_I2C_READ_RETRIES;
118 struct i2c_client *client = data->client;
119
120 /*
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.
126 */
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;
132 }
133 } while (retries-- > 0 && ret & MMA7660_REG_OUT_BIT_ALERT);
134
135 if (ret & MMA7660_REG_OUT_BIT_ALERT) {
136 dev_err(&client->dev, "all register read retries failed\n");
137 return -ETIMEDOUT;
138 }
139
140 return ret;
141 }
142
mma7660_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)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)
146 {
147 struct mma7660_data *data = iio_priv(indio_dev);
148 int ret;
149
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;
165 }
166
167 return -EINVAL;
168 }
169
170 static const struct iio_info mma7660_info = {
171 .driver_module = THIS_MODULE,
172 .read_raw = mma7660_read_raw,
173 .attrs = &mma7660_attribute_group,
174 };
175
mma7660_probe(struct i2c_client * client,const struct i2c_device_id * id)176 static int mma7660_probe(struct i2c_client *client,
177 const struct i2c_device_id *id)
178 {
179 int ret;
180 struct iio_dev *indio_dev;
181 struct mma7660_data *data;
182
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;
187 }
188
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;
194
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);
201
202 ret = mma7660_set_mode(data, MMA7660_MODE_ACTIVE);
203 if (ret < 0)
204 return ret;
205
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);
210 }
211
212 return ret;
213 }
214
mma7660_remove(struct i2c_client * client)215 static int mma7660_remove(struct i2c_client *client)
216 {
217 struct iio_dev *indio_dev = i2c_get_clientdata(client);
218
219 iio_device_unregister(indio_dev);
220
221 return mma7660_set_mode(iio_priv(indio_dev), MMA7660_MODE_STANDBY);
222 }
223
224 #ifdef CONFIG_PM_SLEEP
mma7660_suspend(struct device * dev)225 static int mma7660_suspend(struct device *dev)
226 {
227 struct mma7660_data *data;
228
229 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
230
231 return mma7660_set_mode(data, MMA7660_MODE_STANDBY);
232 }
233
mma7660_resume(struct device * dev)234 static int mma7660_resume(struct device *dev)
235 {
236 struct mma7660_data *data;
237
238 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
239
240 return mma7660_set_mode(data, MMA7660_MODE_ACTIVE);
241 }
242
243 static SIMPLE_DEV_PM_OPS(mma7660_pm_ops, mma7660_suspend, mma7660_resume);
244
245 #define MMA7660_PM_OPS (&mma7660_pm_ops)
246 #else
247 #define MMA7660_PM_OPS NULL
248 #endif
249
250 static const struct i2c_device_id mma7660_i2c_id[] = {
251 {"mma7660", 0},
252 {}
253 };
254 MODULE_DEVICE_TABLE(i2c, mma7660_i2c_id);
255
256 static const struct acpi_device_id mma7660_acpi_id[] = {
257 {"MMA7660", 0},
258 {}
259 };
260
261 MODULE_DEVICE_TABLE(acpi, mma7660_acpi_id);
262
263 static struct i2c_driver mma7660_driver = {
264 .driver = {
265 .name = "mma7660",
266 .pm = MMA7660_PM_OPS,
267 .acpi_match_table = ACPI_PTR(mma7660_acpi_id),
268 },
269 .probe = mma7660_probe,
270 .remove = mma7660_remove,
271 .id_table = mma7660_i2c_id,
272 };
273
274 module_i2c_driver(mma7660_driver);
275
276 MODULE_AUTHOR("Constantin Musca <constantin.musca@intel.com>");
277 MODULE_DESCRIPTION("Freescale MMA7660FC 3-Axis Accelerometer driver");
278 MODULE_LICENSE("GPL v2");
279