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