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