]>
Commit | Line | Data |
---|---|---|
abeb6b1e DB |
1 | /* |
2 | * MMC35240 - MEMSIC 3-axis Magnetic Sensor | |
3 | * | |
4 | * Copyright (c) 2015, 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 MMC35240 (7-bit I2C slave address 0x30). | |
11 | * | |
12 | * TODO: offset, ACPI, continuous measurement mode, PM | |
13 | */ | |
14 | ||
15 | #include <linux/module.h> | |
16 | #include <linux/init.h> | |
17 | #include <linux/i2c.h> | |
18 | #include <linux/delay.h> | |
19 | #include <linux/regmap.h> | |
d11715f0 | 20 | #include <linux/acpi.h> |
553a776b | 21 | #include <linux/pm.h> |
abeb6b1e DB |
22 | |
23 | #include <linux/iio/iio.h> | |
24 | #include <linux/iio/sysfs.h> | |
25 | ||
26 | #define MMC35240_DRV_NAME "mmc35240" | |
27 | #define MMC35240_REGMAP_NAME "mmc35240_regmap" | |
28 | ||
29 | #define MMC35240_REG_XOUT_L 0x00 | |
30 | #define MMC35240_REG_XOUT_H 0x01 | |
31 | #define MMC35240_REG_YOUT_L 0x02 | |
32 | #define MMC35240_REG_YOUT_H 0x03 | |
33 | #define MMC35240_REG_ZOUT_L 0x04 | |
34 | #define MMC35240_REG_ZOUT_H 0x05 | |
35 | ||
36 | #define MMC35240_REG_STATUS 0x06 | |
37 | #define MMC35240_REG_CTRL0 0x07 | |
38 | #define MMC35240_REG_CTRL1 0x08 | |
39 | ||
40 | #define MMC35240_REG_ID 0x20 | |
41 | ||
42 | #define MMC35240_STATUS_MEAS_DONE_BIT BIT(0) | |
43 | ||
44 | #define MMC35240_CTRL0_REFILL_BIT BIT(7) | |
45 | #define MMC35240_CTRL0_RESET_BIT BIT(6) | |
46 | #define MMC35240_CTRL0_SET_BIT BIT(5) | |
47 | #define MMC35240_CTRL0_CMM_BIT BIT(1) | |
48 | #define MMC35240_CTRL0_TM_BIT BIT(0) | |
49 | ||
50 | /* output resolution bits */ | |
51 | #define MMC35240_CTRL1_BW0_BIT BIT(0) | |
52 | #define MMC35240_CTRL1_BW1_BIT BIT(1) | |
53 | ||
54 | #define MMC35240_CTRL1_BW_MASK (MMC35240_CTRL1_BW0_BIT | \ | |
55 | MMC35240_CTRL1_BW1_BIT) | |
56 | #define MMC35240_CTRL1_BW_SHIFT 0 | |
57 | ||
58 | #define MMC35240_WAIT_CHARGE_PUMP 50000 /* us */ | |
59 | #define MMC53240_WAIT_SET_RESET 1000 /* us */ | |
60 | ||
61 | enum mmc35240_resolution { | |
62 | MMC35240_16_BITS_SLOW = 0, /* 100 Hz */ | |
63 | MMC35240_16_BITS_FAST, /* 200 Hz */ | |
64 | MMC35240_14_BITS, /* 333 Hz */ | |
65 | MMC35240_12_BITS, /* 666 Hz */ | |
66 | }; | |
67 | ||
68 | enum mmc35240_axis { | |
69 | AXIS_X = 0, | |
70 | AXIS_Y, | |
71 | AXIS_Z, | |
72 | }; | |
73 | ||
74 | static const struct { | |
75 | int sens[3]; /* sensitivity per X, Y, Z axis */ | |
76 | int nfo; /* null field output */ | |
77 | } mmc35240_props_table[] = { | |
78 | /* 16 bits, 100Hz ODR */ | |
79 | { | |
80 | {1024, 1024, 770}, | |
81 | 32768, | |
82 | }, | |
83 | /* 16 bits, 200Hz ODR */ | |
84 | { | |
85 | {1024, 1024, 770}, | |
86 | 32768, | |
87 | }, | |
88 | /* 14 bits, 333Hz ODR */ | |
89 | { | |
90 | {256, 256, 193}, | |
91 | 8192, | |
92 | }, | |
93 | /* 12 bits, 666Hz ODR */ | |
94 | { | |
95 | {64, 64, 48}, | |
96 | 2048, | |
97 | }, | |
98 | }; | |
99 | ||
100 | struct mmc35240_data { | |
101 | struct i2c_client *client; | |
102 | struct mutex mutex; | |
103 | struct regmap *regmap; | |
104 | enum mmc35240_resolution res; | |
105 | }; | |
106 | ||
107 | static const int mmc35240_samp_freq[] = {100, 200, 333, 666}; | |
108 | ||
109 | static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("100 200 333 666"); | |
110 | ||
111 | #define MMC35240_CHANNEL(_axis) { \ | |
112 | .type = IIO_MAGN, \ | |
113 | .modified = 1, \ | |
114 | .channel2 = IIO_MOD_ ## _axis, \ | |
115 | .address = AXIS_ ## _axis, \ | |
c2890547 DB |
116 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
117 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ | |
118 | BIT(IIO_CHAN_INFO_SCALE), \ | |
abeb6b1e DB |
119 | } |
120 | ||
121 | static const struct iio_chan_spec mmc35240_channels[] = { | |
122 | MMC35240_CHANNEL(X), | |
123 | MMC35240_CHANNEL(Y), | |
124 | MMC35240_CHANNEL(Z), | |
125 | }; | |
126 | ||
127 | static struct attribute *mmc35240_attributes[] = { | |
128 | &iio_const_attr_sampling_frequency_available.dev_attr.attr, | |
bd35a214 | 129 | NULL |
abeb6b1e DB |
130 | }; |
131 | ||
132 | static const struct attribute_group mmc35240_attribute_group = { | |
133 | .attrs = mmc35240_attributes, | |
134 | }; | |
135 | ||
136 | static int mmc35240_get_samp_freq_index(struct mmc35240_data *data, | |
137 | int val, int val2) | |
138 | { | |
139 | int i; | |
140 | ||
141 | for (i = 0; i < ARRAY_SIZE(mmc35240_samp_freq); i++) | |
142 | if (mmc35240_samp_freq[i] == val) | |
143 | return i; | |
144 | return -EINVAL; | |
145 | } | |
146 | ||
147 | static int mmc35240_hw_set(struct mmc35240_data *data, bool set) | |
148 | { | |
149 | int ret; | |
150 | u8 coil_bit; | |
151 | ||
152 | /* | |
153 | * Recharge the capacitor at VCAP pin, requested to be issued | |
154 | * before a SET/RESET command. | |
155 | */ | |
156 | ret = regmap_update_bits(data->regmap, MMC35240_REG_CTRL0, | |
157 | MMC35240_CTRL0_REFILL_BIT, | |
158 | MMC35240_CTRL0_REFILL_BIT); | |
159 | if (ret < 0) | |
160 | return ret; | |
161 | usleep_range(MMC35240_WAIT_CHARGE_PUMP, MMC35240_WAIT_CHARGE_PUMP + 1); | |
162 | ||
163 | if (set) | |
164 | coil_bit = MMC35240_CTRL0_SET_BIT; | |
165 | else | |
166 | coil_bit = MMC35240_CTRL0_RESET_BIT; | |
167 | ||
168 | return regmap_update_bits(data->regmap, MMC35240_REG_CTRL0, | |
169 | MMC35240_CTRL0_REFILL_BIT, | |
170 | coil_bit); | |
171 | } | |
172 | ||
173 | static int mmc35240_init(struct mmc35240_data *data) | |
174 | { | |
175 | int ret; | |
176 | unsigned int reg_id; | |
177 | ||
178 | ret = regmap_read(data->regmap, MMC35240_REG_ID, ®_id); | |
179 | if (ret < 0) { | |
180 | dev_err(&data->client->dev, "Error reading product id\n"); | |
181 | return ret; | |
182 | } | |
183 | ||
184 | dev_dbg(&data->client->dev, "MMC35240 chip id %x\n", reg_id); | |
185 | ||
186 | /* | |
187 | * make sure we restore sensor characteristics, by doing | |
188 | * a RESET/SET sequence | |
189 | */ | |
190 | ret = mmc35240_hw_set(data, false); | |
191 | if (ret < 0) | |
192 | return ret; | |
193 | usleep_range(MMC53240_WAIT_SET_RESET, MMC53240_WAIT_SET_RESET + 1); | |
194 | ||
195 | ret = mmc35240_hw_set(data, true); | |
196 | if (ret < 0) | |
197 | return ret; | |
198 | ||
199 | /* set default sampling frequency */ | |
200 | return regmap_update_bits(data->regmap, MMC35240_REG_CTRL1, | |
201 | MMC35240_CTRL1_BW_MASK, | |
202 | data->res << MMC35240_CTRL1_BW_SHIFT); | |
203 | } | |
204 | ||
205 | static int mmc35240_take_measurement(struct mmc35240_data *data) | |
206 | { | |
207 | int ret, tries = 100; | |
208 | unsigned int reg_status; | |
209 | ||
210 | ret = regmap_write(data->regmap, MMC35240_REG_CTRL0, | |
211 | MMC35240_CTRL0_TM_BIT); | |
212 | if (ret < 0) | |
213 | return ret; | |
214 | ||
215 | while (tries-- > 0) { | |
216 | ret = regmap_read(data->regmap, MMC35240_REG_STATUS, | |
217 | ®_status); | |
218 | if (ret < 0) | |
219 | return ret; | |
220 | if (reg_status & MMC35240_STATUS_MEAS_DONE_BIT) | |
221 | break; | |
787f55c4 DB |
222 | /* minimum wait time to complete measurement is 10 ms */ |
223 | usleep_range(10000, 11000); | |
abeb6b1e DB |
224 | } |
225 | ||
226 | if (tries < 0) { | |
227 | dev_err(&data->client->dev, "data not ready\n"); | |
228 | return -EIO; | |
229 | } | |
230 | ||
231 | return 0; | |
232 | } | |
233 | ||
234 | static int mmc35240_read_measurement(struct mmc35240_data *data, __le16 buf[3]) | |
235 | { | |
236 | int ret; | |
237 | ||
238 | ret = mmc35240_take_measurement(data); | |
239 | if (ret < 0) | |
240 | return ret; | |
241 | ||
242 | return regmap_bulk_read(data->regmap, MMC35240_REG_XOUT_L, (u8 *)buf, | |
243 | 3 * sizeof(__le16)); | |
244 | } | |
245 | ||
c2890547 DB |
246 | /** |
247 | * mmc35240_raw_to_mgauss - convert raw readings to milli gauss. Also apply | |
248 | compensation for output value. | |
249 | * | |
250 | * @data: device private data | |
251 | * @index: axis index for which we want the conversion | |
252 | * @buf: raw data to be converted, 2 bytes in little endian format | |
253 | * @val: compensated output reading (unit is milli gauss) | |
254 | * | |
255 | * Returns: 0 in case of success, -EINVAL when @index is not valid | |
256 | */ | |
257 | static int mmc35240_raw_to_mgauss(struct mmc35240_data *data, int index, | |
258 | __le16 buf[], int *val) | |
abeb6b1e DB |
259 | { |
260 | int raw_x, raw_y, raw_z; | |
261 | int sens_x, sens_y, sens_z; | |
262 | int nfo; | |
263 | ||
264 | raw_x = le16_to_cpu(buf[AXIS_X]); | |
265 | raw_y = le16_to_cpu(buf[AXIS_Y]); | |
266 | raw_z = le16_to_cpu(buf[AXIS_Z]); | |
267 | ||
268 | sens_x = mmc35240_props_table[data->res].sens[AXIS_X]; | |
269 | sens_y = mmc35240_props_table[data->res].sens[AXIS_Y]; | |
270 | sens_z = mmc35240_props_table[data->res].sens[AXIS_Z]; | |
271 | ||
272 | nfo = mmc35240_props_table[data->res].nfo; | |
273 | ||
274 | switch (index) { | |
275 | case AXIS_X: | |
c2890547 | 276 | *val = (raw_x - nfo) * 1000 / sens_x; |
abeb6b1e DB |
277 | break; |
278 | case AXIS_Y: | |
c2890547 DB |
279 | *val = (raw_y - nfo) * 1000 / sens_y - |
280 | (raw_z - nfo) * 1000 / sens_z; | |
abeb6b1e DB |
281 | break; |
282 | case AXIS_Z: | |
c2890547 DB |
283 | *val = (raw_y - nfo) * 1000 / sens_y + |
284 | (raw_z - nfo) * 1000 / sens_z; | |
abeb6b1e DB |
285 | break; |
286 | default: | |
287 | return -EINVAL; | |
288 | } | |
289 | return 0; | |
290 | } | |
291 | ||
292 | static int mmc35240_read_raw(struct iio_dev *indio_dev, | |
293 | struct iio_chan_spec const *chan, int *val, | |
294 | int *val2, long mask) | |
295 | { | |
296 | struct mmc35240_data *data = iio_priv(indio_dev); | |
297 | int ret, i; | |
298 | unsigned int reg; | |
299 | __le16 buf[3]; | |
300 | ||
301 | switch (mask) { | |
c2890547 | 302 | case IIO_CHAN_INFO_RAW: |
abeb6b1e DB |
303 | mutex_lock(&data->mutex); |
304 | ret = mmc35240_read_measurement(data, buf); | |
305 | mutex_unlock(&data->mutex); | |
306 | if (ret < 0) | |
307 | return ret; | |
c2890547 | 308 | ret = mmc35240_raw_to_mgauss(data, chan->address, buf, val); |
abeb6b1e DB |
309 | if (ret < 0) |
310 | return ret; | |
c2890547 DB |
311 | return IIO_VAL_INT; |
312 | case IIO_CHAN_INFO_SCALE: | |
313 | *val = 0; | |
314 | *val2 = 1000; | |
abeb6b1e DB |
315 | return IIO_VAL_INT_PLUS_MICRO; |
316 | case IIO_CHAN_INFO_SAMP_FREQ: | |
317 | mutex_lock(&data->mutex); | |
318 | ret = regmap_read(data->regmap, MMC35240_REG_CTRL1, ®); | |
319 | mutex_unlock(&data->mutex); | |
320 | if (ret < 0) | |
321 | return ret; | |
322 | ||
323 | i = (reg & MMC35240_CTRL1_BW_MASK) >> MMC35240_CTRL1_BW_SHIFT; | |
5517547b | 324 | if (i < 0 || i >= ARRAY_SIZE(mmc35240_samp_freq)) |
abeb6b1e DB |
325 | return -EINVAL; |
326 | ||
327 | *val = mmc35240_samp_freq[i]; | |
328 | *val2 = 0; | |
329 | return IIO_VAL_INT; | |
330 | default: | |
331 | return -EINVAL; | |
332 | } | |
333 | } | |
334 | ||
335 | static int mmc35240_write_raw(struct iio_dev *indio_dev, | |
336 | struct iio_chan_spec const *chan, int val, | |
337 | int val2, long mask) | |
338 | { | |
339 | struct mmc35240_data *data = iio_priv(indio_dev); | |
340 | int i, ret; | |
341 | ||
342 | switch (mask) { | |
343 | case IIO_CHAN_INFO_SAMP_FREQ: | |
344 | i = mmc35240_get_samp_freq_index(data, val, val2); | |
345 | if (i < 0) | |
346 | return -EINVAL; | |
347 | mutex_lock(&data->mutex); | |
348 | ret = regmap_update_bits(data->regmap, MMC35240_REG_CTRL1, | |
349 | MMC35240_CTRL1_BW_MASK, | |
350 | i << MMC35240_CTRL1_BW_SHIFT); | |
351 | mutex_unlock(&data->mutex); | |
352 | return ret; | |
353 | default: | |
354 | return -EINVAL; | |
355 | } | |
356 | } | |
357 | ||
358 | static const struct iio_info mmc35240_info = { | |
359 | .driver_module = THIS_MODULE, | |
360 | .read_raw = mmc35240_read_raw, | |
361 | .write_raw = mmc35240_write_raw, | |
362 | .attrs = &mmc35240_attribute_group, | |
363 | }; | |
364 | ||
365 | static bool mmc35240_is_writeable_reg(struct device *dev, unsigned int reg) | |
366 | { | |
367 | switch (reg) { | |
368 | case MMC35240_REG_CTRL0: | |
369 | case MMC35240_REG_CTRL1: | |
370 | return true; | |
371 | default: | |
372 | return false; | |
373 | } | |
374 | } | |
375 | ||
376 | static bool mmc35240_is_readable_reg(struct device *dev, unsigned int reg) | |
377 | { | |
378 | switch (reg) { | |
379 | case MMC35240_REG_XOUT_L: | |
380 | case MMC35240_REG_XOUT_H: | |
381 | case MMC35240_REG_YOUT_L: | |
382 | case MMC35240_REG_YOUT_H: | |
383 | case MMC35240_REG_ZOUT_L: | |
384 | case MMC35240_REG_ZOUT_H: | |
385 | case MMC35240_REG_STATUS: | |
386 | case MMC35240_REG_ID: | |
387 | return true; | |
388 | default: | |
389 | return false; | |
390 | } | |
391 | } | |
392 | ||
393 | static bool mmc35240_is_volatile_reg(struct device *dev, unsigned int reg) | |
394 | { | |
395 | switch (reg) { | |
396 | case MMC35240_REG_CTRL0: | |
397 | case MMC35240_REG_CTRL1: | |
398 | return false; | |
399 | default: | |
400 | return true; | |
401 | } | |
402 | } | |
403 | ||
404 | static struct reg_default mmc35240_reg_defaults[] = { | |
405 | { MMC35240_REG_CTRL0, 0x00 }, | |
406 | { MMC35240_REG_CTRL1, 0x00 }, | |
407 | }; | |
408 | ||
409 | static const struct regmap_config mmc35240_regmap_config = { | |
410 | .name = MMC35240_REGMAP_NAME, | |
411 | ||
412 | .reg_bits = 8, | |
413 | .val_bits = 8, | |
414 | ||
415 | .max_register = MMC35240_REG_ID, | |
416 | .cache_type = REGCACHE_FLAT, | |
417 | ||
418 | .writeable_reg = mmc35240_is_writeable_reg, | |
419 | .readable_reg = mmc35240_is_readable_reg, | |
420 | .volatile_reg = mmc35240_is_volatile_reg, | |
421 | ||
422 | .reg_defaults = mmc35240_reg_defaults, | |
423 | .num_reg_defaults = ARRAY_SIZE(mmc35240_reg_defaults), | |
424 | }; | |
425 | ||
426 | static int mmc35240_probe(struct i2c_client *client, | |
427 | const struct i2c_device_id *id) | |
428 | { | |
429 | struct mmc35240_data *data; | |
430 | struct iio_dev *indio_dev; | |
431 | struct regmap *regmap; | |
432 | int ret; | |
433 | ||
434 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); | |
435 | if (!indio_dev) | |
436 | return -ENOMEM; | |
437 | ||
438 | regmap = devm_regmap_init_i2c(client, &mmc35240_regmap_config); | |
439 | if (IS_ERR(regmap)) { | |
440 | dev_err(&client->dev, "regmap initialization failed\n"); | |
441 | return PTR_ERR(regmap); | |
442 | } | |
443 | ||
444 | data = iio_priv(indio_dev); | |
445 | data->client = client; | |
446 | data->regmap = regmap; | |
447 | data->res = MMC35240_16_BITS_SLOW; | |
448 | ||
449 | mutex_init(&data->mutex); | |
450 | ||
451 | indio_dev->dev.parent = &client->dev; | |
452 | indio_dev->info = &mmc35240_info; | |
453 | indio_dev->name = MMC35240_DRV_NAME; | |
454 | indio_dev->channels = mmc35240_channels; | |
455 | indio_dev->num_channels = ARRAY_SIZE(mmc35240_channels); | |
456 | indio_dev->modes = INDIO_DIRECT_MODE; | |
457 | ||
458 | ret = mmc35240_init(data); | |
459 | if (ret < 0) { | |
460 | dev_err(&client->dev, "mmc35240 chip init failed\n"); | |
461 | return ret; | |
462 | } | |
463 | return devm_iio_device_register(&client->dev, indio_dev); | |
464 | } | |
465 | ||
553a776b DB |
466 | #ifdef CONFIG_PM_SLEEP |
467 | static int mmc35240_suspend(struct device *dev) | |
468 | { | |
469 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); | |
470 | struct mmc35240_data *data = iio_priv(indio_dev); | |
471 | ||
472 | regcache_cache_only(data->regmap, true); | |
473 | ||
474 | return 0; | |
475 | } | |
476 | ||
477 | static int mmc35240_resume(struct device *dev) | |
478 | { | |
479 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); | |
480 | struct mmc35240_data *data = iio_priv(indio_dev); | |
481 | int ret; | |
482 | ||
483 | regcache_mark_dirty(data->regmap); | |
484 | ret = regcache_sync_region(data->regmap, MMC35240_REG_CTRL0, | |
485 | MMC35240_REG_CTRL1); | |
486 | if (ret < 0) | |
487 | dev_err(dev, "Failed to restore control registers\n"); | |
488 | ||
489 | regcache_cache_only(data->regmap, false); | |
490 | ||
491 | return 0; | |
492 | } | |
493 | #endif | |
494 | ||
495 | static const struct dev_pm_ops mmc35240_pm_ops = { | |
496 | SET_SYSTEM_SLEEP_PM_OPS(mmc35240_suspend, mmc35240_resume) | |
497 | }; | |
498 | ||
d11715f0 DB |
499 | static const struct acpi_device_id mmc35240_acpi_match[] = { |
500 | {"MMC35240", 0}, | |
501 | { }, | |
502 | }; | |
503 | MODULE_DEVICE_TABLE(acpi, mmc35240_acpi_match); | |
504 | ||
abeb6b1e | 505 | static const struct i2c_device_id mmc35240_id[] = { |
a52ffebc | 506 | {"mmc35240", 0}, |
abeb6b1e DB |
507 | {} |
508 | }; | |
509 | MODULE_DEVICE_TABLE(i2c, mmc35240_id); | |
510 | ||
511 | static struct i2c_driver mmc35240_driver = { | |
512 | .driver = { | |
513 | .name = MMC35240_DRV_NAME, | |
553a776b | 514 | .pm = &mmc35240_pm_ops, |
d11715f0 | 515 | .acpi_match_table = ACPI_PTR(mmc35240_acpi_match), |
abeb6b1e DB |
516 | }, |
517 | .probe = mmc35240_probe, | |
518 | .id_table = mmc35240_id, | |
519 | }; | |
520 | ||
521 | module_i2c_driver(mmc35240_driver); | |
522 | ||
523 | MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>"); | |
524 | MODULE_DESCRIPTION("MEMSIC MMC35240 magnetic sensor driver"); | |
525 | MODULE_LICENSE("GPL v2"); |