diff options
-rw-r--r-- | drivers/iio/magnetometer/Kconfig | 10 | ||||
-rw-r--r-- | drivers/iio/magnetometer/Makefile | 1 | ||||
-rw-r--r-- | drivers/iio/magnetometer/mag3110.c | 401 |
3 files changed, 412 insertions, 0 deletions
diff --git a/drivers/iio/magnetometer/Kconfig b/drivers/iio/magnetometer/Kconfig index 4fa923f37b97..0cf09637b35b 100644 --- a/drivers/iio/magnetometer/Kconfig +++ b/drivers/iio/magnetometer/Kconfig | |||
@@ -16,6 +16,16 @@ config AK8975 | |||
16 | To compile this driver as a module, choose M here: the module | 16 | To compile this driver as a module, choose M here: the module |
17 | will be called ak8975. | 17 | will be called ak8975. |
18 | 18 | ||
19 | config MAG3110 | ||
20 | tristate "Freescale MAG3110 3-Axis Magnetometer" | ||
21 | depends on I2C | ||
22 | help | ||
23 | Say yes here to build support for the Freescale MAG3110 3-Axis | ||
24 | magnetometer. | ||
25 | |||
26 | To compile this driver as a module, choose M here: the module | ||
27 | will be called mag3110. | ||
28 | |||
19 | config HID_SENSOR_MAGNETOMETER_3D | 29 | config HID_SENSOR_MAGNETOMETER_3D |
20 | depends on HID_SENSOR_HUB | 30 | depends on HID_SENSOR_HUB |
21 | select IIO_BUFFER | 31 | select IIO_BUFFER |
diff --git a/drivers/iio/magnetometer/Makefile b/drivers/iio/magnetometer/Makefile index f91b1b68d392..0f5d3c985799 100644 --- a/drivers/iio/magnetometer/Makefile +++ b/drivers/iio/magnetometer/Makefile | |||
@@ -4,6 +4,7 @@ | |||
4 | 4 | ||
5 | # When adding new entries keep the list in alphabetical order | 5 | # When adding new entries keep the list in alphabetical order |
6 | obj-$(CONFIG_AK8975) += ak8975.o | 6 | obj-$(CONFIG_AK8975) += ak8975.o |
7 | obj-$(CONFIG_MAG3110) += mag3110.o | ||
7 | obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) += hid-sensor-magn-3d.o | 8 | obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) += hid-sensor-magn-3d.o |
8 | 9 | ||
9 | obj-$(CONFIG_IIO_ST_MAGN_3AXIS) += st_magn.o | 10 | obj-$(CONFIG_IIO_ST_MAGN_3AXIS) += st_magn.o |
diff --git a/drivers/iio/magnetometer/mag3110.c b/drivers/iio/magnetometer/mag3110.c new file mode 100644 index 000000000000..783c5b417356 --- /dev/null +++ b/drivers/iio/magnetometer/mag3110.c | |||
@@ -0,0 +1,401 @@ | |||
1 | /* | ||
2 | * mag3110.c - Support for Freescale MAG3110 magnetometer sensor | ||
3 | * | ||
4 | * Copyright (c) 2013 Peter Meerwald <pmeerw@pmeerw.net> | ||
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 | * (7-bit I2C slave address 0x0e) | ||
11 | * | ||
12 | * TODO: irq, user offset, oversampling, continuous mode | ||
13 | */ | ||
14 | |||
15 | #include <linux/module.h> | ||
16 | #include <linux/i2c.h> | ||
17 | #include <linux/iio/iio.h> | ||
18 | #include <linux/iio/sysfs.h> | ||
19 | #include <linux/iio/trigger_consumer.h> | ||
20 | #include <linux/iio/buffer.h> | ||
21 | #include <linux/iio/triggered_buffer.h> | ||
22 | #include <linux/delay.h> | ||
23 | |||
24 | #define MAG3110_STATUS 0x00 | ||
25 | #define MAG3110_OUT_X 0x01 /* MSB first */ | ||
26 | #define MAG3110_OUT_Y 0x03 | ||
27 | #define MAG3110_OUT_Z 0x05 | ||
28 | #define MAG3110_WHO_AM_I 0x07 | ||
29 | #define MAG3110_OFF_X 0x09 /* MSB first */ | ||
30 | #define MAG3110_OFF_Y 0x0b | ||
31 | #define MAG3110_OFF_Z 0x0d | ||
32 | #define MAG3110_DIE_TEMP 0x0f | ||
33 | #define MAG3110_CTRL_REG1 0x10 | ||
34 | #define MAG3110_CTRL_REG2 0x11 | ||
35 | |||
36 | #define MAG3110_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0)) | ||
37 | |||
38 | #define MAG3110_CTRL_DR_MASK (BIT(7) | BIT(6) | BIT(5)) | ||
39 | #define MAG3110_CTRL_DR_SHIFT 5 | ||
40 | #define MAG3110_CTRL_DR_DEFAULT 0 | ||
41 | |||
42 | #define MAG3110_CTRL_TM BIT(1) /* trigger single measurement */ | ||
43 | #define MAG3110_CTRL_AC BIT(0) /* continuous measurements */ | ||
44 | |||
45 | #define MAG3110_CTRL_AUTO_MRST_EN BIT(7) /* magnetic auto-reset */ | ||
46 | #define MAG3110_CTRL_RAW BIT(5) /* measurements not user-offset corrected */ | ||
47 | |||
48 | #define MAG3110_DEVICE_ID 0xc4 | ||
49 | |||
50 | /* Each client has this additional data */ | ||
51 | struct mag3110_data { | ||
52 | struct i2c_client *client; | ||
53 | struct mutex lock; | ||
54 | u8 ctrl_reg1; | ||
55 | }; | ||
56 | |||
57 | static int mag3110_request(struct mag3110_data *data) | ||
58 | { | ||
59 | int ret, tries = 150; | ||
60 | |||
61 | /* trigger measurement */ | ||
62 | ret = i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1, | ||
63 | data->ctrl_reg1 | MAG3110_CTRL_TM); | ||
64 | if (ret < 0) | ||
65 | return ret; | ||
66 | |||
67 | while (tries-- > 0) { | ||
68 | ret = i2c_smbus_read_byte_data(data->client, MAG3110_STATUS); | ||
69 | if (ret < 0) | ||
70 | return ret; | ||
71 | /* wait for data ready */ | ||
72 | if ((ret & MAG3110_STATUS_DRDY) == MAG3110_STATUS_DRDY) | ||
73 | break; | ||
74 | msleep(20); | ||
75 | } | ||
76 | |||
77 | if (tries < 0) { | ||
78 | dev_err(&data->client->dev, "data not ready\n"); | ||
79 | return -EIO; | ||
80 | } | ||
81 | |||
82 | return 0; | ||
83 | } | ||
84 | |||
85 | static int mag3110_read(struct mag3110_data *data, __be16 buf[3]) | ||
86 | { | ||
87 | int ret; | ||
88 | |||
89 | mutex_lock(&data->lock); | ||
90 | ret = mag3110_request(data); | ||
91 | if (ret < 0) { | ||
92 | mutex_unlock(&data->lock); | ||
93 | return ret; | ||
94 | } | ||
95 | ret = i2c_smbus_read_i2c_block_data(data->client, | ||
96 | MAG3110_OUT_X, 3 * sizeof(__be16), (u8 *) buf); | ||
97 | mutex_unlock(&data->lock); | ||
98 | |||
99 | return ret; | ||
100 | } | ||
101 | |||
102 | static ssize_t mag3110_show_int_plus_micros(char *buf, | ||
103 | const int (*vals)[2], int n) | ||
104 | { | ||
105 | size_t len = 0; | ||
106 | |||
107 | while (n-- > 0) | ||
108 | len += scnprintf(buf + len, PAGE_SIZE - len, | ||
109 | "%d.%d ", vals[n][0], vals[n][1]); | ||
110 | |||
111 | /* replace trailing space by newline */ | ||
112 | buf[len - 1] = '\n'; | ||
113 | |||
114 | return len; | ||
115 | } | ||
116 | |||
117 | static int mag3110_get_int_plus_micros_index(const int (*vals)[2], int n, | ||
118 | int val, int val2) | ||
119 | { | ||
120 | while (n-- > 0) | ||
121 | if (val == vals[n][0] && val2 == vals[n][1]) | ||
122 | return n; | ||
123 | |||
124 | return -EINVAL; | ||
125 | } | ||
126 | |||
127 | static const int mag3110_samp_freq[8][2] = { | ||
128 | {80, 0}, {40, 0}, {20, 0}, {10, 0}, {5, 0}, {2, 500000}, | ||
129 | {1, 250000}, {0, 625000} | ||
130 | }; | ||
131 | |||
132 | static ssize_t mag3110_show_samp_freq_avail(struct device *dev, | ||
133 | struct device_attribute *attr, char *buf) | ||
134 | { | ||
135 | return mag3110_show_int_plus_micros(buf, mag3110_samp_freq, 8); | ||
136 | } | ||
137 | |||
138 | static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mag3110_show_samp_freq_avail); | ||
139 | |||
140 | static int mag3110_get_samp_freq_index(struct mag3110_data *data, | ||
141 | int val, int val2) | ||
142 | { | ||
143 | return mag3110_get_int_plus_micros_index(mag3110_samp_freq, 8, val, | ||
144 | val2); | ||
145 | } | ||
146 | |||
147 | static int mag3110_read_raw(struct iio_dev *indio_dev, | ||
148 | struct iio_chan_spec const *chan, | ||
149 | int *val, int *val2, long mask) | ||
150 | { | ||
151 | struct mag3110_data *data = iio_priv(indio_dev); | ||
152 | __be16 buffer[3]; | ||
153 | int i, ret; | ||
154 | |||
155 | switch (mask) { | ||
156 | case IIO_CHAN_INFO_RAW: | ||
157 | switch (chan->type) { | ||
158 | case IIO_MAGN: /* in 0.1 uT / LSB */ | ||
159 | ret = mag3110_read(data, buffer); | ||
160 | if (ret < 0) | ||
161 | return ret; | ||
162 | *val = sign_extend32( | ||
163 | be16_to_cpu(buffer[chan->scan_index]), 15); | ||
164 | return IIO_VAL_INT; | ||
165 | case IIO_TEMP: /* in 1 C / LSB */ | ||
166 | mutex_lock(&data->lock); | ||
167 | ret = mag3110_request(data); | ||
168 | if (ret < 0) { | ||
169 | mutex_unlock(&data->lock); | ||
170 | return ret; | ||
171 | } | ||
172 | ret = i2c_smbus_read_byte_data(data->client, | ||
173 | MAG3110_DIE_TEMP); | ||
174 | mutex_unlock(&data->lock); | ||
175 | if (ret < 0) | ||
176 | return ret; | ||
177 | *val = sign_extend32(ret, 7); | ||
178 | return IIO_VAL_INT; | ||
179 | default: | ||
180 | return -EINVAL; | ||
181 | } | ||
182 | case IIO_CHAN_INFO_SCALE: | ||
183 | *val = 0; | ||
184 | *val2 = 1000; | ||
185 | return IIO_VAL_INT_PLUS_MICRO; | ||
186 | case IIO_CHAN_INFO_SAMP_FREQ: | ||
187 | i = data->ctrl_reg1 >> MAG3110_CTRL_DR_SHIFT; | ||
188 | *val = mag3110_samp_freq[i][0]; | ||
189 | *val2 = mag3110_samp_freq[i][1]; | ||
190 | return IIO_VAL_INT_PLUS_MICRO; | ||
191 | } | ||
192 | return -EINVAL; | ||
193 | } | ||
194 | |||
195 | static int mag3110_write_raw(struct iio_dev *indio_dev, | ||
196 | struct iio_chan_spec const *chan, | ||
197 | int val, int val2, long mask) | ||
198 | { | ||
199 | struct mag3110_data *data = iio_priv(indio_dev); | ||
200 | int rate; | ||
201 | |||
202 | switch (mask) { | ||
203 | case IIO_CHAN_INFO_SAMP_FREQ: | ||
204 | rate = mag3110_get_samp_freq_index(data, val, val2); | ||
205 | if (rate < 0) | ||
206 | return -EINVAL; | ||
207 | |||
208 | data->ctrl_reg1 &= ~MAG3110_CTRL_DR_MASK; | ||
209 | data->ctrl_reg1 |= rate << MAG3110_CTRL_DR_SHIFT; | ||
210 | return i2c_smbus_write_byte_data(data->client, | ||
211 | MAG3110_CTRL_REG1, data->ctrl_reg1); | ||
212 | default: | ||
213 | return -EINVAL; | ||
214 | } | ||
215 | } | ||
216 | |||
217 | static irqreturn_t mag3110_trigger_handler(int irq, void *p) | ||
218 | { | ||
219 | struct iio_poll_func *pf = p; | ||
220 | struct iio_dev *indio_dev = pf->indio_dev; | ||
221 | struct mag3110_data *data = iio_priv(indio_dev); | ||
222 | u8 buffer[16]; /* 3 16-bit channels + 1 byte temp + padding + ts */ | ||
223 | int ret; | ||
224 | |||
225 | ret = mag3110_read(data, (__be16 *) buffer); | ||
226 | if (ret < 0) | ||
227 | goto done; | ||
228 | |||
229 | if (test_bit(3, indio_dev->active_scan_mask)) { | ||
230 | ret = i2c_smbus_read_byte_data(data->client, | ||
231 | MAG3110_DIE_TEMP); | ||
232 | if (ret < 0) | ||
233 | goto done; | ||
234 | buffer[6] = ret; | ||
235 | } | ||
236 | |||
237 | iio_push_to_buffers_with_timestamp(indio_dev, buffer, | ||
238 | iio_get_time_ns()); | ||
239 | |||
240 | done: | ||
241 | iio_trigger_notify_done(indio_dev->trig); | ||
242 | return IRQ_HANDLED; | ||
243 | } | ||
244 | |||
245 | #define MAG3110_CHANNEL(axis, idx) { \ | ||
246 | .type = IIO_MAGN, \ | ||
247 | .modified = 1, \ | ||
248 | .channel2 = IIO_MOD_##axis, \ | ||
249 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ | ||
250 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ | ||
251 | BIT(IIO_CHAN_INFO_SCALE), \ | ||
252 | .scan_index = idx, \ | ||
253 | .scan_type = IIO_ST('s', 16, 16, IIO_BE), \ | ||
254 | } | ||
255 | |||
256 | static const struct iio_chan_spec mag3110_channels[] = { | ||
257 | MAG3110_CHANNEL(X, 0), | ||
258 | MAG3110_CHANNEL(Y, 1), | ||
259 | MAG3110_CHANNEL(Z, 2), | ||
260 | { | ||
261 | .type = IIO_TEMP, | ||
262 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), | ||
263 | .scan_index = 3, | ||
264 | .scan_type = IIO_ST('s', 8, 8, 0), | ||
265 | }, | ||
266 | IIO_CHAN_SOFT_TIMESTAMP(4), | ||
267 | }; | ||
268 | |||
269 | static struct attribute *mag3110_attributes[] = { | ||
270 | &iio_dev_attr_sampling_frequency_available.dev_attr.attr, | ||
271 | NULL | ||
272 | }; | ||
273 | |||
274 | static const struct attribute_group mag3110_group = { | ||
275 | .attrs = mag3110_attributes, | ||
276 | }; | ||
277 | |||
278 | static const struct iio_info mag3110_info = { | ||
279 | .attrs = &mag3110_group, | ||
280 | .read_raw = &mag3110_read_raw, | ||
281 | .write_raw = &mag3110_write_raw, | ||
282 | .driver_module = THIS_MODULE, | ||
283 | }; | ||
284 | |||
285 | static const unsigned long mag3110_scan_masks[] = {0x7, 0xf, 0}; | ||
286 | |||
287 | static int mag3110_probe(struct i2c_client *client, | ||
288 | const struct i2c_device_id *id) | ||
289 | { | ||
290 | struct mag3110_data *data; | ||
291 | struct iio_dev *indio_dev; | ||
292 | int ret; | ||
293 | |||
294 | ret = i2c_smbus_read_byte_data(client, MAG3110_WHO_AM_I); | ||
295 | if (ret < 0) | ||
296 | return ret; | ||
297 | if (ret != MAG3110_DEVICE_ID) | ||
298 | return -ENODEV; | ||
299 | |||
300 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); | ||
301 | if (!indio_dev) | ||
302 | return -ENOMEM; | ||
303 | |||
304 | data = iio_priv(indio_dev); | ||
305 | data->client = client; | ||
306 | mutex_init(&data->lock); | ||
307 | |||
308 | i2c_set_clientdata(client, indio_dev); | ||
309 | indio_dev->info = &mag3110_info; | ||
310 | indio_dev->name = id->name; | ||
311 | indio_dev->dev.parent = &client->dev; | ||
312 | indio_dev->modes = INDIO_DIRECT_MODE; | ||
313 | indio_dev->channels = mag3110_channels; | ||
314 | indio_dev->num_channels = ARRAY_SIZE(mag3110_channels); | ||
315 | indio_dev->available_scan_masks = mag3110_scan_masks; | ||
316 | |||
317 | data->ctrl_reg1 = MAG3110_CTRL_DR_DEFAULT; | ||
318 | ret = i2c_smbus_write_byte_data(client, MAG3110_CTRL_REG1, | ||
319 | data->ctrl_reg1); | ||
320 | if (ret < 0) | ||
321 | return ret; | ||
322 | |||
323 | ret = i2c_smbus_write_byte_data(client, MAG3110_CTRL_REG2, | ||
324 | MAG3110_CTRL_AUTO_MRST_EN | MAG3110_CTRL_RAW); | ||
325 | if (ret < 0) | ||
326 | return ret; | ||
327 | |||
328 | ret = iio_triggered_buffer_setup(indio_dev, NULL, | ||
329 | mag3110_trigger_handler, NULL); | ||
330 | if (ret < 0) | ||
331 | return ret; | ||
332 | |||
333 | ret = iio_device_register(indio_dev); | ||
334 | if (ret < 0) | ||
335 | goto buffer_cleanup; | ||
336 | return 0; | ||
337 | |||
338 | buffer_cleanup: | ||
339 | iio_triggered_buffer_cleanup(indio_dev); | ||
340 | return ret; | ||
341 | } | ||
342 | |||
343 | static int mag3110_standby(struct mag3110_data *data) | ||
344 | { | ||
345 | return i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1, | ||
346 | data->ctrl_reg1 & ~MAG3110_CTRL_AC); | ||
347 | } | ||
348 | |||
349 | static int mag3110_remove(struct i2c_client *client) | ||
350 | { | ||
351 | struct iio_dev *indio_dev = i2c_get_clientdata(client); | ||
352 | |||
353 | iio_device_unregister(indio_dev); | ||
354 | iio_triggered_buffer_cleanup(indio_dev); | ||
355 | mag3110_standby(iio_priv(indio_dev)); | ||
356 | |||
357 | return 0; | ||
358 | } | ||
359 | |||
360 | #ifdef CONFIG_PM_SLEEP | ||
361 | static int mag3110_suspend(struct device *dev) | ||
362 | { | ||
363 | return mag3110_standby(iio_priv(i2c_get_clientdata( | ||
364 | to_i2c_client(dev)))); | ||
365 | } | ||
366 | |||
367 | static int mag3110_resume(struct device *dev) | ||
368 | { | ||
369 | struct mag3110_data *data = iio_priv(i2c_get_clientdata( | ||
370 | to_i2c_client(dev))); | ||
371 | |||
372 | return i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1, | ||
373 | data->ctrl_reg1); | ||
374 | } | ||
375 | |||
376 | static SIMPLE_DEV_PM_OPS(mag3110_pm_ops, mag3110_suspend, mag3110_resume); | ||
377 | #define MAG3110_PM_OPS (&mag3110_pm_ops) | ||
378 | #else | ||
379 | #define MAG3110_PM_OPS NULL | ||
380 | #endif | ||
381 | |||
382 | static const struct i2c_device_id mag3110_id[] = { | ||
383 | { "mag3110", 0 }, | ||
384 | { } | ||
385 | }; | ||
386 | MODULE_DEVICE_TABLE(i2c, mag3110_id); | ||
387 | |||
388 | static struct i2c_driver mag3110_driver = { | ||
389 | .driver = { | ||
390 | .name = "mag3110", | ||
391 | .pm = MAG3110_PM_OPS, | ||
392 | }, | ||
393 | .probe = mag3110_probe, | ||
394 | .remove = mag3110_remove, | ||
395 | .id_table = mag3110_id, | ||
396 | }; | ||
397 | module_i2c_driver(mag3110_driver); | ||
398 | |||
399 | MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); | ||
400 | MODULE_DESCRIPTION("Freescale MAG3110 magnetometer driver"); | ||
401 | MODULE_LICENSE("GPL"); | ||