]>
Commit | Line | Data |
---|---|---|
32133be6 CM |
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 | ||
1b14adca | 42 | static const int mma7660_nscale = 467142857; |
32133be6 CM |
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 | ||
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 | ||
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 | ||
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 = { | |
32133be6 CM |
171 | .read_raw = mma7660_read_raw, |
172 | .attrs = &mma7660_attribute_group, | |
173 | }; | |
174 | ||
175 | static int mma7660_probe(struct i2c_client *client, | |
176 | const struct i2c_device_id *id) | |
177 | { | |
178 | int ret; | |
179 | struct iio_dev *indio_dev; | |
180 | struct mma7660_data *data; | |
181 | ||
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; | |
186 | } | |
187 | ||
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; | |
193 | ||
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); | |
200 | ||
201 | ret = mma7660_set_mode(data, MMA7660_MODE_ACTIVE); | |
202 | if (ret < 0) | |
203 | return ret; | |
204 | ||
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); | |
209 | } | |
210 | ||
211 | return ret; | |
212 | } | |
213 | ||
214 | static int mma7660_remove(struct i2c_client *client) | |
215 | { | |
216 | struct iio_dev *indio_dev = i2c_get_clientdata(client); | |
217 | ||
218 | iio_device_unregister(indio_dev); | |
219 | ||
220 | return mma7660_set_mode(iio_priv(indio_dev), MMA7660_MODE_STANDBY); | |
221 | } | |
222 | ||
223 | #ifdef CONFIG_PM_SLEEP | |
224 | static int mma7660_suspend(struct device *dev) | |
225 | { | |
226 | struct mma7660_data *data; | |
227 | ||
228 | data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); | |
229 | ||
230 | return mma7660_set_mode(data, MMA7660_MODE_STANDBY); | |
231 | } | |
232 | ||
233 | static int mma7660_resume(struct device *dev) | |
234 | { | |
235 | struct mma7660_data *data; | |
236 | ||
237 | data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); | |
238 | ||
239 | return mma7660_set_mode(data, MMA7660_MODE_ACTIVE); | |
240 | } | |
241 | ||
242 | static SIMPLE_DEV_PM_OPS(mma7660_pm_ops, mma7660_suspend, mma7660_resume); | |
243 | ||
244 | #define MMA7660_PM_OPS (&mma7660_pm_ops) | |
245 | #else | |
246 | #define MMA7660_PM_OPS NULL | |
247 | #endif | |
248 | ||
249 | static const struct i2c_device_id mma7660_i2c_id[] = { | |
250 | {"mma7660", 0}, | |
251 | {} | |
252 | }; | |
56203188 | 253 | MODULE_DEVICE_TABLE(i2c, mma7660_i2c_id); |
32133be6 | 254 | |
f1c088a7 JMC |
255 | static const struct of_device_id mma7660_of_match[] = { |
256 | { .compatible = "fsl,mma7660" }, | |
257 | { } | |
258 | }; | |
259 | MODULE_DEVICE_TABLE(of, mma7660_of_match); | |
260 | ||
32133be6 CM |
261 | static const struct acpi_device_id mma7660_acpi_id[] = { |
262 | {"MMA7660", 0}, | |
263 | {} | |
264 | }; | |
265 | ||
266 | MODULE_DEVICE_TABLE(acpi, mma7660_acpi_id); | |
267 | ||
268 | static struct i2c_driver mma7660_driver = { | |
269 | .driver = { | |
270 | .name = "mma7660", | |
271 | .pm = MMA7660_PM_OPS, | |
f1c088a7 | 272 | .of_match_table = mma7660_of_match, |
32133be6 CM |
273 | .acpi_match_table = ACPI_PTR(mma7660_acpi_id), |
274 | }, | |
275 | .probe = mma7660_probe, | |
276 | .remove = mma7660_remove, | |
277 | .id_table = mma7660_i2c_id, | |
278 | }; | |
279 | ||
280 | module_i2c_driver(mma7660_driver); | |
281 | ||
282 | MODULE_AUTHOR("Constantin Musca <constantin.musca@intel.com>"); | |
283 | MODULE_DESCRIPTION("Freescale MMA7660FC 3-Axis Accelerometer driver"); | |
284 | MODULE_LICENSE("GPL v2"); |