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, \ | |
116 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \ | |
117 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ | |
118 | } | |
119 | ||
120 | static const struct iio_chan_spec mmc35240_channels[] = { | |
121 | MMC35240_CHANNEL(X), | |
122 | MMC35240_CHANNEL(Y), | |
123 | MMC35240_CHANNEL(Z), | |
124 | }; | |
125 | ||
126 | static struct attribute *mmc35240_attributes[] = { | |
127 | &iio_const_attr_sampling_frequency_available.dev_attr.attr, | |
128 | }; | |
129 | ||
130 | static const struct attribute_group mmc35240_attribute_group = { | |
131 | .attrs = mmc35240_attributes, | |
132 | }; | |
133 | ||
134 | static int mmc35240_get_samp_freq_index(struct mmc35240_data *data, | |
135 | int val, int val2) | |
136 | { | |
137 | int i; | |
138 | ||
139 | for (i = 0; i < ARRAY_SIZE(mmc35240_samp_freq); i++) | |
140 | if (mmc35240_samp_freq[i] == val) | |
141 | return i; | |
142 | return -EINVAL; | |
143 | } | |
144 | ||
145 | static int mmc35240_hw_set(struct mmc35240_data *data, bool set) | |
146 | { | |
147 | int ret; | |
148 | u8 coil_bit; | |
149 | ||
150 | /* | |
151 | * Recharge the capacitor at VCAP pin, requested to be issued | |
152 | * before a SET/RESET command. | |
153 | */ | |
154 | ret = regmap_update_bits(data->regmap, MMC35240_REG_CTRL0, | |
155 | MMC35240_CTRL0_REFILL_BIT, | |
156 | MMC35240_CTRL0_REFILL_BIT); | |
157 | if (ret < 0) | |
158 | return ret; | |
159 | usleep_range(MMC35240_WAIT_CHARGE_PUMP, MMC35240_WAIT_CHARGE_PUMP + 1); | |
160 | ||
161 | if (set) | |
162 | coil_bit = MMC35240_CTRL0_SET_BIT; | |
163 | else | |
164 | coil_bit = MMC35240_CTRL0_RESET_BIT; | |
165 | ||
166 | return regmap_update_bits(data->regmap, MMC35240_REG_CTRL0, | |
167 | MMC35240_CTRL0_REFILL_BIT, | |
168 | coil_bit); | |
169 | } | |
170 | ||
171 | static int mmc35240_init(struct mmc35240_data *data) | |
172 | { | |
173 | int ret; | |
174 | unsigned int reg_id; | |
175 | ||
176 | ret = regmap_read(data->regmap, MMC35240_REG_ID, ®_id); | |
177 | if (ret < 0) { | |
178 | dev_err(&data->client->dev, "Error reading product id\n"); | |
179 | return ret; | |
180 | } | |
181 | ||
182 | dev_dbg(&data->client->dev, "MMC35240 chip id %x\n", reg_id); | |
183 | ||
184 | /* | |
185 | * make sure we restore sensor characteristics, by doing | |
186 | * a RESET/SET sequence | |
187 | */ | |
188 | ret = mmc35240_hw_set(data, false); | |
189 | if (ret < 0) | |
190 | return ret; | |
191 | usleep_range(MMC53240_WAIT_SET_RESET, MMC53240_WAIT_SET_RESET + 1); | |
192 | ||
193 | ret = mmc35240_hw_set(data, true); | |
194 | if (ret < 0) | |
195 | return ret; | |
196 | ||
197 | /* set default sampling frequency */ | |
198 | return regmap_update_bits(data->regmap, MMC35240_REG_CTRL1, | |
199 | MMC35240_CTRL1_BW_MASK, | |
200 | data->res << MMC35240_CTRL1_BW_SHIFT); | |
201 | } | |
202 | ||
203 | static int mmc35240_take_measurement(struct mmc35240_data *data) | |
204 | { | |
205 | int ret, tries = 100; | |
206 | unsigned int reg_status; | |
207 | ||
208 | ret = regmap_write(data->regmap, MMC35240_REG_CTRL0, | |
209 | MMC35240_CTRL0_TM_BIT); | |
210 | if (ret < 0) | |
211 | return ret; | |
212 | ||
213 | while (tries-- > 0) { | |
214 | ret = regmap_read(data->regmap, MMC35240_REG_STATUS, | |
215 | ®_status); | |
216 | if (ret < 0) | |
217 | return ret; | |
218 | if (reg_status & MMC35240_STATUS_MEAS_DONE_BIT) | |
219 | break; | |
220 | msleep(20); | |
221 | } | |
222 | ||
223 | if (tries < 0) { | |
224 | dev_err(&data->client->dev, "data not ready\n"); | |
225 | return -EIO; | |
226 | } | |
227 | ||
228 | return 0; | |
229 | } | |
230 | ||
231 | static int mmc35240_read_measurement(struct mmc35240_data *data, __le16 buf[3]) | |
232 | { | |
233 | int ret; | |
234 | ||
235 | ret = mmc35240_take_measurement(data); | |
236 | if (ret < 0) | |
237 | return ret; | |
238 | ||
239 | return regmap_bulk_read(data->regmap, MMC35240_REG_XOUT_L, (u8 *)buf, | |
240 | 3 * sizeof(__le16)); | |
241 | } | |
242 | ||
243 | static int mmc35240_raw_to_gauss(struct mmc35240_data *data, int index, | |
244 | __le16 buf[], | |
245 | int *val, int *val2) | |
246 | { | |
247 | int raw_x, raw_y, raw_z; | |
248 | int sens_x, sens_y, sens_z; | |
249 | int nfo; | |
250 | ||
251 | raw_x = le16_to_cpu(buf[AXIS_X]); | |
252 | raw_y = le16_to_cpu(buf[AXIS_Y]); | |
253 | raw_z = le16_to_cpu(buf[AXIS_Z]); | |
254 | ||
255 | sens_x = mmc35240_props_table[data->res].sens[AXIS_X]; | |
256 | sens_y = mmc35240_props_table[data->res].sens[AXIS_Y]; | |
257 | sens_z = mmc35240_props_table[data->res].sens[AXIS_Z]; | |
258 | ||
259 | nfo = mmc35240_props_table[data->res].nfo; | |
260 | ||
261 | switch (index) { | |
262 | case AXIS_X: | |
263 | *val = (raw_x - nfo) / sens_x; | |
264 | *val2 = ((raw_x - nfo) % sens_x) * 1000000; | |
265 | break; | |
266 | case AXIS_Y: | |
267 | *val = (raw_y - nfo) / sens_y - (raw_z - nfo) / sens_z; | |
268 | *val2 = (((raw_y - nfo) % sens_y - (raw_z - nfo) % sens_z)) | |
269 | * 1000000; | |
270 | break; | |
271 | case AXIS_Z: | |
272 | *val = (raw_y - nfo) / sens_y + (raw_z - nfo) / sens_z; | |
273 | *val2 = (((raw_y - nfo) % sens_y + (raw_z - nfo) % sens_z)) | |
274 | * 1000000; | |
275 | break; | |
276 | default: | |
277 | return -EINVAL; | |
278 | } | |
279 | return 0; | |
280 | } | |
281 | ||
282 | static int mmc35240_read_raw(struct iio_dev *indio_dev, | |
283 | struct iio_chan_spec const *chan, int *val, | |
284 | int *val2, long mask) | |
285 | { | |
286 | struct mmc35240_data *data = iio_priv(indio_dev); | |
287 | int ret, i; | |
288 | unsigned int reg; | |
289 | __le16 buf[3]; | |
290 | ||
291 | switch (mask) { | |
292 | case IIO_CHAN_INFO_PROCESSED: | |
293 | mutex_lock(&data->mutex); | |
294 | ret = mmc35240_read_measurement(data, buf); | |
295 | mutex_unlock(&data->mutex); | |
296 | if (ret < 0) | |
297 | return ret; | |
298 | ret = mmc35240_raw_to_gauss(data, chan->address, | |
299 | buf, val, val2); | |
300 | if (ret < 0) | |
301 | return ret; | |
302 | return IIO_VAL_INT_PLUS_MICRO; | |
303 | case IIO_CHAN_INFO_SAMP_FREQ: | |
304 | mutex_lock(&data->mutex); | |
305 | ret = regmap_read(data->regmap, MMC35240_REG_CTRL1, ®); | |
306 | mutex_unlock(&data->mutex); | |
307 | if (ret < 0) | |
308 | return ret; | |
309 | ||
310 | i = (reg & MMC35240_CTRL1_BW_MASK) >> MMC35240_CTRL1_BW_SHIFT; | |
5517547b | 311 | if (i < 0 || i >= ARRAY_SIZE(mmc35240_samp_freq)) |
abeb6b1e DB |
312 | return -EINVAL; |
313 | ||
314 | *val = mmc35240_samp_freq[i]; | |
315 | *val2 = 0; | |
316 | return IIO_VAL_INT; | |
317 | default: | |
318 | return -EINVAL; | |
319 | } | |
320 | } | |
321 | ||
322 | static int mmc35240_write_raw(struct iio_dev *indio_dev, | |
323 | struct iio_chan_spec const *chan, int val, | |
324 | int val2, long mask) | |
325 | { | |
326 | struct mmc35240_data *data = iio_priv(indio_dev); | |
327 | int i, ret; | |
328 | ||
329 | switch (mask) { | |
330 | case IIO_CHAN_INFO_SAMP_FREQ: | |
331 | i = mmc35240_get_samp_freq_index(data, val, val2); | |
332 | if (i < 0) | |
333 | return -EINVAL; | |
334 | mutex_lock(&data->mutex); | |
335 | ret = regmap_update_bits(data->regmap, MMC35240_REG_CTRL1, | |
336 | MMC35240_CTRL1_BW_MASK, | |
337 | i << MMC35240_CTRL1_BW_SHIFT); | |
338 | mutex_unlock(&data->mutex); | |
339 | return ret; | |
340 | default: | |
341 | return -EINVAL; | |
342 | } | |
343 | } | |
344 | ||
345 | static const struct iio_info mmc35240_info = { | |
346 | .driver_module = THIS_MODULE, | |
347 | .read_raw = mmc35240_read_raw, | |
348 | .write_raw = mmc35240_write_raw, | |
349 | .attrs = &mmc35240_attribute_group, | |
350 | }; | |
351 | ||
352 | static bool mmc35240_is_writeable_reg(struct device *dev, unsigned int reg) | |
353 | { | |
354 | switch (reg) { | |
355 | case MMC35240_REG_CTRL0: | |
356 | case MMC35240_REG_CTRL1: | |
357 | return true; | |
358 | default: | |
359 | return false; | |
360 | } | |
361 | } | |
362 | ||
363 | static bool mmc35240_is_readable_reg(struct device *dev, unsigned int reg) | |
364 | { | |
365 | switch (reg) { | |
366 | case MMC35240_REG_XOUT_L: | |
367 | case MMC35240_REG_XOUT_H: | |
368 | case MMC35240_REG_YOUT_L: | |
369 | case MMC35240_REG_YOUT_H: | |
370 | case MMC35240_REG_ZOUT_L: | |
371 | case MMC35240_REG_ZOUT_H: | |
372 | case MMC35240_REG_STATUS: | |
373 | case MMC35240_REG_ID: | |
374 | return true; | |
375 | default: | |
376 | return false; | |
377 | } | |
378 | } | |
379 | ||
380 | static bool mmc35240_is_volatile_reg(struct device *dev, unsigned int reg) | |
381 | { | |
382 | switch (reg) { | |
383 | case MMC35240_REG_CTRL0: | |
384 | case MMC35240_REG_CTRL1: | |
385 | return false; | |
386 | default: | |
387 | return true; | |
388 | } | |
389 | } | |
390 | ||
391 | static struct reg_default mmc35240_reg_defaults[] = { | |
392 | { MMC35240_REG_CTRL0, 0x00 }, | |
393 | { MMC35240_REG_CTRL1, 0x00 }, | |
394 | }; | |
395 | ||
396 | static const struct regmap_config mmc35240_regmap_config = { | |
397 | .name = MMC35240_REGMAP_NAME, | |
398 | ||
399 | .reg_bits = 8, | |
400 | .val_bits = 8, | |
401 | ||
402 | .max_register = MMC35240_REG_ID, | |
403 | .cache_type = REGCACHE_FLAT, | |
404 | ||
405 | .writeable_reg = mmc35240_is_writeable_reg, | |
406 | .readable_reg = mmc35240_is_readable_reg, | |
407 | .volatile_reg = mmc35240_is_volatile_reg, | |
408 | ||
409 | .reg_defaults = mmc35240_reg_defaults, | |
410 | .num_reg_defaults = ARRAY_SIZE(mmc35240_reg_defaults), | |
411 | }; | |
412 | ||
413 | static int mmc35240_probe(struct i2c_client *client, | |
414 | const struct i2c_device_id *id) | |
415 | { | |
416 | struct mmc35240_data *data; | |
417 | struct iio_dev *indio_dev; | |
418 | struct regmap *regmap; | |
419 | int ret; | |
420 | ||
421 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); | |
422 | if (!indio_dev) | |
423 | return -ENOMEM; | |
424 | ||
425 | regmap = devm_regmap_init_i2c(client, &mmc35240_regmap_config); | |
426 | if (IS_ERR(regmap)) { | |
427 | dev_err(&client->dev, "regmap initialization failed\n"); | |
428 | return PTR_ERR(regmap); | |
429 | } | |
430 | ||
431 | data = iio_priv(indio_dev); | |
432 | data->client = client; | |
433 | data->regmap = regmap; | |
434 | data->res = MMC35240_16_BITS_SLOW; | |
435 | ||
436 | mutex_init(&data->mutex); | |
437 | ||
438 | indio_dev->dev.parent = &client->dev; | |
439 | indio_dev->info = &mmc35240_info; | |
440 | indio_dev->name = MMC35240_DRV_NAME; | |
441 | indio_dev->channels = mmc35240_channels; | |
442 | indio_dev->num_channels = ARRAY_SIZE(mmc35240_channels); | |
443 | indio_dev->modes = INDIO_DIRECT_MODE; | |
444 | ||
445 | ret = mmc35240_init(data); | |
446 | if (ret < 0) { | |
447 | dev_err(&client->dev, "mmc35240 chip init failed\n"); | |
448 | return ret; | |
449 | } | |
450 | return devm_iio_device_register(&client->dev, indio_dev); | |
451 | } | |
452 | ||
553a776b DB |
453 | #ifdef CONFIG_PM_SLEEP |
454 | static int mmc35240_suspend(struct device *dev) | |
455 | { | |
456 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); | |
457 | struct mmc35240_data *data = iio_priv(indio_dev); | |
458 | ||
459 | regcache_cache_only(data->regmap, true); | |
460 | ||
461 | return 0; | |
462 | } | |
463 | ||
464 | static int mmc35240_resume(struct device *dev) | |
465 | { | |
466 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); | |
467 | struct mmc35240_data *data = iio_priv(indio_dev); | |
468 | int ret; | |
469 | ||
470 | regcache_mark_dirty(data->regmap); | |
471 | ret = regcache_sync_region(data->regmap, MMC35240_REG_CTRL0, | |
472 | MMC35240_REG_CTRL1); | |
473 | if (ret < 0) | |
474 | dev_err(dev, "Failed to restore control registers\n"); | |
475 | ||
476 | regcache_cache_only(data->regmap, false); | |
477 | ||
478 | return 0; | |
479 | } | |
480 | #endif | |
481 | ||
482 | static const struct dev_pm_ops mmc35240_pm_ops = { | |
483 | SET_SYSTEM_SLEEP_PM_OPS(mmc35240_suspend, mmc35240_resume) | |
484 | }; | |
485 | ||
d11715f0 DB |
486 | static const struct acpi_device_id mmc35240_acpi_match[] = { |
487 | {"MMC35240", 0}, | |
488 | { }, | |
489 | }; | |
490 | MODULE_DEVICE_TABLE(acpi, mmc35240_acpi_match); | |
491 | ||
abeb6b1e DB |
492 | static const struct i2c_device_id mmc35240_id[] = { |
493 | {"MMC35240", 0}, | |
494 | {} | |
495 | }; | |
496 | MODULE_DEVICE_TABLE(i2c, mmc35240_id); | |
497 | ||
498 | static struct i2c_driver mmc35240_driver = { | |
499 | .driver = { | |
500 | .name = MMC35240_DRV_NAME, | |
553a776b | 501 | .pm = &mmc35240_pm_ops, |
d11715f0 | 502 | .acpi_match_table = ACPI_PTR(mmc35240_acpi_match), |
abeb6b1e DB |
503 | }, |
504 | .probe = mmc35240_probe, | |
505 | .id_table = mmc35240_id, | |
506 | }; | |
507 | ||
508 | module_i2c_driver(mmc35240_driver); | |
509 | ||
510 | MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>"); | |
511 | MODULE_DESCRIPTION("MEMSIC MMC35240 magnetic sensor driver"); | |
512 | MODULE_LICENSE("GPL v2"); |